【问题标题】:Get collected message before time limit in discord.js在 discord.js 中的时间限制之前获取收集的消息
【发布时间】:2019-11-02 06:30:31
【问题描述】:

有没有办法在时间限制到期之前接收来自 discord.js 收集器的消息?

我尝试使用collector.on collect,但它在我设置的时间限制后触发。

这是我目前拥有的:

this.collected = false
        this.collector = new Discord.MessageCollector(msg.channel, m => m.author.bot === false,{time: 10000});
        this.collector.on('collect', message =>{
            if(!this.collected){
                this.collected = true
                console.log(message)
                msg.channel.send(message.content)
                this.collector.stop()
               //Insert the same thing here(Copy+Paste the same code here)
            }
        });

(所有的 this 都是为了全局,因为它必须是递归的)

我希望收集器在收到第一条消息时发出一个事件,但使用当前代码它只在时间限制之后才这样做。

【问题讨论】:

  • 每次收集器检测到通过过滤器的消息时都应该调用 collect 事件,因此您不必等待超时来获取收集的消息。如果删除 if (!this.collected)condition 会发生什么?
  • 哦,对不起,我忘了补充,我只需要收集一条消息,反正它遍历所有消息,我只需要收集一条消息。
  • collector.stop() 正如它所说的,应该停止收集器;所以你的情况对我来说似乎有点多余。否则,您的代码对我来说似乎很好,我不明白为什么它没有按预期工作。顺便说一句,您的评论 \\Insert the same thing here 是什么意思?您的问题中是否还有更多代码未包含在内?
  • 是的,它必须是递归的

标签: javascript node.js discord.js


【解决方案1】:

经过一些测试,collect 事件似乎仅在达到设置time 选项之后 发出。似乎它实际上并没有在收到消息时收集消息,而是在计时器用完时收集消息。这是否是故意的,我不确定。

由于您只需要一定数量的消息,您可以设置您的收集器的maxMatches 选项。然后,如果在达到time 限制之前收集到该数量的消息,则收集器将发出collect 事件并停止。

this.collector = new Discord.MessageCollector(msg.channel, m => !m.author.bot, { maxMatches: 1, time: 10000 });

this.collector.on('collect', message => {
  msg.channel.send(message.content)
    .catch(console.error);
});

【讨论】:

  • .first() 将崩溃,因为收集的变量包含消息,而不是集合
  • 我的错,编辑了代码。我在想TextChannel.awaitMessages()
猜你喜欢
  • 2022-01-21
  • 2020-12-30
  • 1970-01-01
  • 2021-03-31
  • 2021-12-18
  • 2021-10-17
  • 2019-11-25
  • 2020-10-25
  • 2018-06-05
相关资源
最近更新 更多