Home > other >  interaction collector discord.js collects all interactions that are executed after the collector dec
interaction collector discord.js collects all interactions that are executed after the collector dec

Time:03-04

Meaning: there is some content and a button in the response to the slash command. The task of the button is to delete the message. What we managed to do was to force the button to delete the message, but the problem is that not only the message in which the button is deleted, but also other messages.

const {
  MessageEmbed,
  MessageActionRow,
  MessageButton
} = require('discord.js')
const {
  SlashCommandBuilder
} = require('@discordjs/builders')
const Color = 'RANDOM'
module.exports = {
    name: 'Name',
    data: new SlashCommandBuilder()
      .setName('Name')
      .setDescription('Description')
      .addStringOption(option => option.setName('choice')
        .setDescription('Description')
        .setRequired(true)
        .addChoice('choice1', 'choice1')
        .addChoice('choice2', 'choice2')
        .addChoice('choice3', 'choice3') 
    async run(interaction) {
          const Embed = new MessageEmbed()
            .setColor(Color)
            .setTimestamp()
            .setImage(url)
          const Btns = new MessageActionRow()
            .addComponents(new MessageButton()
              .setCustomId('DeleteMsg')
              .setStyle('DANGER')
              .setLabel('Удалить сообщение (Только автор или администратор)'))
          interaction.reply({
            embeds: [Embed],
            components: [Btns]
          })
          try {
            const collector = interaction.channel.createMessageComponentCollector({
              time: 60000
            })
            collector.on('collect', async i => {
              interaction.deleteReply()
            })
          } catch (error) {
            console.log(error)
          }
   }
}

CodePudding user response:

Ok first of all, you should always add a filter to your component collector. (How many items to collect, check if correct users used your component, ...)

You should also have a real custom ID (like an UUID or something like that) because if there's multiple calls of your command in the same channel then, your custom ID wouldn't be custom anywhere and could be collected by every collector running in the channel.

  • Related