I’m working on a Discord bot using Python and the discord.py library. I need to set up an automated task that triggers every week on Sunday at midnight. I’ve been searching for ways to implement this scheduling feature but I’m not sure about the best approach.
My bot is built with Python 3.7 and discord.py version 1.3.2. I want the bot to execute a specific function automatically without any user commands or manual intervention. The timing needs to be precise - exactly at 00:00 on Sunday.
What’s the most reliable method to achieve this weekly scheduling in a Discord bot? Should I use built-in discord.py features or external libraries?
just use time.sleep() in a separate thread if u want something simple. write a function to calculate seconds until sunday midnight, sleep that long, then run ur weekly task. wrap it in a while loop so it repeats. way fewer dependencies than apscheduler and u don’t need to learn the tasks extension.
I’ve done something similar with my Discord bot. The trick is combining discord.py’s tasks extension with datetime calculations instead of just using simple intervals. Use the @tasks.loop() decorator, but calculate the exact seconds until next Sunday midnight first. I wrote a helper function for this, then used asyncio.sleep() for the initial delay before the loop starts. This handles timezones way better than external schedulers and keeps everything in your bot process. Just make sure your bot handles restarts properly - I save the last execution timestamp to a text file so it knows if it missed a run while offline.
Skip the tasks extension for weekly scheduling - use APScheduler with discord.py instead. It’s way better at handling complex schedules and gives you proper cron functionality. Install with pip install apscheduler, then use scheduler.add_job(your_function, 'cron', day_of_week=6, hour=0, minute=0) for Sunday midnight. APScheduler automatically handles daylight saving time and recovers better from downtime. I’ve run production bots with this setup for 2+ years - it’s rock solid. Start the scheduler in your on_ready event and shut it down gracefully when the bot stops so you don’t get hanging processes.