From 6054a0ace4907101a5bad7ee53eb100f0ad061b0 Mon Sep 17 00:00:00 2001 From: Justin Kristensen Date: Sat, 19 Oct 2024 15:35:07 -0400 Subject: [PATCH] fix: Ignore Non-Messageable Channels (#879) --- .../bot/services/emote_board_service.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/ClemBot.Bot/bot/services/emote_board_service.py b/ClemBot.Bot/bot/services/emote_board_service.py index 554525fc..c233cc40 100644 --- a/ClemBot.Bot/bot/services/emote_board_service.py +++ b/ClemBot.Bot/bot/services/emote_board_service.py @@ -28,6 +28,13 @@ "***GOD-TIER***", ] +VALID_CHANNEL_TYPES = ( + discord.Thread, + discord.TextChannel, + discord.VoiceChannel, + discord.StageChannel, +) + class EmoteBoardService(BaseService): """ @@ -58,7 +65,10 @@ async def on_reaction_add(self, event: RawReactionActionEvent) -> None: assert guild is not None channel = guild.get_channel_or_thread(event.channel_id) - assert channel is not None and isinstance(channel, discord.abc.Messageable) + assert channel is not None + + if not isinstance(channel, VALID_CHANNEL_TYPES): + return message = await channel.fetch_message(event.message_id) @@ -116,7 +126,10 @@ async def on_message_edit(self, event: RawMessageUpdateEvent) -> None: assert guild is not None channel = guild.get_channel_or_thread(event.channel_id) - assert channel is not None and isinstance(channel, discord.abc.Messageable) + assert channel is not None + + if not isinstance(channel, VALID_CHANNEL_TYPES): + return # Attempt to fetch the message, ignore if we do not have permissions. message: discord.Message @@ -153,7 +166,7 @@ async def on_message_edit(self, event: RawMessageUpdateEvent) -> None: if not (channel := guild.get_channel_or_thread(channel_id)): continue - assert isinstance(channel, discord.abc.Messageable) + assert isinstance(channel, VALID_CHANNEL_TYPES) embed_msg = await channel.fetch_message(message_id) embed = await self._as_embed( message, @@ -181,7 +194,7 @@ async def on_message_delete(self, event: RawMessageDeleteEvent) -> None: if not (channel := guild.get_channel_or_thread(channel_id)): continue - assert isinstance(channel, discord.abc.Messageable) + assert isinstance(channel, VALID_CHANNEL_TYPES) embed_msg = await channel.fetch_message(message_id) await embed_msg.delete() except NotFound: # Skips over the item if fetch_message() raises `NotFound` @@ -217,7 +230,7 @@ async def _create_post( for channel_id in board.channels: if not (channel := guild.get_channel_or_thread(channel_id)): continue - assert isinstance(channel, discord.abc.Messageable) + assert isinstance(channel, VALID_CHANNEL_TYPES) message = await channel.send(embed=embed) post.channel_message_ids[channel_id] = message.id @@ -252,7 +265,7 @@ async def _update_post( if not (channel := guild.get_channel_or_thread(channel_id)): continue - if not isinstance(channel, discord.abc.Messageable): + if not isinstance(channel, VALID_CHANNEL_TYPES): continue embed_msg = await channel.fetch_message(message_id)