feat(aurora): migrated to Red's builtin timedelta/relativedelta parsing
All checks were successful
Actions / Lint Code (Ruff & Pylint) (push) Successful in 26s
Actions / Build Documentation (MkDocs) (push) Successful in 20s

This commit is contained in:
SeaswimmerTheFsh 2024-03-08 14:19:48 -05:00
parent 6035aea5c6
commit f4efcb8ea5
Signed by: cswimr
GPG key ID: B8953EC01E5C4063
5 changed files with 50 additions and 62 deletions

View file

@ -15,12 +15,11 @@ from math import ceil
import discord
import humanize
from discord.ext import tasks
from pytimeparse2 import disable_dateutil, parse
from redbot.core import app_commands, commands, data_manager
from redbot.core.app_commands import Choice
from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import (box, error, humanize_list,
warning)
from redbot.core.commands.converter import parse_relativedelta, parse_timedelta
from redbot.core.utils.chat_formatting import box, error, humanize_list, warning
from aurora.importers.aurora import ImportAuroraView
from aurora.importers.galacticbot import ImportGalacticBotView
@ -29,17 +28,10 @@ from aurora.menus.guild import Guild
from aurora.menus.immune import Immune
from aurora.menus.overrides import Overrides
from aurora.utilities.config import config, register_config
from aurora.utilities.database import (connect, create_guild_table, fetch_case,
mysql_log)
from aurora.utilities.factory import (addrole_embed, case_factory,
changes_factory, evidenceformat_factory,
guild_embed, immune_embed,
message_factory, overrides_embed)
from aurora.utilities.database import connect, create_guild_table, fetch_case, mysql_log
from aurora.utilities.factory import addrole_embed, case_factory, changes_factory, evidenceformat_factory, guild_embed, immune_embed, message_factory, overrides_embed
from aurora.utilities.logger import logger
from aurora.utilities.utils import (check_moddable, check_permissions,
convert_timedelta_to_str,
fetch_channel_dict, fetch_user_dict,
generate_dict, log, send_evidenceformat)
from aurora.utilities.utils import check_moddable, check_permissions, convert_timedelta_to_str, fetch_channel_dict, fetch_user_dict, generate_dict, log, send_evidenceformat, timedelta_from_relativedelta
class Aurora(commands.Cog):
@ -84,7 +76,6 @@ class Aurora(commands.Cog):
super().__init__()
self.bot = bot
register_config(config)
disable_dateutil()
self.handle_expiry.start()
def format_help_for_context(self, ctx: commands.Context) -> str:
@ -332,13 +323,10 @@ class Aurora(commands.Cog):
return
if duration is not None:
try:
parsed_time = parse(
sval=duration, as_timedelta=True, raise_exception=True
)
except ValueError:
parsed_time = parse_timedelta(duration)
if parsed_time is None:
await interaction.response.send_message(
error("Please provide a valid duration!"), ephemeral=True
content=error("Please provide a valid duration!"), ephemeral=True
)
return
else:
@ -440,16 +428,15 @@ class Aurora(commands.Cog):
return
try:
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
except ValueError:
parsed_time = parse_timedelta(duration, maximum=timedelta(days=28))
if parsed_time is None:
await interaction.response.send_message(
error("Please provide a valid duration!"), ephemeral=True
)
return
except commands.BadArgument:
await interaction.response.send_message(
error("Please provide a valid duration!"), ephemeral=True
)
return
if parsed_time.total_seconds() / 1000 > 2419200000:
await interaction.response.send_message(
error("Please provide a duration that is less than 28 days.")
error("Please provide a duration that is less than 28 days."), ephemeral=True
)
return
@ -684,15 +671,13 @@ class Aurora(commands.Cog):
pass
if duration:
try:
parsed_time = parse(
sval=duration, as_timedelta=True, raise_exception=True
)
except ValueError:
parsed_time = parse_relativedelta(duration)
if parsed_time is None:
await interaction.response.send_message(
error("Please provide a valid duration!"), ephemeral=True
content=error("Please provide a valid duration!"), ephemeral=True
)
return
parsed_time = timedelta_from_relativedelta(parsed_time)
await interaction.response.send_message(
content=f"{target.mention} has been banned for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`"
@ -1380,11 +1365,8 @@ class Aurora(commands.Cog):
case_dict = await fetch_case(case, interaction.guild.id)
if case_dict:
if duration:
try:
parsed_time = parse(
sval=duration, as_timedelta=True, raise_exception=True
)
except ValueError:
parsed_time = parse_timedelta(duration)
if parsed_time is None:
await interaction.response.send_message(
error("Please provide a valid duration!"), ephemeral=True
)
@ -1700,15 +1682,28 @@ class Aurora(commands.Cog):
)
@aurora.command(aliases=["tdc", "td", "timedeltaconvert"])
async def timedelta(self, ctx: commands.Context, *, duration: str):
async def timedelta(self, ctx: commands.Context, *, duration: str) -> None:
"""This command converts a duration to a [`timedelta`](https://docs.python.org/3/library/datetime.html#datetime.timedelta) Python object.
You cannot convert years or months as they are not fixed units. Use `[p]aurora relativedelta` for that.
**Example usage**
`[p]aurora timedelta 1 day 15hr 82 minutes 52s`
**Output**
`1 day, 16:22:52`"""
try:
parsed_time = parse(duration, as_timedelta=True, raise_exception=True)
await ctx.send(f"`{str(parsed_time)}`")
except ValueError:
parsed_time = parse_timedelta(duration)
if parsed_time is None:
await ctx.send(error("Please provide a convertible value!"))
await ctx.send(f"`{parsed_time}`")
@aurora.command(aliases=["rdc", "rd", "relativedeltaconvert"])
async def relativedelta(self, ctx: commands.Context, *, duration: str) -> None:
"""This command converts a duration to a [`relativedelta`](https://dateutil.readthedocs.io/en/stable/relativedelta.html) Python object.
**Example usage**
`[p]aurora relativedelta 3 years 1 day 15hr 82 minutes 52s`
**Output**
`relativedelta(years=+3, days=+1, hours=+15, minutes=+82, seconds=+52)`"""
parsed_time = parse_relativedelta(duration)
if parsed_time is None:
await ctx.send(error("Please provide a convertible value!"))
await ctx.send(f"`{parsed_time}`")