首页 > 解决方案 > 有没有办法使用 Discord.js 回复机器人以激活命令。此外,给用户一定的时间来使用该命令

问题描述

我正在尝试制作一个不和谐的机器人,它使用命令和确认删除频道。我已经制作了一个嵌入,但我不知道如何制作它,您可以在使用命令后键入“确认”之类的单词并激活删除通道部分。另外,我想让他们只有 10 秒的时间来输入“确认”来激活命令。我希望这一切都有意义。我是新手,现在已经是凌晨 4 点了,哈哈。这是我到目前为止的一段代码:

 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;
}
});

因此,在管理员执行 !nuke 后,他们有 10 秒的时间无法启动 nuke(删除频道)。因此,在发送嵌入后,我希望他们必须输入 Confirm 才能删除他们正在输入的频道。我该如何做到这一点?再次希望您能理解这一点。这是我第一次大声笑。

标签: javascriptdiscorddiscord.js

解决方案


您可以发送确认嵌入,等待它发布并在同一频道中使用createMessageCollector.

对于收集器filter,您可以检查传入消息是否来自要删除频道的同一用户(因此其他人无法确认该操作),并检查消息内容是否为“确认”。您可能希望它不区分大小写,因此您可以将消息转换为小写。

您还可以添加一些选项,例如消息的最大数量(应该是一个),以及收集器收集消息的最长时间。我将它设置为提到的 10 秒,在这 10 秒后它会发送一条消息,让原始发布者知道该操作已取消,他们无法再确认 nuke 命令。

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;
}

推荐阅读