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

17
bot.py
View File

@ -460,15 +460,19 @@ async def ai(ctx, message: str):
await ctx.response.send_message(chatai.chat(prompt)) await ctx.response.send_message(chatai.chat(prompt))
@tree.command(name="remindme", description="Remind me") @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) time_delta = parse_time(when)
if time_delta is not None: if time_delta is not None:
# Schedule the reminder # Schedule the reminder
reminder_time = datetime.datetime.now() + time_delta reminder_time = datetime.datetime.now() + time_delta
store_reminder(ctx.user.id, reminder_time, reminder) if not public:
await ctx.response.send_message("I've set a reminder for you in " + when + ".\n" + reminder,ephemeral=True) 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: 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)
@ -613,12 +617,15 @@ async def check_reminders():
now = datetime.datetime.now() now = datetime.datetime.now()
reminders = read_reminders() reminders = read_reminders()
for reminder in reminders[:]: for reminder in 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(embed=tools.embed("Reminder", reminder['text'])) 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) print("Reminder sent for "+str(reminder), flush=True)
reminders.remove(reminder) reminders.remove(reminder)

View File

@ -1,6 +1,7 @@
import datetime import datetime
import re import re
import discord import discord
import json
REMINDERS_FILE_PATH = '/mnt/reminders.txt' REMINDERS_FILE_PATH = '/mnt/reminders.txt'
@ -26,30 +27,22 @@ def parse_time(time_str):
def read_reminders(): def read_reminders():
try: try:
with open(REMINDERS_FILE_PATH, 'r') as file: with open(REMINDERS_FILE_PATH, 'r') as file:
reminders = [line.strip().split(',') for line in file.readlines()] return json.load(file)
# 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]
except FileNotFoundError: except FileNotFoundError:
return [] return []
def write_reminders(reminders): def write_reminders(reminders):
with open(REMINDERS_FILE_PATH, 'w') as file: with open(REMINDERS_FILE_PATH, 'w') as file:
for reminder in reminders: json.dump(reminders, file)
file.write(f"{reminder['user_id']},{reminder['time']},{reminder['text']}\n")
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 = read_reminders()
reminders.append({ reminders.append({
'user_id': str(user_id), 'user_id': str(user_id),
'time': reminder_time.strftime("%Y-%m-%d %H:%M:%S"), '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) write_reminders(reminders)