Home > Software design >  How to add the user id to the button interaction?
How to add the user id to the button interaction?

Time:05-18

I am working on a Discord bot and I am trying to get the buttons to work only for the user who used the command.

I'm sorry if I sound dumb, but I don't know how to explain it correctly.

What I have so far, is the command test which sends a message in the channel, with 2 buttons attached to it, and what I want, is to attach the user's ID to the buttons.

.setCustomId(button1-${message.author.id})

This actually works if all the code is in one file. But it doesn't work if I split the code for the buttons.

I tried adding -${interaction.user.id} to the button name, but it doesn't work, as it gives me "interaction is not defined".

module.exports = {
  data: {
    name: `button1`
  },
  async execute (interaction, client) {

  interaction.channel.send({ content: "This button works!"})

    
  }
}

Is there any way to add the user's id to the button name?

CodePudding user response:

This should help you out here. Any time an application command is used, that message has an element to identify who sent it interaction.message.interaction.user.id which is the user id of the person who sent the command.

However in your use, you are using a message, which is fine and it will still work as a message has an element to specify who is being replied to interaction.message.mentions.users so to put that into use for you, try the below code. You won't need to add the users id to the button code.

if (message.content.startsWith(PREFIX   "test")) {
    const row = new MessageActionRow().addComponents(
        new MessageButton()
            .setLabel("✔")
            .setStyle("PRIMARY")
            .setCustomId(`button1`),
        new MessageButton()
            .setLabel("✔")
            .setStyle("SECONDARY")
            .setCustomId(`button2`),
    );
    message.reply({
        content: "Hello",
        components: [row],
    });
}

The below section is part of your interactionCreate code if part of the main bot.js file

client.on('interactionCreate', async interaction => {
    if (interaction.isButton()) {
        const buttonID = interaction.customId;
        if (buttonID === 'button1') {
            if (interaction.message.mentions.users.get(interaction.user.id)) {
                interaction.channel.send({
                    content: "This button works!",
                });
                // This is the sender of the command
                console.log(true);
            } else {
                // This is not the sender
                console.log(false);
                return;
            }
        } else if (buttonID === 'button2') {
            if (interaction.message.mentions.users.get(interaction.user.id)) {
                interaction.channel.send({
                    content: "This button works!",
                });
                console.log(true);
            } else {
                // This is not the sender
                console.log(false);
                return;
            }
        }
    }
});

In the event you have your interactionCreate command as a separate file, the code would look like this

module.exports = {
    data: {
        name: `interactionCreate`,
    },
    async execute(interaction, client) {
        const buttonID = interaction.customId;
        if (buttonID === 'button1') {
            if (interaction.message.mentions.users.get(interaction.user.id)) {
                interaction.channel.send({
                    content: "This button works!",
                });
                // This is the sender of the command
                console.log(true);
            } else {
                // This is not the sender
                console.log(false);
                return;
            }
        } else if (buttonID === 'button2') {
            if (interaction.message.mentions.users.get(interaction.user.id)) {
                interaction.channel.send({
                    content: "This button works!",
                });
                console.log(true);
            } else {
                // This is not the sender
                console.log(false);
                return;
            }
        }
    },
};
  • Related