feat: Update reminders to have public ones
All checks were successful
Build Docker / Build Docker (push) Successful in 51s

This commit is contained in:
Nathan Woodburn 2024-02-08 11:22:23 +11:00
parent 7064f0a1f7
commit cd3a61f542
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 19 additions and 19 deletions

13
bot.py
View File

@ -460,15 +460,19 @@ async def ai(ctx, message: str):
await ctx.response.send_message(chatai.chat(prompt))
@tree.command(name="remindme", description="Remind me")
async def remindme(ctx, when: str, reminder: str):
async def remindme(ctx, when: str, reminder: str, public: bool = False):
time_delta = parse_time(when)
if time_delta is not None:
# Schedule the reminder
reminder_time = datetime.datetime.now() + time_delta
if not public:
store_reminder(ctx.user.id, reminder_time, reminder)
await ctx.response.send_message("I've set a reminder for you in " + when + ".\n" + reminder,ephemeral=True)
else:
store_reminder(ctx.user.id, reminder_time, reminder, True, ctx.channel.id)
await ctx.response.send_message("I've set a reminder for you in " + when + ".\n" + reminder)
else:
await ctx.response.send_message("Invalid time format. Please use something like `1d 3h` or `4hr`.",ephemeral=True)
@ -613,12 +617,15 @@ async def check_reminders():
now = datetime.datetime.now()
reminders = read_reminders()
for reminder in reminders[:]:
for reminder in 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(embed=tools.embed("Reminder", reminder['text']))
if 'public' in reminder:
if reminder['public']:
channel = client.get_channel(int(reminder['channel_id']))
await channel.send(embed=tools.embed("Reminder", reminder['text']))
print("Reminder sent for "+str(reminder), flush=True)
reminders.remove(reminder)

View File

@ -1,6 +1,7 @@
import datetime
import re
import discord
import json
REMINDERS_FILE_PATH = '/mnt/reminders.txt'
@ -26,30 +27,22 @@ def parse_time(time_str):
def read_reminders():
try:
with open(REMINDERS_FILE_PATH, 'r') as file:
reminders = [line.strip().split(',') for line in file.readlines()]
# Remove empty lines
reminders = [r for r in reminders if len(r) == 3]
if len(reminders) == 0:
return []
return [{
'user_id': r[0],
'time': r[1],
'text': r[2]
} for r in reminders]
return json.load(file)
except FileNotFoundError:
return []
def write_reminders(reminders):
with open(REMINDERS_FILE_PATH, 'w') as file:
for reminder in reminders:
file.write(f"{reminder['user_id']},{reminder['time']},{reminder['text']}\n")
json.dump(reminders, file)
def store_reminder(user_id, reminder_time, reminder_text):
def store_reminder(user_id, reminder_time, reminder_text, public=False, channel_id=None):
reminders = read_reminders()
reminders.append({
'user_id': str(user_id),
'time': reminder_time.strftime("%Y-%m-%d %H:%M:%S"),
'text': reminder_text
'text': reminder_text,
'public': public,
'channel_id': channel_id
})
write_reminders(reminders)