added 10% chance for bot to reply without being mentioned or replied to
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
Deploy / Deploy-Application (push) Has been cancelled

This commit is contained in:
2025-05-18 22:37:26 -04:00
parent ec4a3ac93a
commit 9f81efcf40
3 changed files with 41 additions and 25 deletions

View File

@@ -34,10 +34,16 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
log(`Message "${cleanedMessage}" from ${message.author.tag} in channel/thread ${message.channelId}.`)
// Do not respond if bot talks in the chat
if (message.author.username === message.client.user.username) return
if (message.author.id === clientId) return
// Only respond if message mentions the bot
if (!message.mentions.has(clientId)) return
// Check if message mentions the bot or passes random chance (10%)
const isMentioned = message.mentions.has(clientId)
const isCommand = message.content.startsWith('/')
const randomChance = Math.random() < 0.1 // 10% chance
if (!isMentioned && (isCommand || !randomChance)) return
// Log response trigger
log(isMentioned ? 'Responding to mention' : 'Responding due to random chance')
// Default stream to false
let shouldStream = false
@@ -179,38 +185,41 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
try {
const userSentimentRaw = await redis.get(userSentimentKey)
userSentiment = parseFloat(userSentimentRaw || '0.5')
userSentiment = parseFloat(userSentimentRaw || '0.50')
if (isNaN(userSentiment) || userSentiment < 0 || userSentiment > 1) {
log(`Invalid user sentiment for ${message.author.id}: ${userSentimentRaw}. Using default 0.5.`)
userSentiment = 0.5
await redis.set(userSentimentKey, '0.5').catch((err: Error) => log(`Failed to set default user sentiment: ${err.message}`))
log(`Invalid user sentiment for ${message.author.id}: ${userSentimentRaw}. Using default 0.50.`)
userSentiment = 0.50
await redis.set(userSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default user sentiment: ${err.message}`))
}
} catch (error) {
log(`Failed to get user sentiment from Redis: ${error}`)
userSentiment = 0.5
await redis.set(userSentimentKey, '0.5').catch((err: Error) => log(`Failed to set default user sentiment: ${err.message}`))
userSentiment = 0.50
await redis.set(userSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default user sentiment: ${err.message}`))
}
try {
const botSentimentRaw = await redis.get(botSentimentKey)
botSentiment = parseFloat(botSentimentRaw || '0.5')
botSentiment = parseFloat(botSentimentRaw || '0.50')
if (botSentimentRaw === null) {
log(`Bot sentiment not initialized. Setting to 0.5.`)
botSentiment = 0.5
await redis.set(botSentimentKey, '0.5').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
log(`Bot sentiment not initialized. Setting to 0.50.`)
botSentiment = 0.50
await redis.set(botSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
} else if (isNaN(botSentiment) || botSentiment < 0 || botSentiment > 1) {
log(`Invalid bot sentiment: ${botSentimentRaw}. Using default 0.5.`)
botSentiment = 0.5
await redis.set(botSentimentKey, '0.5').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
log(`Invalid bot sentiment: ${botSentimentRaw}. Using default 0.50.`)
botSentiment = 0.50
await redis.set(botSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
}
} catch (error) {
log(`Failed to get bot sentiment from Redis: ${error}`)
botSentiment = 0.5
await redis.set(botSentimentKey, '0.5').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
botSentiment = 0.50
await redis.set(botSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
}
// Log initial sentiments with two decimals
log(`Initial sentiments - User ${message.author.id}: ${userSentiment.toFixed(2)}, Bot: ${botSentiment.toFixed(2)}`)
// Construct sentiment data for prompt
const sentimentData = `User ${message.author.id} sentiment: ${userSentiment}, Bot sentiment: ${botSentiment}`
const sentimentData = `User ${message.author.id} sentiment: ${userSentiment.toFixed(2)}, Bot sentiment: ${botSentiment.toFixed(2)}`
// Construct prompt with [CHARACTER] and [SENTIMENT]
const prompt = `[CHARACTER]\n${personality}\n[SENTIMENT]\n${sentimentData}\n[USER_INPUT]\n${cleanedMessage}`
@@ -269,8 +278,9 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
log(`Invalid sentiment value for ${op.key}: ${op.value}. Skipping.`)
continue
}
await redis.set(op.key, value)
log(`Set ${op.key} to ${value}`)
// Store with two decimal places
await redis.set(op.key, value.toFixed(2))
log(`Set ${op.key} to ${value.toFixed(2)}`)
} else if (op.action === 'get' && op.key) {
const value = await redis.get(op.key)
log(`Got ${op.key}: ${value}`)
@@ -283,6 +293,11 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
}
}
// Log updated sentiments with two decimals
if (jsonResponse.metadata) {
log(`Updated sentiments - Self: ${(jsonResponse.metadata.self_sentiment || 0).toFixed(2)}, User ${message.author.id}: ${(jsonResponse.metadata.user_sentiment[message.author.id] || 0).toFixed(2)}`)
}
// Send reply to Discord
const reply = jsonResponse.reply || 'Sorry, I didnt get that. Can you try again?'
await message.reply(reply)