diff --git a/agon/cogs/commands/set.py b/agon/cogs/commands/set.py index dc015c0..b43c589 100644 --- a/agon/cogs/commands/set.py +++ b/agon/cogs/commands/set.py @@ -251,6 +251,10 @@ class SetCog(commands.Cog, name="Set command."): channel (nextcord.abc.GuildChannel, optional): Channel to register. Is a SlashOption. Limited to text channels, Defaults to None. """ + if interaction.guild is None or interaction.channel is None: + agon_logger.warning("Set command was called with an interaction that had no guild or channel.") + return + channel_id = interaction.channel.id if not channel else channel.id with Session(engine) as session: @@ -262,11 +266,14 @@ class SetCog(commands.Cog, name="Set command."): if (group := session.scalar(stmt)) is None: await interaction.response.send_message("Group or Category was not found.", ephemeral=True) return + old_channel_id = group.channel_id group.channel_id = channel_id session.commit() - # TODO(imcompiling): delete the message and call send_highscore_message to send a new one. Bug needs fixed - # first. - # 082 + + old_channel = interaction.guild.get_channel(old_channel_id) + if old_channel and isinstance(old_channel, nextcord.TextChannel | nextcord.Thread) and group.message_id: + await old_channel.get_partial_message(group.message_id).delete() + await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @group.subcommand( diff --git a/agon/utils.py b/agon/utils.py index 91208f9..d30868e 100644 --- a/agon/utils.py +++ b/agon/utils.py @@ -358,10 +358,14 @@ async def send_highscore_message(session: Session, guild: nextcord.Guild, group: group.message_id = message.id session.commit() else: - # TODO(imcompiling): check what happens when message is deleted. - # https://gitea.cheezers.org/imcompiling/agon/issues/83 - message: nextcord.Message = await channel.fetch_message(group.message_id) - await message.edit(content=highscore_string) + try: + message: nextcord.Message = await channel.fetch_message(group.message_id) + await message.edit(content=highscore_string) + except nextcord.NotFound: + # Message was deleted on discord's side. + message: nextcord.Message = await channel.send(highscore_string) + group.message_id = message.id + session.commit() return