Skip to content

Commit

Permalink
Code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
darakelian committed Sep 17, 2024
1 parent 59fd441 commit 4baee49
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/remind_me/cog.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from logging import getLogger
import asyncio
import datetime
import dateparser
import django.utils.timezone
import tabulate
from asgiref.sync import sync_to_async
from discord.ext import commands
from discord.ext.commands import Bot, Context
from discord_bot.cog import BaseCog
from .models import Reminder
from gam.settings._django import USE_TZ


logger = getLogger(__name__)
Expand All @@ -20,7 +20,7 @@ class RemindMeCog(BaseCog):
@commands.Cog.listener()
async def on_ready(self) -> None:
existing_reminders = Reminder.objects.all()
start_time = datetime.datetime.now(datetime.UTC)
start_time = django.utils.timezone.now()
# Check for any reminders that may have expired while the bot was down and send them
# or create timers for the remaining ones
async for existing_reminder in existing_reminders:
Expand Down Expand Up @@ -48,10 +48,10 @@ async def _send_reminder(self, reminder: Reminder) -> None:
"No channel was found for %d, doing nothing.",
reminder.initial_channel_id,
)
await reminder.adelete()
await channel.send(
f'<@{reminder.creator_id}>, I am reminding you about "{reminder.reminder_text}"'
)
else:
await channel.send(
f'<@{reminder.creator_id}>, I am reminding you about "{reminder.reminder_text}"'
)
await reminder.adelete()

@commands.command(
Expand All @@ -60,9 +60,11 @@ async def _send_reminder(self, reminder: Reminder) -> None:
async def remindme(
self, ctx: Context[Bot], time_text: str, reminder_text: str
) -> None:
start_time = django.utils.timezone.now()
settings: dateparser._Settings = {
"PREFER_DATES_FROM": "future",
"RELATIVE_BASE": datetime.datetime.now(),
"RELATIVE_BASE": start_time,
"RETURN_AS_TIMEZONE_AWARE": USE_TZ,
}
reminder_time = dateparser.parse(time_text, settings=settings)
if not reminder_time:
Expand All @@ -72,15 +74,13 @@ async def remindme(
"Your reminder time is invalid, please make sure it is correct and try again!"
)
return
reminder = Reminder(
reminder = await Reminder.objects.acreate(
creator_id=ctx.author.id,
reminder_text=reminder_text,
reminder_time=reminder_time,
initial_channel_id=ctx.channel.id,
)
await reminder.asave()
await ctx.channel.send(f"Got it, I will remind you in {time_text}")
start_time = datetime.datetime.now()
delta = (reminder_time - start_time).seconds
self._create_timer(delta, reminder)

Expand All @@ -107,17 +107,14 @@ async def list_reminders(self, ctx: Context[Bot]) -> None:
else:
await ctx.send("You have no active reminders")

@sync_to_async
def _get_reminder(self, creator_id: int, reminder_id: int) -> Reminder:
return Reminder.objects.filter(creator_id=creator_id).order_by("reminder_time")[
reminder_id
]

@commands.command(help="Delete a particular reminder you have")
async def delete_reminder(self, ctx: Context[Bot], reminder_id: int) -> None:
try:
reminder = await self._get_reminder(ctx.author.id, reminder_id)
await reminder.adelete()
await (
Reminder.objects.filter(creator_id=ctx.author.id)
.order_by("reminder_time")[reminder_id - 1 : reminder_id]
.adelete()
)
await ctx.send("Reminder has been deleted")
except Reminder.DoesNotExist:
await ctx.send("No reminder exists for that ID!")

0 comments on commit 4baee49

Please sign in to comment.