I’m working on a Discord bot that needs to send automated messages at exactly midnight (00:00 CET). Right now I only know how to use delays but that’s not what I want. I need the bot to trigger a function precisely when the clock hits midnight.
I have an array with different text messages that the bot should broadcast when midnight arrives. Can someone help me figure out the best approach for this?
Here’s the current function I use for storing the data:
You could just use JavaScript’s Date object with setInterval to check every minute for midnight. Make a function that grabs the current time, checks if hours and minutes are both zero, then sends your message. Something like setInterval(() => { const now = new Date(); if (now.getHours() === 0 && now.getMinutes() === 0) { sendMidnightMessages(); } }, 60000); No external dependencies needed and you control the timing yourself. I’ve been running something similar for daily announcements - works great, just add a flag so it doesn’t fire multiple times in the same minute.
discord.js has built-in scheduling now. use client.setTimeout() but calculate milliseconds until midnight instead of hardcoding delays. try const msUntilMidnight = new Date().setHours(24,0,0,0) - Date.now() then set your timeout for that duration. when it fires, set another one for 24hrs later. i’ve used this for months without issues and it’s way simpler than adding more dependencies.
Check out node-cron for this. It’s built for scheduling tasks at exact times instead of using delays that drift over time. Install with npm install node-cron, then set up a job like cron.schedule('0 0 * * *', () => { /* your midnight function */ }); - the first part sets exactly when to run (midnight daily). Way more reliable than setTimeout or setInterval since it handles system time changes and doesn’t pile up timing errors. I’ve used node-cron in production bots for over a year and it handles timezone conversions well if you need CET.