fix: 채널 중복 완전 해결 - Discord 캐시 대신 자체 dict로 이름 검색

This commit is contained in:
2026-03-07 12:02:03 +09:00
parent b2622e9052
commit 51ece61a61

42
bot.py
View File

@@ -195,7 +195,7 @@ class GravityBot(commands.Bot):
async def _ensure_channel( async def _ensure_channel(
self, conversation_id: str, project_name: str self, conversation_id: str, project_name: str
) -> discord.TextChannel: ) -> 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 # Fast path: already mapped
if conversation_id in self.session_channels: if conversation_id in self.session_channels:
return self.session_channels[conversation_id] return self.session_channels[conversation_id]
@@ -208,24 +208,16 @@ class GravityBot(commands.Bot):
channel_name = f"{Config.CHANNEL_PREFIX}-{project_name}" channel_name = f"{Config.CHANNEL_PREFIX}-{project_name}"
target_name = channel_name.lower().replace(" ", "-") target_name = channel_name.lower().replace(" ", "-")
# Fetch FRESH channel list from Discord API (not cached) # Check OWN dict for a channel with the same NAME
if self.session_category: # (different conv IDs can share one channel)
category = await self.guild.fetch_channel(self.session_category.id) for cid, ch in self.session_channels.items():
for ch in category.text_channels: if ch.name == target_name:
# Match by NAME (handles different conv IDs → same project) self.session_channels[conversation_id] = ch
if ch.name == target_name or (ch.topic and conversation_id in ch.topic): self.session_names[conversation_id] = project_name
self.session_channels[conversation_id] = ch logger.info(f"Reusing channel #{ch.name} for {conversation_id[:8]}")
self.session_names[conversation_id] = project_name return ch
# 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
# Create new channel (only if truly no match found) # Create new channel (truly first time)
try: try:
channel = await self.guild.create_text_channel( channel = await self.guild.create_text_channel(
name=channel_name, name=channel_name,
@@ -280,10 +272,16 @@ class GravityBot(commands.Bot):
if not channel: if not channel:
return return
if event.file_name == "task.md": try:
await self._send_task_update(channel, event) if event.file_name == "task.md":
else: await self._send_task_update(channel, event)
await self._send_artifact_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 ───────────────────────────────────────────── # ─── Message Senders ─────────────────────────────────────────────