From e45a56835a48f26ce691e94338662e63f9ee642a Mon Sep 17 00:00:00 2001 From: profitroll Date: Sat, 26 Apr 2025 20:01:52 +0200 Subject: [PATCH] Fixed messages about not created channels --- classes/pycord_bot.py | 25 +++++++++++++++++---- classes/pycord_user.py | 6 ++++-- cogs/cog_register.py | 49 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/classes/pycord_bot.py b/classes/pycord_bot.py index 592521c..29b3a2e 100644 --- a/classes/pycord_bot.py +++ b/classes/pycord_bot.py @@ -98,10 +98,27 @@ class PycordBot(LibPycordBot): await user._set(self.cache, current_event_id=event._id, current_stage_id=first_stage._id) # Create a channel for each participant - await user.setup_event_channel(self, guild, pycord_guild, event, cache=self.cache) + user_channel: TextChannel | None = await user.setup_event_channel( + self, guild, pycord_guild, event, cache=self.cache + ) - # Send a notification about event start - user_channel: TextChannel = guild.get_channel(user.event_channels[str(event._id)]) + if user_channel is None: + logger.error( + "Event channel was not created for user %s from guild %s and event %s after registration.", + user.id, + guild.id, + event._id, + ) + + discord_user: User = self.get_user(user.id) + + await self.notify_admins( + guild, + pycord_guild, + f"Event channel could not be created for user **{discord_user.display_name}** ({discord_user.mention}) and event **{event.name}**.", + ) + + continue thumbnail: File | None = ( None @@ -109,8 +126,8 @@ class PycordBot(LibPycordBot): else File(Path(f"data/{event.thumbnail['id']}"), event.thumbnail["filename"]) ) + # Send a notification about event start # TODO Make a nice message - # TODO Also send a thumbnail, event info and short explanation on how to play await user_channel.send( f"Event **{event.name}** is starting!\n\nUse slash command `/guess` to suggest your answers to each event stage.", file=thumbnail, diff --git a/classes/pycord_user.py b/classes/pycord_user.py index 011b985..fa1b5dd 100644 --- a/classes/pycord_user.py +++ b/classes/pycord_user.py @@ -270,9 +270,9 @@ class PycordUser: pycord_guild: "PycordGuild", pycord_event: "PycordEvent", cache: Optional[Cache] = None, - ): + ) -> TextChannel | None: if str(pycord_event._id) in self.event_channels.keys(): - return + return None discord_member: Member | None = guild.get_member(self.id) discord_category: GuildChannel | None = bot.get_channel(pycord_guild.category_id) @@ -316,6 +316,8 @@ class PycordUser: await self.set_event_channel(pycord_event._id, channel.id, cache=cache) + return channel + async def lock_event_channel( self, guild: Guild, diff --git a/cogs/cog_register.py b/cogs/cog_register.py index fc9a0a0..1934077 100644 --- a/cogs/cog_register.py +++ b/cogs/cog_register.py @@ -1,13 +1,18 @@ from datetime import datetime +from logging import Logger +from pathlib import Path +from typing import List from zoneinfo import ZoneInfo from bson.errors import InvalidId -from discord import ApplicationContext, Cog, option, slash_command +from discord import ApplicationContext, Cog, option, slash_command, TextChannel, File from discord.utils import basic_autocomplete -from classes import PycordEvent, PycordGuild, PycordUser +from classes import PycordEvent, PycordGuild, PycordUser, PycordEventStage from classes.pycord_bot import PycordBot -from modules.utils import autocomplete_active_events, get_unix_timestamp +from modules.utils import autocomplete_active_events, get_unix_timestamp, get_logger + +logger: Logger = get_logger(__name__) class CogRegister(Cog): @@ -60,7 +65,43 @@ class CogRegister(Cog): ) if pycord_event.starts.replace(tzinfo=ZoneInfo("UTC")) < datetime.now(tz=ZoneInfo("UTC")): - await user.setup_event_channel(self.bot, ctx.guild, guild, pycord_event, cache=self.bot.cache) + user_channel: TextChannel = await user.setup_event_channel( + self.bot, ctx.guild, guild, pycord_event, cache=self.bot.cache + ) + + if user_channel is None: + logger.error( + "Event channel was not created for user %s from guild %s and event %s after registration.", + ctx.author.id, + guild.id, + pycord_event._id, + ) + + 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}**.", + ) + + return + + thumbnail: File | None = ( + None + if pycord_event.thumbnail is None + 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.", + file=thumbnail, + ) + + first_stage: PycordEventStage = await self.bot.find_event_stage(pycord_event.stage_ids[0]) + + first_stage_files: List[File] | None = first_stage.get_media_files() + + await user_channel.send(f"First stage...\n\n{first_stage.question}", files=first_stage_files) def setup(bot: PycordBot) -> None: