【问题标题】:Discord message collector repeats collected messageDiscord 消息收集器重复收集的消息
【发布时间】:2021-04-20 13:17:28
【问题描述】:

我正在尝试让我的 Discord 机器人检测何时在短时间内发送了两条或更多类似的消息,然后发送一条消息。

我无法理解收集器,所以它只是一遍又一遍地重复信息。我应该切换到等待消息吗?

const filter = m => m.content.toLowerCase() == message.content.toLowerCase();
const collector = message.channel.createMessageCollector(filter, {max: 2, time: 6000});
collector.on('collect', m => {
    message.channel.send(message.content);
});

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    当我第一次使用收集器时,我也遇到了这个错误。诀窍是你必须停止收集器。即使有人回答。

    所以,代码是:

    collector.on("collect", m => {
        message.channel.send(message.content)l
        collector.stop()
    });
    

    我确信可能有一个合乎逻辑的解释和另一种方法,但这对我有用,而且很容易。

    【讨论】:

    • thankssssss 它有点工作,但它仍然发送 2 条消息:/ idk 为什么
    【解决方案2】:

    如果您想在这六秒内有两条内容相同的消息时立即发送消息,则需要在createMessageCollector 选项中将max 值设置为1

    此外,您不希望每次收集消息时都发送消息。如果您将max 设置为1,您可以订阅collectorend 事件并在有两条相同消息时从那里发送消息。由于end 事件也会在该时间范围内没有相同消息时触发,因此您还需要检查collected.size 并仅在大于零时发送消息。

    client.on('message', (message) => {
      if (message.author.bot) return;
    
      const filter = (m) => m.content.toLowerCase() === message.content.toLowerCase();
      const collector = message.channel.createMessageCollector(filter, {
        max: 1,
        time: 6000,
      });
    
      collector.on('collect', (m) => {
        console.log(`Collected ${m.content}`);
      });
    
      collector.on('end', (collected) => {
        console.log(`Collected ${collected.size} items`);
    
        if (collected.size > 0) {
          message.channel.send(
            `Naughty, naughty! You sent _"${message.content}"_ more than once in a short time...`,
          );
        }
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 2020-08-12
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2020-11-02
      • 2021-05-12
      • 2021-12-11
      • 2020-10-25
      相关资源
      最近更新 更多