HzMeth
HzMeth

Reputation: 55

Delaying "for each" method

I'm trying delete the channels of server for server admins. It's for when the Server Owner wants to reset discord, he could use the command. However it was to fast so I needed add a delay before deleting each channel cause Discord employees can think its raid. Here's my code;

if (message.member.id == "<Server Owner ID>") {
    message.guild.channels.forEach(channel => {
        setTimeout(function(){channel.delete()}, 3000);
    });
}

else {
    message.channel.send("You don't have permission to execute this command.").then(msg => {
        msg.delete("5000")
    })
}}

How can I handle with this?

Upvotes: 0

Views: 134

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371019

Use another parameter in the forEach callback to indicate the index of the current item being iterated over, and set the timeout to some multiplier of that index. That way, for example, the first timeout will trigger after 3 seconds, the second will trigger after 6 seconds, etc.

message.guild.channels.forEach((channel, i) => {
  setTimeout(() => channel.delete(), (i + 1) * 3000);
});

Upvotes: 6

Related Questions