Is there a way to get user from mention discordjs v12?

Viewed 433

Sorry if there are any typos, but english is not my first language.

Hi, I have a problem when I am trying to get userinfo from a mention. It works perfectly fine when I am doing the command on a non nicknamed user, but when I try on a nicknamed user, it only returns undefined. Keep in mind that I am using WOKCommands to handle my slash commands, and the error is happening on a slash command.

Here is the code for the command:

const { MessageEmbed } = require('discord.js');
const moment = require('moment');

module.exports = {
    slash: true,
    testOnly: true,
    description: 'En spioneringskommando.',
    minArgs: 1,
    expectedArgs: '<Mention>',
    callback: ({ args, client, interaction }) => {
        const userId = args[0].toString().replace(/[\\<>@#&!]/g, "");
        const guild = client.guilds.cache.get(interaction.guild_id);
        const member = guild.members.cache.get(userId);

        const embed = new MessageEmbed()
            .setTitle("Spioneringsprogram v1.0")
            .setDescription(`Bruker: ${member.user.username}`)
            .setColor("RANDOM")
            .addField("Kallenavn:", `${member.nickname ? `${member.nickname}` : 'Ingen'}`, false)
            .addField("Ble medlem av discord:", `${moment.utc(member.user.createdAt).format('DD/MM/YY')}`, false)
            .addField("Ble medlem av discord serveren:", `${moment.utc(member.joinedAt).format('DD/MM/YY')}`, false)
            .setFooter(`ID: ${member.user.id}`)
            .setTimestamp();

        return embed;
    }
}

And here is my index.js file:

require('dotenv').config();

const Discord = require("discord.js");
const WOKCommands = require('wokcommands');
const client = new Discord.Client();

const guildId = 'censored'

client.on('ready', () => {
    console.log("Bot is ready!");
    new WOKCommands(client, {
        commandsDir: 'commands',
        testServers: [guildId],
        showWarns: false
    });
});

Thanks for any help I can get.

1 Answers

since you got the command as a slash command ("slashCommand:true") You should be using "interaction." examples:

(interaction.user.username) // The username of the interaction's user
(interaction.user.id)       // The id of the interaction's user
(interaction.user.avatarURL)// The avatar's url of the interaction's user

But you seem to be new to wokcommands, and slash commands at general, so contact me at: Glowy#8213 And i'll help you out

Related