简体   繁体   English

有没有办法让命令自行重置,这样它就不会响应两次?

[英]Is there a way to make a command reset itself so it doesn't respond twice?

I'm making a guess the number command, and when the number is guessed you can keep spamming the number and it'll respond.我正在猜测数字命令,当数字被猜到时,您可以继续向该数字发送垃圾邮件,它会响应。

I want it so that the command resets itself and you have to run it again.我想要它,以便命令自行重置,您必须再次运行它。 Is there a way to do this?有没有办法做到这一点? Here is my code for the command:这是我的命令代码:

module.exports = {
    name: "guess",
    description: "guess a number after activating a command",
    async execute(client, message, args, Discord) {
        const numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
        const randomNumber = numbers[Math.floor(Math.random() * numbers.length)];
        let embed = new Discord.MessageEmbed().setColor("#FF0000").setTitle("Guess the number between 1 and 10!");
        message.channel.send(embed);

        await client.on("message", (message) => {
            if (message.content === `${randomNumber}`) {
                message.channel.send("You guessed correctly!");
            }
        });
    },
};

By the way, I am using a dynamic handler so you won't see what activates the command.顺便说一句,我使用的是动态处理程序,因此您不会看到激活命令的内容。

You can't add an event listener inside the event lister.您不能在事件侦听器中添加事件侦听器。 The exported object is already inside a client.on('message', fn) .导出的 object 已经在client.on('message', fn)中。

This is a perfect use case for message collectors though.不过,这对于消息收集器来说是一个完美的用例。 Collectors are useful when you want your bot to receive additional input after the first command was sent.当您希望机器人在发送第一个命令后接收额外的输入时,收集器很有用。

You can set up a filter to only collect messages from the user who typed the command.您可以设置过滤器以仅收集来自键入命令的用户的消息。 You can also set a time limit using the time option, or the maximum number of guesses accepted using the max option .您还可以使用time选项设置时间限制,或者使用max选项设置接受的最大猜测数。

Inside the .on('collect) event, you can check if the response is valid or not and send a message based on that..on('collect)事件中,您可以检查响应是否有效并根据该响应发送消息。 When the correct number is guessed, we use the collector.end() method to stop collecting more messages.当猜到正确的数字时,我们使用collector.end()方法停止收集更多消息。

Check a working example below;检查下面的工作示例; I've added some comments too:我也添加了一些评论:

async execute(client, message, args, Discord) {
  const numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
  const randomNumber = numbers[Math.floor(Math.random() * numbers.length)];
  const maxWait = 30000; // in ms, so it's 30 sec
  const embed = new Discord.MessageEmbed()
    .setColor('#FF0000')
    .setTitle('Guess the number between 1 and 10! 🙈');

  await message.channel.send(embed);

  // filter checks if the response is from the author who typed the command
  // and if the response is one of the possible guesses
  const filter = (response) =>
    response.author.id === message.author.id &&
    numbers.includes(response.content.trim());

  const collector = message.channel.createMessageCollector(filter, {
    // set up the max wait time the collector runs
    time: maxWait,
  });

  // fires when a response is collected
  collector.on('collect', (response) => {
    if (response.content.trim() === randomNumber) {
      message.channel.send(
        `🎉🎉🎉 Woohoo, ${response.author}! 🎉🎉🎉\n\nYour guess is correct, my number was \`${randomNumber}\`.`,
      );
      // the guess is correct, so stop this collector and emit the "end" event
      collector.stop();
    } else {
      // another chance if the response is incorrect
      message.channel.send(
        `Oh, ${response.author}, \`${response.content}\` is not correct... 🙊\nDo you want to try another number?`,
      );
    }
  });

  // 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') return;

    // if there are incorrect guesses
    if (collected.size > 0) {
      return message.channel.send(
        `Ah, ${message.author}. Out of ${collected.size} guess${
          collected.size > 1 ? 'es' : ''
        } you couldn't find the number \`${randomNumber}\`. I'm not saying you're slow, but no more guesses are accepted.`,
      );
    }

    message.channel.send(
      `Okay, ${message.author}, I'm bored and I can't wait any longer. No more guesses are accepted. At least you could have tried...`,
    );
  });
};

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM