首页 > 解决方案 > 在 discord.js 中的时间限制之前获取收集的消息

问题描述

有没有办法在时间限制到期之前接收来自 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 都是为了全局,因为它必须是递归的)

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

标签: javascriptnode.jsdiscord.js

解决方案


经过一些测试,似乎只有在达到设置选项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);
});

推荐阅读