问题
Is there a way to receive messages from discord.js collectors before the time limit expires?
I tried using collector.on collected, but it triggered after the time limit I set.
Here is what I currently have:
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)
}
});
(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.
回答1:
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.
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);
});
来源:https://stackoverflow.com/questions/56668985/get-collected-message-before-time-limit-in-discord-js