Added bot-to-bot replies
Some checks failed
Builds / Discord-Node-Build (push) Has been cancelled
Builds / Discord-Ollama-Container-Build (push) Has been cancelled
Coverage / Discord-Node-Coverage (push) Has been cancelled

This commit is contained in:
2025-05-20 14:10:11 -04:00
parent 3ee8347268
commit efa8b84d75

View File

@@ -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 bots own history
const historyFile = `${message.channelId}-${client.user.username}.json`
// Default stream to false
let shouldStream = false