1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-03 00:53:12 +00:00

Merge pull request #286 from allthingslinux/284-edit-time-format-for-remindme

Add more time formats to `remindme` and make message ephemeral
This commit is contained in:
electron271 2024-06-24 16:27:11 -05:00 committed by GitHub
commit 326ed607f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import asyncio
import contextlib
import datetime
@ -88,6 +89,10 @@ class RemindMe(commands.Cog):
# Delete the reminder after sending
await self.db_controller.reminders.delete_reminder(reminder.id)
# wait for a second so that the reminder is deleted before checking for more reminders
# who knows if this works, it seems to
await asyncio.sleep(1)
# Run update again to check if there are any more reminders
await self.update()
@ -146,9 +151,9 @@ class RemindMe(commands.Cog):
interaction : discord.Interaction
The discord interaction object.
time : str
The time to wait before reminding.
Time in the format `[number][M/w/d/h/m/s]`.
reminder : str
The reminder message.
Reminder content.
"""
seconds = convert_to_seconds(time)
@ -156,7 +161,7 @@ class RemindMe(commands.Cog):
# Check if the time is valid (this is set to 0 if the time is invalid via convert_to_seconds)
if seconds == 0:
await interaction.response.send_message(
"Invalid time format. Please use a format like `1d`, `2h`, `3m`, etc. (only days, hours, and minutes are supported)"
"Invalid time format. Please use the format `[number][M/w/d/h/m/s]`.",
)
return
@ -191,7 +196,7 @@ class RemindMe(commands.Cog):
logger.error(f"Error creating reminder: {e}")
await interaction.response.send_message(embed=embed)
await interaction.response.send_message(embed=embed, ephemeral=True)
# Run update again to check if this reminder is the closest
await self.update()

View file

@ -7,7 +7,7 @@ import discord
def convert_to_seconds(time_str: str) -> int:
"""
Converts a formatted time string with 'd' for days, 'h' for hours, and 'm' for minutes into total seconds.
Converts a formatted time string with the formats Mwdhms
Any unexpected format leads to returning 0.
Parameters:
@ -21,14 +21,14 @@ def convert_to_seconds(time_str: str) -> int:
The total seconds from the formatted time string.
"""
# Lowercase to standardize the input
time_str = time_str.lower()
# Time conversion factors from units to seconds
time_units = {
"M": 2592000, # Months to seconds
"w": 604800, # Weeks to seconds
"d": 86400, # Days to seconds
"h": 3600, # Hours to seconds
"m": 60, # Minutes to seconds
"s": 1, # Seconds to seconds
}
total_seconds = 0