在Discord.js中在时间限制之前获取收集的消息 [英] Get collected message before time limit in discord.js

查看:43
本文介绍了在Discord.js中在时间限制之前获取收集的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在期限到期之前从discord.js收集器接收消息?

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

我尝试使用collect.on进行收集,但是它在我设置的时间限制后触发.

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

(这是为了全局性,因为它必须是递归的)

(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.

推荐答案

经过一些测试,看来之后发出discord.js.org/#/docs/main/stable/typedef/CollectorOptions"rel =" nofollow noreferrer> 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中在时间限制之前获取收集的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆