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

2022-01-10 00:00:00 node.js javascript discord.js

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

Is there a way to receive messages from discord.js collectors before the time limit expires?

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

I tried using collector.on collected, but it triggered after the time limit I set.

这是我目前拥有的:

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 都是为了全局,因为它必须是递归的)

(The this on everything is for globality, it's because it has to be recursive)

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

I want the collector to emit an event on the moment it receiveves the first message, but with the current code it only does that after the time limit.

推荐答案

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

After some testing, it appears that the collect event is only emitted after the set time option is reached. It seems as though it's not actually collecting the messages when they're received, but instead when the timer runs out. Whether this is intentional or not, I'm not sure.

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

Since you only need a certain amount of messages, you can set the maxMatches option of your collector. Then, if that amount of messages is collected before the time limit is reached, the collector will emit the collect event and stop.

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

相关文章