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

@@ -6,6 +6,7 @@ from discord import ApplicationContext, Cog, File, option, slash_command
from libbot.i18n import _, in_every_locale
from classes import PycordEvent, PycordEventStage, PycordGuild, PycordUser
from classes.errors import EventNotFoundError, EventStageNotFoundError, GuildNotFoundError
from classes.pycord_bot import PycordBot
@@ -26,7 +27,11 @@ class CogGuess(Cog):
description_localizations=in_every_locale("description", "commands", "guess", "options", "answer"),
)
async def command_guess(self, ctx: ApplicationContext, answer: 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
if not guild.is_configured():
await ctx.respond(self.bot._("guild_unconfigured", "messages", locale=ctx.locale))
@@ -35,30 +40,24 @@ class CogGuess(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 user.current_event_id is None or user.current_stage_id is None:
# TODO Make a nice message
await ctx.respond(
"You have no ongoing events. You can register for events using the `/register` command."
)
await ctx.respond(self.bot._("guess_unregistered", "messages", locale=ctx.locale))
return
try:
event: PycordEvent = await self.bot.find_event(event_id=user.current_event_id)
stage: PycordEventStage = await self.bot.find_event_stage(user.current_stage_id)
except (InvalidId, RuntimeError):
# TODO Make a nice message
await ctx.respond("Your event could not be found. Please, contact the administrator.")
except (InvalidId, EventNotFoundError, EventStageNotFoundError):
await ctx.respond(self.bot._("guess_incorrect_event", "messages", locale=ctx.locale))
return
if ctx.channel_id != user.event_channels[str(event._id)]:
# TODO Make a nice message
await ctx.respond("Usage outside own event channel is not allowed.", ephemeral=True)
await ctx.respond(
self.bot._("guess_incorrect_channel", "messages", locale=ctx.locale), ephemeral=True
)
return
if answer.lower() != stage.answer.lower():
@@ -73,7 +72,6 @@ class CogGuess(Cog):
)
if next_stage_id is None:
# TODO Make a nice message
user.completed_event_ids.append(event._id)
await user._set(
@@ -83,12 +81,14 @@ class CogGuess(Cog):
completed_event_ids=user.completed_event_ids,
)
await ctx.respond("Congratulations! You have completed the event!")
await ctx.respond(self.bot._("guess_completed_event", "messages", locale=ctx.locale))
await self.bot.notify_admins(
ctx.guild,
guild,
f"User **{ctx.author.display_name}** ({ctx.author.mention}) has completed the event",
self.bot._("admin_user_completed_event", "messages", locale=ctx.locale).format(
display_name=ctx.author.display_name, mention=ctx.author.mention
),
)
return
@@ -108,7 +108,12 @@ class CogGuess(Cog):
await self.bot.notify_admins(
ctx.guild,
guild,
f"User **{ctx.author.display_name}** ({ctx.author.mention}) has completed the stage {stage.sequence+1} of the event **{event.name}**.",
self.bot._("admin_user_completed_stage", "messages", locale=ctx.locale).format(
display_name=ctx.author.display_name,
mention=ctx.author.mention,
stage_sequence=stage.sequence + 1,
event_name=event.name,
),
)