問題描述
有沒有辦法在時間限制到期之前接收來自 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);
});
這篇關于在 discord.js 中的時間限制之前獲取收集的消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!