I am trying to send a message every x amount of seconds in a discord.js bot. I know how to do this put the problem I am having is that it is spamming messages even when I have slowmode enabled. How can I fix this?
client.on('message', message => {
if (message.content === '$ww) {
let count = 0;
let ecount = 0;
for(let x = 0; x < 9000; x++) {
message.channel.send(`hy`)
.then(m => {
count++;
})
}
}
});
You can use setInterval()
to repeat your function every X milliseconds. For example:
setInterval(() => {
message.channel.send(`hy`).then(() => count++);
}, 10000);
setInterval(() => console.log('hey'), 1000)
The code you've provided is spamming because it is not waiting 10 seconds; it is counting from 0 to 9000 and for every count, it sends the message 'hy', so it quickly spams 9000 'hy' messages.