Skip to content
← Back to Community
How to make a countdown timer on discord.js?
Profile icon
tallphin

I have a discord bot, and I want to make a command where, whenever someone writes "(prefix)count x seconds", the bot will count down, and when that time is over, send a @mention to the person on the same server. Thanks!

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
tallphin
Comments
hotnewtop
Profile icon
Coder100

Alright, sorry for the delay, went to bed. Let's do this!

client.on('message', msg => { // todo: make this an actual command lol if (msg == '1') { let duration = 5; // todo: make this change based on user input let interval = setInterval(() => { msg.channel.send(duration --); // todo: make this look better lol if (duration == 0) { clearInterval(interval); // todo: add times up message } }, 1000) // 1000 = 1 second // NOTE: ANYTHING THAT GOES HERE WILL BE EXECUTED BEFORE THE SETINTERVAL! BE WARY! } })
Profile icon
tallphin

@Coder100 Thanks so much!

Profile icon
Coder100
Profile icon
Coder100

did it work? @GoldenFox5313

Profile icon
tallphin

@Coder100 I haven't tested it, but I will soon. Currently, where I am, it's about 2 AM. I'll let you know in the morning

Profile icon
Coder100
Profile icon
Coder100
Profile icon
tallphin

Thanks, @Coder100. Anyways, good night!

Profile icon
MichaelDiaz6

@Coder100

solid code. adjusted for my needs ty

Profile icon
Whacko
var timer = 0 var timeToStop = 6 SetTimout(function() { if (timer == timeToStop) { breakTimout(); /* And whatever else you need him to say. */ } else { console.log(timer); /* So that way you know where the timer is */ timer += 1; }; }, 1000)

So, after 6 seconds the timer will stop.

Profile icon
tallphin

Oh wait, I missed a very important part of the sentence

Profile icon
Coder100

In discord type what you want the bot to say send scrnsht here

Profile icon
Coder100

Count-down? Sounds like someone is getting their discord api privileges revoked...

Profile icon
tallphin
Profile icon
Coder100

if many people use it, your bot is going to spam a lot, and spamming = ban @GoldenFox5313

Profile icon
Coder100

also ratelimiting too @GoldenFox5313

Profile icon
tallphin

@Coder100 I'm sorry, I don't understand. Could you tell me what you mean by that?

Profile icon
tallphin

@Coder100 Oh wait, I did not read the other replies. Actually, that could be a problem. Maybe the bot could DM the user?

Profile icon
Coder100

probably not, it's still sending a message @GoldenFox5313

Profile icon
Coder100

you know how you get a woah there way to spicy whenever you send messages to quickly? That happens to bots too @GoldenFox5313

Profile icon
tallphin

@Coder100 Oh yeah, because, the way I understand it (I'm not sure though), bots on discord are basically like normal accounts, but with commands. So discord is basically treating them like "human" accounts.

Profile icon
Coder100

*human accounts with restrictions @GoldenFox5313