discord.jscommanddiceslashmention

Cannot Get Discord.js to Mention User in Command


Alright, so I'm working on making a d100 slash command for Discord.js v13, the app works almost perfectly I've tried a few things but have run into a wall. The bot gives the undoctored user name, without a mention and ignored nicknames, rather than the nickname.

This is the closest I've gotten to my goal (a command that @mentions the nickname)

 //Remember to run " node deploy-commands.js " to register your commands!


    const { SlashCommandBuilder } = require('@discordjs/builders');
    
    module.exports = {
        data: new SlashCommandBuilder()
            .setName('100')
            .setDescription('rolls a 100'),
        async execute(interaction) {
            await interaction.reply(`${interaction.user.username} rolled ${Math.round(Math.random() *100 )}`);
        },
    };

Please Ignore the node command at the top I tend to forget to node when adding new commands for slash commands.

trying to change the ${interaction.user.username} for nicknames, mentions, etc lead to:

undefined rolled 44
undefined rolled 55
etc

rather then the username or a @mention

on a few options I got the return:

function userMention(userId) {
    return <@${userId}>;
} rolled 5

if anyone could give me any tips I would be grateful I'm pretty sure its the ${interaction.user.username} that is my issue.


Solution

  • You are trying to use ${interaction.user.username} which is invalid and will always give you undefined, so to mention someone you have to use interaction.member.user and for username: interaction.member.user.username (docs)