Reputation: 25
I'm trying to make my Discord bot to detect when two or more similar messages are sent in a short time, and then send one message.
I'm having trouble understanding collectors so it just repeats the message over and over. Should I switch to await messages?
const filter = m => m.content.toLowerCase() == message.content.toLowerCase();
const collector = message.channel.createMessageCollector(filter, {max: 2, time: 6000});
collector.on('collect', m => {
message.channel.send(message.content);
});
Upvotes: 2
Views: 887
Reputation: 23179
You need to set the max
value to 1
in the createMessageCollector
options if you want to send a message as soon as there are two messages with the same content in that six seconds.
Also, you don't want to send a message every time a message is collected. If you set the max
to 1
, you can subscribe to the collector
's end
event and send the message from there once there are two identical messages. As the end
event also fires when there are no identical messages within that timeframe, you will also need to check the collected.size
and only send a message if it's greater than zero.
client.on('message', (message) => {
if (message.author.bot) return;
const filter = (m) => m.content.toLowerCase() === message.content.toLowerCase();
const collector = message.channel.createMessageCollector(filter, {
max: 1,
time: 6000,
});
collector.on('collect', (m) => {
console.log(`Collected ${m.content}`);
});
collector.on('end', (collected) => {
console.log(`Collected ${collected.size} items`);
if (collected.size > 0) {
message.channel.send(
`Naughty, naughty! You sent _"${message.content}"_ more than once in a short time...`,
);
}
});
});
Upvotes: 1
Reputation: 459
I got this error too when I was first using collectors. The trick is that you have to stop the collector. Even if someone does answer.
So, the code would be:
collector.on("collect", m => {
message.channel.send(message.content)l
collector.stop()
});
I'm sure there is probably a logical explanation and an alternative approach, but this worked for me and it's pretty easy.
Upvotes: 0