diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 7fbce8b..0ff8c13 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -33,18 +33,52 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client let cleanedMessage = clean(message.content, clientId) log(`Message "${cleanedMessage}" from ${message.author.tag} in channel/thread ${message.channelId}.`) - // Ignore messages from all bots - if (message.author.bot) return - // Check if message mentions the bot or passes random chance (30%) + const isFromBot = message.author.bot && message.author.id !== clientId; const isMentioned = message.mentions.has(clientId) const isCommand = message.content.startsWith('/') - const randomChance = Math.random() < 0.30 // 30% chance - if (!isMentioned && (isCommand || !randomChance)) return + + if (isFromBot) { + // Check interaction key to prevent rapid back-and-forth + const otherBotId = message.author.id + const interactionKey = `bot_interaction:${[clientId, otherBotId].sort().join(':')}` + const interactionExists = await redis.exists(interactionKey) + if (interactionExists) { + log('Interaction cooldown active, not responding') + return + } + + // Determine probability + let respondProbability = 0 + if (isMentioned) { + respondProbability = 0.9 // 90% chance if mentioned + } else { + respondProbability = 0.2 // 20% chance if not mentioned + } + + const shouldRespond = Math.random() < respondProbability + if (!shouldRespond) return + + // Set interaction key with 60s expiration + await redis.set(interactionKey, '1', 'EX', 60) + } else if (!message.author.bot) { + // Human message + const randomChance = Math.random() < 0.30 + if (!isMentioned && (isCommand || !randomChance)) return + } else { + // Message from self: ignore + return + } // Log response trigger log(isMentioned ? 'Responding to mention' : 'Responding due to random chance') + // Log response trigger + log(isFromBot ? 'Responding to bot message' : (isMentioned ? 'Responding to mention' : 'Responding due to random chance')) + + // Load and process bot’s own history + const historyFile = `${message.channelId}-${client.user.username}.json` + // Default stream to false let shouldStream = false