From 4e8d9f2369e150c5f56e19efe2d62e72fd44bc39 Mon Sep 17 00:00:00 2001 From: chaodu-agent Date: Sun, 19 Apr 2026 13:12:06 +0000 Subject: [PATCH] perf(discord): eager multibot detection from msg.author MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of fetching thread history to detect other bots on every unmentioned message, detect multi-bot threads eagerly from msg.author: when a bot message arrives in a thread, cache multibot_threads immediately — zero API calls needed. This eliminates per-message fetches in MultibotMentions mode for single-bot threads (which would never find other bots but fetched every time to check). - Add early detection before gating: msg.author.bot → cache multibot - Simplify bot_participated_in_thread: cached_involved → return immediately with cached_multibot, no MultibotMentions special-casing Builds on #464 --- src/discord.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/discord.rs b/src/discord.rs index 2dbdc0a..84450a7 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -148,12 +148,10 @@ impl Handler { }; // Both cached → skip fetch entirely - if cached_involved && cached_multibot { - return (true, true); - } - // Involved cached + not MultibotMentions mode → don't need other_bot info - if cached_involved && self.allow_user_messages != AllowUsers::MultibotMentions { - return (true, false); + // With early detection from msg.author, multibot_threads is populated + // eagerly — no need to fetch just to check for other bots. + if cached_involved { + return (true, cached_multibot); } // Fetch recent messages @@ -321,6 +319,14 @@ impl EventHandler for Handler { return; } + // Early multibot detection: if the current message is from another bot, + // this thread is multi-bot. Cache it now — no fetch needed. + if in_thread && msg.author.bot && msg.author.id != bot_id { + let key = msg.channel_id.to_string(); + let mut cache = self.multibot_threads.lock().await; + cache.entry(key).or_insert_with(tokio::time::Instant::now); + } + // User message gating (mirrors Slack's AllowUsers logic). // Mentions: always require @mention, even in bot's own threads. // Involved (default): skip @mention if the bot owns the thread