From af14e5fbc75c341cc131344d82efe43f2f887dd0 Mon Sep 17 00:00:00 2001 From: CD Date: Sat, 7 Mar 2026 15:10:43 +0900 Subject: [PATCH] fix: _get_channel fetches existing channels before creating (prevents duplicates) --- bot.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index f911f3a..27568cd 100644 --- a/bot.py +++ b/bot.py @@ -199,11 +199,27 @@ class GravityBot(commands.Bot): return self.project_channels[project_name] async with self._channel_lock: - # Double-check + # Double-check after lock if project_name in self.project_channels: return self.project_channels[project_name] channel_name = self._make_channel_name(project_name) + + # Search existing channels FIRST (prevents duplicates) + try: + all_channels = await self.guild.fetch_channels() + for ch in all_channels: + if (isinstance(ch, discord.TextChannel) + and ch.name == channel_name + and ch.category_id == self.session_category.id): + self.project_channels[project_name] = ch + self.channel_to_project[ch.id] = project_name + logger.info(f"Found existing channel: #{channel_name}") + return ch + except Exception as e: + logger.warning(f"fetch_channels failed: {e}") + + # No existing channel — create new try: ch = await self.guild.create_text_channel( name=channel_name,