From 078403eaf7db8b033e3ac12adda291de9462a151 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Tue, 14 Nov 2023 13:56:55 +1100 Subject: [PATCH] fix: Task scheduler and embeds --- bot.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index e5d28db..62de82a 100644 --- a/bot.py +++ b/bot.py @@ -16,6 +16,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler import chatai from timeparser import parse_time, read_reminders, store_reminder, write_reminders import asyncio +from discord.ext import tasks, commands @@ -319,6 +320,7 @@ def updateStatus(): activity=discord.Activity(type=discord.ActivityType.watching, name=activityMessage) client.loop.create_task(client.change_presence(activity=activity)) +@tasks.loop(hours=24) async def checkForSSLExpiry(): with open("/mnt/sslnotify.txt", "r") as file: lines = file.readlines() @@ -468,8 +470,8 @@ async def remindme(ctx, when: str, reminder: str): else: await ctx.response.send_message("Invalid time format. Please use something like `1d 3h` or `4hr`.",ephemeral=True) +@tasks.loop(seconds=10) async def check_reminders(): - print("Checking reminders", flush=True) now = datetime.datetime.now() reminders = read_reminders() @@ -477,8 +479,17 @@ async def check_reminders(): reminder_time = datetime.datetime.strptime(reminder['time'], "%Y-%m-%d %H:%M:%S") if reminder_time <= now: user = await client.fetch_user(int(reminder['user_id'])) - await user.send("G'day,\nI need to remind you of:\n" + reminder['text']) - print("Reminder sent", flush=True) + embed = discord.Embed( + title='Reminder', + description=reminder['text'], + color=discord.Color.dark_purple() + ) + + # Set a footer for the embed + embed.set_footer(text='Powered by Nathan.Woodburn/') + await user.send(embed=embed) + + print("Reminder sent for "+str(reminder), flush=True) reminders.remove(reminder) write_reminders(reminders) @@ -491,12 +502,10 @@ async def on_ready(): await tree.sync() updateStatus() await checkForSSLExpiry() + check_reminders.start() + checkForSSLExpiry.start() -# Every 12 hours check for SSL expiry -scheduler = AsyncIOScheduler() -scheduler.add_job(checkForSSLExpiry, 'interval', hours=12) -scheduler.add_job(check_reminders, 'interval', seconds=30) -scheduler.start() +client.run(TOKEN) + -client.run(TOKEN) \ No newline at end of file