From 888aea355038d4831d69462bd38aba71da10eef9 Mon Sep 17 00:00:00 2001 From: zv Date: Wed, 29 Apr 2026 20:09:15 +0200 Subject: [PATCH] fix(tempvoice): enhance temporary channel deletion with retry mechanism --- tempvoice.py | 59 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/tempvoice.py b/tempvoice.py index 30b9693..84a4c6c 100644 --- a/tempvoice.py +++ b/tempvoice.py @@ -524,7 +524,12 @@ class TempVoice(commands.Cog): await self._send_creation_message(member, new_channel) - async def _delete_temp_channel_if_empty(self, channel: discord.abc.GuildChannel) -> None: + async def _delete_temp_channel_if_empty( + self, + channel: discord.abc.GuildChannel, + retries: int = 1, + retry_delay: float = 0.75, + ) -> None: if not isinstance(channel, discord.VoiceChannel): return @@ -532,27 +537,37 @@ class TempVoice(commands.Cog): if owner_id is None: return - if channel.members: - return + for attempt in range(retries + 1): + refreshed_channel = channel.guild.get_channel(channel.id) + if not isinstance(refreshed_channel, discord.VoiceChannel): + await self._remove_channel_mappings_bulk(channel.guild, {channel.id}) + return - try: - await channel.delete(reason="TempVoice: deleting empty temporary channel") - except discord.NotFound: - await self._remove_channel_mappings_bulk(channel.guild, {channel.id}) - except discord.Forbidden: - log.warning( - "Missing permissions to delete temporary channel %s (%s).", - channel, - channel.id, - ) - except discord.HTTPException: - log.exception( - "HTTP error while deleting temporary channel %s (%s).", - channel, - channel.id, - ) - else: - await self._remove_channel_mappings_bulk(channel.guild, {channel.id}) + if refreshed_channel.members: + if attempt < retries: + await asyncio.sleep(retry_delay) + continue + return + + try: + await refreshed_channel.delete(reason="TempVoice: deleting empty temporary channel") + except discord.NotFound: + await self._remove_channel_mappings_bulk(channel.guild, {refreshed_channel.id}) + except discord.Forbidden: + log.warning( + "Missing permissions to delete temporary channel %s (%s).", + refreshed_channel, + refreshed_channel.id, + ) + except discord.HTTPException: + log.exception( + "HTTP error while deleting temporary channel %s (%s).", + refreshed_channel, + refreshed_channel.id, + ) + else: + await self._remove_channel_mappings_bulk(channel.guild, {refreshed_channel.id}) + return async def _setlimit_impl(self, ctx: commands.Context, limit: int) -> None: await self._set_locale_context(ctx.guild) @@ -641,7 +656,7 @@ class TempVoice(commands.Cog): await self._handle_join_to_create(member, target_category_id) if before.channel is not None: - await self._delete_temp_channel_if_empty(before.channel) + await self._delete_temp_channel_if_empty(before.channel, retries=2, retry_delay=0.75) except Exception: log.exception( "Unexpected exception in on_voice_state_update for member %s (%s).",