Get collected message before time limit in discord.js

我的梦境 提交于 2021-02-08 04:42:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!