From cd3a61f54227223bd57ca9417623e400cf520f42 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 8 Feb 2024 11:22:23 +1100 Subject: [PATCH] feat: Update reminders to have public ones --- bot.py | 17 ++++++++++++----- tools.py | 21 +++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/bot.py b/bot.py index e399fb1..5fd45ac 100644 --- a/bot.py +++ b/bot.py @@ -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 - 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) + 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) diff --git a/tools.py b/tools.py index d322959..1c7e01d 100644 --- a/tools.py +++ b/tools.py @@ -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)