在Discord.js中在时间限制之前获取收集的消息

时间:2019-06-19 13:35:09

标签: javascript node.js discord.js

有没有办法在期限到期之前从discord.js收集器接收消息?

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

这是我目前拥有的:

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)
            }
        });

(这是为了全局性,因为它必须是递归的)

我希望收集器在收到第一条消息时就发出一个事件,但是对于当前代码,它仅在时间限制之后执行。

1 个答案:

答案 0 :(得分:0)

经过一些测试,看来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);
});