diff --git a/services/ask-ai-bot/events/messageCreate.ts b/services/ask-ai-bot/events/messageCreate.ts index ef8017c3f3..b60f9fac43 100644 --- a/services/ask-ai-bot/events/messageCreate.ts +++ b/services/ask-ai-bot/events/messageCreate.ts @@ -9,6 +9,48 @@ import { answerQuestion } from "../utils/ai"; import { buildServerContext } from "../utils/discord/server-context"; import { logger } from "../utils/logger"; +async function shouldSendFollowUpHint( + message: OmitPartialGroupDMChannel>, +): Promise { + try { + const thread = message.channel; + if (!thread.isThread()) return false; + + const botId = message.client.user?.id || ""; + const starterMessage = await thread.fetchStarterMessage().catch(() => null); + + const messages = await thread.messages.fetch({ limit: 20 }); + + let hasOtherHumanMessages = false; + let botWasPingedOrRepliedTo = false; + + for (const msg of messages.values()) { + if (msg.author.bot) continue; + if (starterMessage && msg.id === starterMessage.id) continue; + if (msg.id === message.id) continue; + + hasOtherHumanMessages = true; + + if (msg.mentions.has(botId)) { + botWasPingedOrRepliedTo = true; + } + + if (msg.reference?.messageId && !botWasPingedOrRepliedTo) { + const referenced = await thread.messages + .fetch(msg.reference.messageId) + .catch(() => null); + if (referenced?.author.bot) { + botWasPingedOrRepliedTo = true; + } + } + } + + return !hasOtherHumanMessages && !botWasPingedOrRepliedTo; + } catch { + return false; + } +} + export default { event: Events.MessageCreate, handler: async ( @@ -56,6 +98,13 @@ export default { logger.verbose( `Ignoring thread message from ${message.author.username} (not mentioned or replied to)`, ); + + if (await shouldSendFollowUpHint(message)) { + await message.channel.send( + "To ask me a follow-up, please **reply to one of my messages** or **@mention me**. I won't see messages sent otherwise.", + ); + } + return; }