Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions tux/cogs/utility/remindme.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio

Check warning on line 1 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L1

Added line #L1 was not covered by tests
import contextlib
import datetime

import discord
from discord.ext import commands, tasks
from discord.ext import commands

Check warning on line 6 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L6

Added line #L6 was not covered by tests
from loguru import logger

from prisma.models import Reminder
Expand All @@ -16,25 +17,11 @@
def __init__(self, bot: Tux) -> None:
self.bot = bot
self.db = DatabaseController()
self.check_reminders.start()
self.remindme.usage = generate_usage(self.remindme)

@tasks.loop(seconds=120)
async def check_reminders(self):
reminders = await self.db.reminder.get_unsent_reminders()

try:
for reminder in reminders:
await self.send_reminder(reminder)
await self.db.reminder.update_reminder_status(reminder.reminder_id, sent=True)
logger.debug(f'Status of reminder {reminder.reminder_id} updated to "sent".')

except Exception as e:
logger.error(f"Error sending reminders: {e}")
self._initialized = False

Check warning on line 21 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L21

Added line #L21 was not covered by tests

async def send_reminder(self, reminder: Reminder) -> None:
user = self.bot.get_user(reminder.reminder_user_id)

if user is not None:
embed = EmbedCreator.create_embed(
bot=self.bot,
Expand Down Expand Up @@ -69,9 +56,29 @@
f"Failed to send reminder {reminder.reminder_id}, user with ID {reminder.reminder_user_id} not found.",
)

@check_reminders.before_loop
async def before_check_reminders(self):
await self.bot.wait_until_ready()
try:
await self.db.reminder.delete_reminder_by_id(reminder.reminder_id)
except Exception as e:
logger.error(f"Failed to delete reminder: {e}")

Check warning on line 62 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L59-L62

Added lines #L59 - L62 were not covered by tests

@commands.Cog.listener()
async def on_ready(self) -> None:

Check warning on line 65 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L64-L65

Added lines #L64 - L65 were not covered by tests
if self._initialized:
return

Check warning on line 67 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L67

Added line #L67 was not covered by tests

self._initialized = True

Check warning on line 69 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L69

Added line #L69 was not covered by tests

reminders = await self.db.reminder.get_all_reminders()
dt_now = datetime.datetime.now(datetime.UTC)

Check warning on line 72 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L71-L72

Added lines #L71 - L72 were not covered by tests

for reminder in reminders:
seconds = (reminder.reminder_expires_at - dt_now).total_seconds()

Check warning on line 75 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L75

Added line #L75 was not covered by tests

if seconds <= 0:
await self.send_reminder(reminder)
continue

Check warning on line 79 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L78-L79

Added lines #L78 - L79 were not covered by tests

self.bot.loop.call_later(seconds, asyncio.create_task, self.send_reminder(reminder))

Check warning on line 81 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L81

Added line #L81 was not covered by tests

@commands.hybrid_command(
name="remindme",
Expand Down Expand Up @@ -120,14 +127,16 @@
expires_at = datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=seconds)

try:
await self.db.reminder.insert_reminder(
reminder_obj = await self.db.reminder.insert_reminder(

Check warning on line 130 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L130

Added line #L130 was not covered by tests
reminder_user_id=ctx.author.id,
reminder_content=reminder,
reminder_expires_at=expires_at,
reminder_channel_id=ctx.channel.id if ctx.channel else 0,
guild_id=ctx.guild.id if ctx.guild else 0,
)

self.bot.loop.call_later(seconds, asyncio.create_task, self.send_reminder(reminder_obj))

Check warning on line 138 in tux/cogs/utility/remindme.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/remindme.py#L138

Added line #L138 was not covered by tests

embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.SUCCESS,
Expand All @@ -139,8 +148,7 @@

embed.add_field(
name="Note",
value="- If you have DMs closed, we will attempt to send it in this channel instead.\n"
"- The reminder may be delayed by up to 120 seconds due to the way Tux works.",
value="- If you have DMs closed, we will attempt to send it in this channel instead.\n",
)

except Exception as e:
Expand Down
16 changes: 1 addition & 15 deletions tux/database/controllers/reminder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import UTC, datetime
from datetime import datetime

Check warning on line 1 in tux/database/controllers/reminder.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/reminder.py#L1

Added line #L1 was not covered by tests

from prisma.actions import GuildActions
from prisma.models import Guild, Reminder
Expand Down Expand Up @@ -43,20 +43,6 @@
"""
return await self.find_unique(where={"reminder_id": reminder_id})

async def get_unsent_reminders(self) -> list[Reminder]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making changes to db and controller structure this close to a large migration is not advisable. is this necessary?

"""Get all unsent reminders that have expired.

This method finds reminders that should be sent to users
because their expiration time has passed.

Returns
-------
list[Reminder]
List of unsent expired reminders
"""
now = datetime.now(UTC)
return await self.find_many(where={"reminder_sent": False, "reminder_expires_at": {"lte": now}})

async def insert_reminder(
self,
reminder_user_id: int,
Expand Down