From 51ece61a610c7226a76ec91bbb3a7db0f8b5b3e1 Mon Sep 17 00:00:00 2001 From: CD Date: Sat, 7 Mar 2026 12:02:03 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B1=84=EB=84=90=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=20=EC=99=84=EC=A0=84=20=ED=95=B4=EA=B2=B0=20-=20Discord=20?= =?UTF-8?q?=EC=BA=90=EC=8B=9C=20=EB=8C=80=EC=8B=A0=20=EC=9E=90=EC=B2=B4=20?= =?UTF-8?q?dict=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20=EA=B2=80=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/bot.py b/bot.py index a2ba2f7..fa6109f 100644 --- a/bot.py +++ b/bot.py @@ -195,7 +195,7 @@ class GravityBot(commands.Bot): async def _ensure_channel( self, conversation_id: str, project_name: str ) -> discord.TextChannel: - """Get or create a channel. Uses GLOBAL lock and NAME-based lookup.""" + """Get or create a channel. Uses GLOBAL lock + OWN dict lookup (no cache issues).""" # Fast path: already mapped if conversation_id in self.session_channels: return self.session_channels[conversation_id] @@ -208,24 +208,16 @@ class GravityBot(commands.Bot): channel_name = f"{Config.CHANNEL_PREFIX}-{project_name}" target_name = channel_name.lower().replace(" ", "-") - # Fetch FRESH channel list from Discord API (not cached) - if self.session_category: - category = await self.guild.fetch_channel(self.session_category.id) - for ch in category.text_channels: - # Match by NAME (handles different conv IDs → same project) - if ch.name == target_name or (ch.topic and conversation_id in ch.topic): - self.session_channels[conversation_id] = ch - self.session_names[conversation_id] = project_name - # Rename from closed- if needed - if ch.name.startswith("closed-"): - try: - await ch.edit(name=channel_name) - except discord.errors.Forbidden: - pass - logger.info(f"Reusing existing channel #{ch.name}") - return ch + # Check OWN dict for a channel with the same NAME + # (different conv IDs can share one channel) + for cid, ch in self.session_channels.items(): + if ch.name == target_name: + self.session_channels[conversation_id] = ch + self.session_names[conversation_id] = project_name + logger.info(f"Reusing channel #{ch.name} for {conversation_id[:8]}") + return ch - # Create new channel (only if truly no match found) + # Create new channel (truly first time) try: channel = await self.guild.create_text_channel( name=channel_name, @@ -280,10 +272,16 @@ class GravityBot(commands.Bot): if not channel: return - if event.file_name == "task.md": - await self._send_task_update(channel, event) - else: - await self._send_artifact_update(channel, event) + try: + if event.file_name == "task.md": + await self._send_task_update(channel, event) + else: + await self._send_artifact_update(channel, event) + except discord.NotFound: + # Channel was deleted while we held a reference + self.session_channels.pop(event.conversation_id, None) + self.session_status_messages.pop(event.conversation_id, None) + logger.warning(f"Channel deleted, cleared: {event.conversation_id[:8]}") # ─── Message Senders ─────────────────────────────────────────────