fix: Task scheduler and embeds
All checks were successful
Build Docker / Build Docker (push) Successful in 18s

This commit is contained in:
Nathan Woodburn 2023-11-14 13:56:55 +11:00
parent a05702c2be
commit 078403eaf7
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

27
bot.py
View File

@ -16,6 +16,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
import chatai import chatai
from timeparser import parse_time, read_reminders, store_reminder, write_reminders from timeparser import parse_time, read_reminders, store_reminder, write_reminders
import asyncio import asyncio
from discord.ext import tasks, commands
@ -319,6 +320,7 @@ def updateStatus():
activity=discord.Activity(type=discord.ActivityType.watching, name=activityMessage) activity=discord.Activity(type=discord.ActivityType.watching, name=activityMessage)
client.loop.create_task(client.change_presence(activity=activity)) client.loop.create_task(client.change_presence(activity=activity))
@tasks.loop(hours=24)
async def checkForSSLExpiry(): async def checkForSSLExpiry():
with open("/mnt/sslnotify.txt", "r") as file: with open("/mnt/sslnotify.txt", "r") as file:
lines = file.readlines() lines = file.readlines()
@ -468,8 +470,8 @@ async def remindme(ctx, when: str, reminder: str):
else: else:
await ctx.response.send_message("Invalid time format. Please use something like `1d 3h` or `4hr`.",ephemeral=True) 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(): async def check_reminders():
print("Checking reminders", flush=True)
now = datetime.datetime.now() now = datetime.datetime.now()
reminders = read_reminders() reminders = read_reminders()
@ -477,8 +479,17 @@ async def check_reminders():
reminder_time = datetime.datetime.strptime(reminder['time'], "%Y-%m-%d %H:%M:%S") reminder_time = datetime.datetime.strptime(reminder['time'], "%Y-%m-%d %H:%M:%S")
if reminder_time <= now: if reminder_time <= now:
user = await client.fetch_user(int(reminder['user_id'])) user = await client.fetch_user(int(reminder['user_id']))
await user.send("G'day,\nI need to remind you of:\n" + reminder['text']) embed = discord.Embed(
print("Reminder sent", flush=True) 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) reminders.remove(reminder)
write_reminders(reminders) write_reminders(reminders)
@ -491,12 +502,10 @@ async def on_ready():
await tree.sync() await tree.sync()
updateStatus() updateStatus()
await checkForSSLExpiry() await checkForSSLExpiry()
check_reminders.start()
checkForSSLExpiry.start()
# Every 12 hours check for SSL expiry client.run(TOKEN)
scheduler = AsyncIOScheduler()
scheduler.add_job(checkForSSLExpiry, 'interval', hours=12)
scheduler.add_job(check_reminders, 'interval', seconds=30)
scheduler.start()
client.run(TOKEN)