Skip to content
Advertisement

DiscordJS filtering users from buttons and limiting buttons to user who used command

I’m trying to make a bot that lets users create embeds using a series of commands and its going well. But I encountered a problem, the bot uses buttons but to confirm something like “Are you sure this is your title?” any user can click the button; which is a problem. I only want the user who used the command to be able to use the buttons.

Here is what I tried at the start:

client.on('interactionCreate', async interaction => {
const filter = m => !m.author.bot && m.author.id === interaction.user.id;
const collector = interaction.channel.createMessageCollector({filter, max:1, time:10000});

if (!interaction.isButton() && interaction.user.id != interaction.message.author.id) return;

I tried to use interaction.user.id != or === interaction.message.author.id but that doesn’t work

Advertisement

Answer

The best way to make a button only for a single user is to put the user ID in the custom ID

For example, set the ID to title-${interaction.user.id}

if(!interaction.isButton()) return;
if (!interaction.customId.endsWith(interaction.user.id)) {
  return interaction.reply({
    content: "This button is not for you",
    ephemeral: true
  })
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement