2
votes

I'm trying to make a discord bot where it deletes channels with a command along with a confirmation. I have made an Embed but I don't know how I would make it where you can type a word like "Confirm" after you used the command and it activates the delete channel part. Also, I would like to make it where they only have 10 seconds to type "Confirm" to activate the command. I hope this all makes sense. I'm new to this and it's almost 4 am in the morning lol. Here is the piece of code I have so far:

 switch (args[0]) {
    case 'nuke':
        if (message.member.roles.cache.has('865492279334928424')) {
            const embed = new Discord.MessageEmbed()
                .setTitle(':rotating_light: Nuke Enabled :rotating_light:')
                .addField('You have 10 seconds to confirm the nuke.', 'Please type "Confirm" to launch the nuke.')
                .setFooter("This nuke will destroy the channel along with its messages.")
                .setColor(0xff0000)
            message.channel.send(embed);
        }
        break;
}
});

So, after an admin does !nuke they have 10 seconds before they can't launch the nuke (delete the channel). So after the embed is sent I want them to have to type Confirm to then delete the channel they are typing in. How would I be able to do this? Once again I hope you understand this. It's my first time lol.

1

1 Answers

2
votes

You can send the confirmation embed, wait for it to be posted and in the same channel set up a message collector using createMessageCollector.

For the collector's filter, you can check if the incoming message is coming from the same user who want to delete the channel (so no-one else can confirm the action), and check if the message content is "Confirm". You probably want it to be case-insensitive, so you can convert the message to lowercase.

You can also add some options, like the maximum number of messages (should be one), and the maximum time the collector is collecting messages. I set it to the mentioned 10 seconds, and after this 10 seconds it sends a message letting the original poster know that the action is cancelled, they can no longer confirm the nuke command.

switch (args[0]) {
  case 'nuke':
    if (message.member.roles.cache.has('865492279334928424')) {
      const embed = new Discord.MessageEmbed()
        .setTitle(':rotating_light: Nuke Enabled :rotating_light:')
        .addField(
          'You have 10 seconds to confirm the nuke.',
          'Please type "Confirm" to launch the nuke.',
        )
        .setFooter(
          'This nuke will destroy the channel along with its messages.',
        )
        .setColor(0xff0000);

      // send the message and wait for it to be sent
      const confirmation = await message.channel.send(embed);

      // filter checks if the response is from the author who typed the command
      // and if they typed confirm
      const filter = (m) =>
        m.content.toLowerCase() === 'confirm' &&
        m.author.id === message.author.id;

      // set up a message collector to check if there are any responses
      const collector = confirmation.channel.createMessageCollector(filter, {
        max: 1,
        // set up the max wait time the collector runs (in ms)
        time: 10000,
      });

      // fires when a response is collected
      collector.on('collect', async (m) => {
        try {
          const channel = /**** channel here ****/
          await channel.delete();
          return message.channel.send(`You have deleted ${channel}!`);
        } catch (error) {
          return message.channel.send(`Oops, error: ${error}`);
        }
      });

      // fires when the collector is finished collecting
      collector.on('end', (collected, reason) => {
        // only send a message when the "end" event fires because of timeout
        if (reason === 'time') {
          message.channel.send(
            `${message.author}, it's been 10 seconds without confirmation. The action has been cancelled, channel is not deleted.`,
          );
        }
      });
    }
    break;
}