【问题标题】:How do I make this only work on a command我如何使它仅适用于命令
【发布时间】:2021-03-02 06:55:53
【问题描述】:

我正在尝试使用 discord.js 在 discord 上制作一个测验机器人,但是当我运行此代码时,即使它不是正确的词,它也会回复。我该如何解决?

client.once('message', msg => {
  if (msg.content === 'quiz') 
    msg.channel.send('Ok quiz time!');
    msg.channel.send('Can coding be hard. yes ???? or no ????')
    msg.react('????').then(() => msg.react('????'));
const filter = (reaction, user) => {
  return ['????', '????'].includes(reaction.emoji.name) && user.id === msg.author.id;
};

msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then(collected => {
    const reaction = collected.first();

    if (reaction.emoji.name === '????') {
      msg.reply('correct answer');
      msg.channel.send ('Good job!')
    } else {
      msg.reply('Incorrect.');
    }
  })
  .catch(_collected => {
    msg.reply('You ran out of time.');
  })
  });

当我说一个不和谐的随机词时,我的机器人会用“编码难不难。是 ???? 或否 ????”回复它

【问题讨论】:

  • bruh ur if 语句将在它正下方的行结束.. USE BRACKETS PLS 因为msg.channel.send('Ok quiz time!'); 下面的所有其他行都是OUTSIDE if 语句

标签: node.js discord discord.js bots


【解决方案1】:

您的括号未对齐 您更正的代码应该是这样的。

client.once("message", (msg) => {
  if (msg.content === "quiz") {
    msg.channel.send("Ok quiz time!");
    msg.channel.send("Can coding be hard. yes ? or no ?");
    msg.react("?").then(() => msg.react("?"));
    const filter = (reaction, user) => {
      return (
        ["?", "?"].includes(reaction.emoji.name) && user.id === msg.author.id
      );
    };

    msg
      .awaitReactions(filter, { max: 1, time: 60000, errors: ["time"] })
      .then((collected) => {
        const reaction = collected.first();

        if (reaction.emoji.name === "?") {
          msg.reply("correct answer");
          msg.channel.send("Good job!");
        } else {
          msg.reply("Incorrect.");
        }
      })
      .catch((_collected) => {
        msg.reply("You ran out of time.");
      });
  }
});

【讨论】:

  • 请修正缩进
  • 请通过代码格式化程序运行。
  • 已修复,抱歉,我不熟悉此处的缩进。
  • 酷!请在将来更清楚标题和您的问题。如果不够清楚,可能会删除您的帖子:)
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多