javascript - 有没有办法使用 Discord.js 回复机器人来激活命令。另外,给用户一定的时间来使用该命令

标签 javascript discord discord.js

我正在尝试制作一个不和谐的机器人,它可以通过命令和确认来删除 channel 。我已经制作了一个嵌入,但我不知道如何制作它,您可以在使用命令后键入“确认”之类的单词,并激活删除 channel 部分。另外,我想让他们只有 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(删除 channel )。因此,在发送嵌入后,我希望他们必须输入“确认”才能删除他们正在输入的 channel 。我怎样才能做到这一点?我再次希望你能理解这一点。这是我第一次哈哈。

最佳答案

您可以发送确认嵌入,等待其发布,并在同一 channel 中使用 createMessageCollector 设置消息收集器.

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

您还可以添加一些选项,例如最大消息数(应该是一条)以及收集器收集消息的最长时间。我将其设置为提到的 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;
}

关于javascript - 有没有办法使用 Discord.js 回复机器人来激活命令。另外,给用户一定的时间来使用该命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68405425/

相关文章:

javascript - 添加表行时 react 'this'未定义

python - Discord.py 和 youtube_dl、 "Read error"和 "The session has been invalidated for some reason"

java - Discord OAuth2 重定向 URL 无法使用 JavaFX WebView JDK12 显示

discord.js - 获取和删除discord.js v13中的所有斜杠命令

javascript - 如何获取对象元素的序号?

javascript - 如何让机器人说角色但不说 ping?

javascript - 在 SharePoint2010 Client OM/ECMAScript 中获取当前用户

javascript - 检查 IE8 是否使用纯 Javascript

javascript - 如何使用 javascript/jQuery 检测 <div> 大小的调整

Python - 获取 Discord 服务器中的用户列表