Improved error handling; Introduced i18n for /register, /unregister and /guess

This commit is contained in:
2025-04-29 13:50:38 +02:00
parent 28d6340847
commit 9d39b803f3
8 changed files with 177 additions and 76 deletions

View File

@@ -10,6 +10,7 @@ from discord.utils import basic_autocomplete
from libbot.i18n import _, in_every_locale
from classes import PycordEvent, PycordEventStage, PycordGuild, PycordUser
from classes.errors import EventNotFoundError, GuildNotFoundError
from classes.pycord_bot import PycordBot
from modules.utils import autocomplete_active_events, get_logger, get_unix_timestamp
@@ -22,7 +23,6 @@ class CogRegister(Cog):
def __init__(self, bot: PycordBot):
self.bot: PycordBot = bot
# TODO Introduce i18n
@slash_command(
name="register",
description=_("description", "commands", "register"),
@@ -37,13 +37,16 @@ class CogRegister(Cog):
autocomplete=basic_autocomplete(autocomplete_active_events),
)
async def command_register(self, ctx: ApplicationContext, event: str) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
try:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
except (InvalidId, GuildNotFoundError):
await ctx.respond(self.bot._("unexpected_error", "messages", locale=ctx.locale))
return
try:
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Event was not found.")
except (InvalidId, EventNotFoundError):
await ctx.respond(self.bot._("event_not_found", "messages", locale=ctx.locale))
return
if not guild.is_configured():
@@ -53,25 +56,33 @@ class CogRegister(Cog):
user: PycordUser = await self.bot.find_user(ctx.author, ctx.guild)
if user.is_jailed:
# TODO Make a nice message
await ctx.respond(
"You are jailed and cannot interact with events. Please, contact the administrator."
)
await ctx.respond(self.bot._("jailed_error", "messages", locale=ctx.locale))
return
if pycord_event._id in user.registered_event_ids:
# TODO Make a nice message
await ctx.respond("You are already registered for this event.")
await ctx.respond(self.bot._("register_already_registered", "messages", locale=ctx.locale))
return
await user.event_register(pycord_event._id, cache=self.bot.cache)
# TODO Make a nice message
await ctx.respond(
f"You are now registered for the event **{pycord_event.name}**.\n\nNew channel will be created for you and further instructions will be provided as soon as the event starts <t:{get_unix_timestamp(pycord_event.starts, to_utc=True)}:R>. Good luck!"
event_ongoing: bool = pycord_event.starts.replace(tzinfo=ZoneInfo("UTC")) < datetime.now(
tz=ZoneInfo("UTC")
)
if pycord_event.starts.replace(tzinfo=ZoneInfo("UTC")) < datetime.now(tz=ZoneInfo("UTC")):
registered_message: str = (
self.bot._("register_success_ongoing", "messages", locale=ctx.locale).format(
event_name=pycord_event.name
)
if event_ongoing
else self.bot._("register_success_scheduled", "messages", locale=ctx.locale).format(
event_name=pycord_event.name,
event_starts=get_unix_timestamp(pycord_event.starts, to_utc=True),
)
)
await ctx.respond(registered_message)
if event_ongoing:
await user.set_event(pycord_event._id, cache=self.bot.cache)
user_channel: TextChannel = await user.setup_event_channel(
@@ -89,7 +100,11 @@ class CogRegister(Cog):
await self.bot.notify_admins(
ctx.guild,
guild,
f"Event channel could not be created for user **{ctx.author.display_name}** ({ctx.author.mention}) and event **{pycord_event.name}**.",
self.bot._("admin_user_channel_creation_failed", "messages", locale=ctx.locale).format(
display_name=ctx.author.display_name,
mention=ctx.author.mention,
event_name=pycord_event.name,
),
)
return
@@ -100,9 +115,10 @@ class CogRegister(Cog):
else File(Path(f"data/{pycord_event.thumbnail['id']}"), pycord_event.thumbnail["filename"])
)
# TODO Make a nice message
await user_channel.send(
f"Event **{pycord_event.name}** has already started!\n\nUse slash command `/guess` to suggest your answers to each event stage.",
self.bot._("register_already_started", "messages", locale=ctx.locale).format(
event_name=pycord_event.name
),
file=thumbnail,
)