【问题标题】:Collector goes wrong | discord.js收集器出错 |不和谐.js
【发布时间】:2021-05-26 19:45:12
【问题描述】:

我希望人们能够对消息做出反应,然后机器人会通过 3 个问题与他们联系。问题是它自己跳过所有问题并自己回答。

如果您能很好地向我描述问题,以便不再发生,我会很高兴。这是我的代码:

client.on('messageReactionAdd', async (reaction, user) => {
  if (reaction.message.partial) await reaction.message.fetch();
  if (reaction.partial) await reaction.fetch();

  if (user.bot) return;

  if (reaction.message.channel.id === '809490905236373558') {
    if (reaction.emoji.name === '✅') {
      const questions = [`Test1`, `Test2`, `Test3`];
      const dmChannel = await reaction.message.guild.members.cache
        .get(user.id)
        .send('**Beantworte die Fragen du keks**');
      const collector = dmChannel.channel.createMessageCollector(() => true);
      let i = 0;
      const res = [];
      dmChannel.channel.send(questions[0]);
      collector.on('collect', async (msg) => {
        if (questions.length == i) return collector.stop('MAX');
        const answer = msg.content;
        res.push({ question: questions[i], answer });
        i++;
        if (questions.length == i) return collector.stop('MAX');
        else {
          dmChannel.channel.send(questions[i]);
        }
      });

      collector.on('end', async (collected, reason) => {
        if (reason == 'MAX') {
          const data = reaction.message.guild.channels.cache.find(
            (ch) =>
              ch.name.toLowerCase() == 'apply-final-bewerbungen' &&
              ch.type == 'text',
          );
          await data.send(
            `${reaction.message.member || reaction.message.author} (${
              reaction.message.author.tag
            }) hat eine Bewerbung abgegeben!\n\n${res
              .map((d) => `**${d.question}** \n ${d.answer}`)
              .join('\n\n')}`,
          );
        }
      });
    }
  }
});

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    它无需等待即可发送所有问题,因为您不检查消息收集器中的传入消息是否来自成员。当机器人发送第一个问题时,您的收集器会捕获一条传入消息(机器人的消息),然后它会发送下一个消息,直到它到达末尾。

    您可以检查msg.author 是否是您的collector 中的机器人,如果是则停止响应。以下应该有效:

    collector.on('collect', async (msg) => {
      if (msg.author.bot) return;
      if (questions.length == i) return collector.stop('MAX');
    

    【讨论】:

    • 谢谢,但现在又出现了一个问题,也许你可以看看 :) 我正试图让我的机器人向用户发送私人消息,他可以在 1 和 2 之间进行选择,如果他按1,就会出现一条消息,但问题是他不再接听并关闭代码:starb.in/UMonUK.cs错误:imgur.com/hEAKshI
    • 不客气。现在看起来这是另一个问题,仅通过阅读您的代码,我不确定您为什么要将 channel.id 与成员进行比较。它永远不会是true。请随时 post a new question 使用您更新的代码,以便我们进行调查。如果它回答了您的原始问题,请不要忘记单击复选标记 (✓) 接受它。这样别人就知道你的问题已经解决了。见What should I do when someone answers my question?
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2021-10-18
    • 2020-11-18
    • 2021-11-15
    • 2020-12-18
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多