我想要存档的内容...

我正在try 为我的discord bot创建一个猜测命令处理程序.它将从数组中随机选取一个项目,用户需要根据一些提示在1分钟内猜测所选单词.

问题

命令处理程序只允许运行一次特定代码,因此它不会检测用户是否回复.因此,我无法检测用户是否在特定时间内回复了来自机器人的特定消息.

密码


// Copyright © 2022 <copyright holders>

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


const Discord = require("discord.js");
const { prefix, colors } = require("./../utils/config.json");
const embedColor = colors.default;
const embedError = colors.error;

module.exports = {
  name: "guess",
  description: "Find a secret animal by following the hints to get reputation.",
  aliases: ["?", "h"],
  usage: "[prefix] + ping",
  guildOnly: false,
  args: false,
  slash: true,
  permissions: {
    bot: [],
    user: [],
  },
  execute: async (message, args, client) => {
    const { commands } = message.client;

    function question() {
      // Sending message

      const animals = ["pig", "elephant", "shark", "dog", "cat", "lion", "wolf", "bee", "snake", "bird"];

      const random = Math.floor(Math.random() * animals.length);
      const random_choice = (animals[random]);


//    msg2 = message.channel.send("See or Change?");
//    if (msg2.type === 'REPLY')
//      console.log(msg1.content);

      let filter = m => m.author.id === message.author.id
      message.channel.send(`Are you sure to delete all data? \`YES\` / \`NO\``).then(() => {
        message.channel.awaitMessages(filter, {
          max: 1,
          time: 10000,
          errors: ['time']
        })
        .then(message => {
          message = message.first();
          if (message.content.toLowerCase() == random_choice) {
            message.channel.send(`Got it right`)
          } else {
            message.channel.send('The Animal was: `' + random_choice + '`!')
          }
        })
        .catch(collected => {
          message.channel.send('Timeout');
        });
      })
    }

    let { channel, reference: reply } = message

    const cmdHelpEmbed = new Discord.MessageEmbed()
      .setTitle("**PING**")
      .setDescription(`Hey, ` + message.author.username + `! I am ready to play guess game`)
      .setColor(embedColor)
      .addFields(
        {
          name: 'How to play?',
          value: 'This game is simple, I will select a random animal and give you 3 hints. You just need to find the correct animal to win reputation. To answer reply to this message, within 1 minute'
        }, {
          name: '\u200B',
          value: '**RULES**'
        }, {
          name: 'Rule 1:',
          value: 'No Cheating',
          inline: true
        },
        {
          name: 'Rule 2:',
          value: 'Have fun!',
          inline: true
        },
      );

    message.channel.send({
      embeds: [cmdHelpEmbed],
    });

    var delayInMilliseconds = 5000; // 5 seconds

    setTimeout(function() {
      question();
    }, delayInMilliseconds);
  },
};

推荐答案

这是因为您定义收集器的方式.

而不是:

message.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time']})

执行:

message.channel.awaitMessages({filter, max: 1, time: 10000, errors: ['time']})

Javascript相关问答推荐

具有相同参数的JS类

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

微软Edge Select 间隙鼠标退出问题

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

Angular中计算信号和getter的区别

将状态向下传递给映射的子元素

JQuery. show()工作,但. hide()不工作

如何在不创建新键的情况下动态更改 map 中的项目?

为什么按钮会随浮动属性一起移动?

简单的PayPal按钮集成导致404错误

如何将多维数组插入到另一个多维数组中?

在Java中寻找三次Bezier曲线上的点及其Angular

将Node.js包发布到GitHub包-错误ENEEDAUTH

如何在箭头函数中引入or子句?

如何使用<;Link>;执行与JS Reaction中的";window.Location=/GameOverDied;";类似的操作?

Reaction即使在重新呈现后也会在方法内部保留局部值

使用CEPRESS截取时,cy.Wait()在等待5000ms的第一个路由请求时超时

rxjs在每次迭代后更新数组的可观察值

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

使用VITE开发服务器处理错误