I've been trying to schedule a discord.py function to be run every day at midnight.
async def send():
updays[0] += 1
await client.get_channel(XXXXXXXXXXXXXXXXX).send(f"It has been {updays[0]} days.")
I first tried it with schedule
schedule.every().day.at('00:00').do(send)
And then learned it can't handle async functions. I then ran into an SO answer that explained how to do it with just Discord.py
iself. My code now looks like this:
import ...
load_dotenv()
client = discord.Client(intents=discord.Intents.all())
updays = [0]
...
@tasks.loop(hours=24)
async def send():
updays[0] += 1
await client.get_channel(XXXXXXXXXXXXXXXXX).send(f"It has been {updays[0]} days.")
@send.before_loop
async def before_send():
hour, minute = 0, 0
await client.wait_until_ready()
now = datetime.now()
future = datetime.datetime(now.year, now.month, now.day, hour, minute)
if now.hour >= hour and now.minute >= minute:
future += timedelta(days=1)
await asyncio.sleep((future - now).seconds)
if __name__ == '__main__':
client.run(os.getenv('TOKEN'))
send.start()
But it doesn't do anything. The rest of the bot works, but nothing happens at the specified time.