Compare commits

..

2 commits

Author SHA1 Message Date
78630dc317
feat(aurora): added timedelta_from_string() function
Some checks failed
Actions / Build Documentation (MkDocs) (pull_request) Successful in 31s
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 50s
2024-06-04 23:31:52 -04:00
74d122a2e7
fix(aurora): catch importer errors instead of letting the entire import process die 2024-06-04 23:31:36 -04:00
2 changed files with 32 additions and 23 deletions

View file

@ -1,6 +1,5 @@
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
import json import json
from datetime import timedelta
from time import time from time import time
from typing import Dict from typing import Dict
@ -10,6 +9,7 @@ from redbot.core.utils.chat_formatting import box, warning
from ..models.moderation import Moderation from ..models.moderation import Moderation
from ..utilities.database import connect, create_guild_table from ..utilities.database import connect, create_guild_table
from ..utilities.utils import timedelta_from_string
class ImportAuroraView(ui.View): class ImportAuroraView(ui.View):
@ -91,31 +91,33 @@ class ImportAuroraView(ui.View):
metadata.update({"imported_timestamp": int(time())}) metadata.update({"imported_timestamp": int(time())})
if case["duration"] != "NULL" and case["duration"] is not None: if case["duration"] != "NULL" and case["duration"] is not None:
hours, minutes, seconds = map(int, case["duration"].split(":")) duration = timedelta_from_string(case["duration"])
duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
else: else:
duration = None duration = None
Moderation.log( try:
bot=interaction.client, Moderation.log(
guild_id=self.ctx.guild.id, bot=interaction.client,
moderator_id=case["moderator_id"], guild_id=self.ctx.guild.id,
moderation_type=case["moderation_type"], moderator_id=case["moderator_id"],
target_type=case["target_type"], moderation_type=case["moderation_type"],
target_id=case["target_id"], target_type=case["target_type"],
role_id=case["role_id"], target_id=case["target_id"],
duration=duration, role_id=case["role_id"],
reason=case["reason"], duration=duration,
timestamp=case["timestamp"], reason=case["reason"],
resolved=case["resolved"], timestamp=case["timestamp"],
resolved_by=case["resolved_by"], resolved=case["resolved"],
resolved_reason=case["resolve_reason"], resolved_by=case["resolved_by"],
expired=case["expired"], resolved_reason=case["resolve_reason"],
changes=changes, expired=case["expired"],
metadata=metadata, changes=changes,
database=database, metadata=metadata,
return_obj=False database=database,
) return_obj=False
)
except Exception as e:
failed_cases.append(str(case["moderation_id"]) + f": {e}")
await interaction.edit_original_response(content="Import complete.") await interaction.edit_original_response(content="Import complete.")
if failed_cases: if failed_cases:

View file

@ -194,6 +194,13 @@ def timedelta_from_relativedelta(relativedelta: rd) -> timedelta:
then = now - relativedelta then = now - relativedelta
return now - then return now - then
def timedelta_from_string(string: str) -> timedelta:
"""Converts a string to a timedelta object."""
from .logger import logger
hours, minutes, seconds = map(int, string.split(":"))
logger.debug("%s | hours: %s, minutes: %s, seconds: %s", string, hours, minutes, seconds)
return timedelta(hours=hours, minutes=minutes, seconds=seconds)
def timedelta_to_string(timedelta: timedelta) -> str: def timedelta_to_string(timedelta: timedelta) -> str:
"""Converts a timedelta object to a string.""" """Converts a timedelta object to a string."""
hours, remainder = divmod(timedelta.seconds, 3600) hours, remainder = divmod(timedelta.seconds, 3600)