fix bot reply loop
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 12:16:00 -04:00
parent b2edffc98c
commit 194fc57545
3 changed files with 20 additions and 68 deletions

View File

@@ -33,8 +33,8 @@ 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}.`)
// Do not respond if bot talks in the chat
if (message.author.id === clientId) return
// Ignore messages from all bots
if (message.author.bot) return
// Check if message mentions the bot or passes random chance (30%)
const isMentioned = message.mentions.has(clientId)
@@ -186,7 +186,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
try {
const userSentimentRaw = await redis.get(userSentimentKey)
userSentiment = parseFloat(userSentimentRaw || '0.50')
if (isNaN(userSentiment) || userSentiment < 0 || userSentiment > 1) {
if (isNaN(userSentiment) || userSentiment < 0 || userSentiment > 1) {
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}`))
@@ -200,20 +200,19 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
try {
const botSentimentRaw = await redis.get(botSentimentKey)
botSentiment = parseFloat(botSentimentRaw || '0.50')
if (botSentimentRaw === null) {
log(`Bot sentiment not initialized. Setting to 0.50.`)
if (isNaN(botSentiment) || botSentiment < 0 || botSentiment > 1) {
log(`Invalid bot sentiment for ${botSentimentKey}: ${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}`))
} else if (isNaN(botSentiment) || botSentiment < 0 || botSentiment > 1) {
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}`))
await redis.set(botSentimentKey, '0.50')
}
} catch (error) {
log(`Failed to get bot sentiment from Redis: ${error}`)
botSentiment = 0.50
await redis.set(botSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
await redis.set(botSentimentKey, '0.50')
}
// Construct sentiment data with bot ID
const sentimentData = `User ${message.author.id} sentiment: ${userSentiment.toFixed(2)}, Bot ${clientId} sentiment: ${botSentiment.toFixed(2)}`
// Log initial sentiments with two decimals
log(`Initial sentiments - User ${message.author.id}: ${userSentiment.toFixed(2)}, Bot: ${botSentiment.toFixed(2)}`)
@@ -268,22 +267,20 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
}
// Execute redis_ops
if (jsonResponse.metadata?.redis_ops) {
for (const op of jsonResponse.metadata.redis_ops) {
try {
if (op.action === 'set' && op.key && op.value !== undefined) {
// Validate sentiment value
const key = op.key.replace('<bot_id>', clientId)
if (op.action === 'set' && op.value !== undefined) {
const value = parseFloat(op.value.toString())
if (isNaN(value) || value < 0 || value > 1) {
log(`Invalid sentiment value for ${op.key}: ${op.value}. Skipping.`)
log(`Invalid sentiment value for ${key}: ${op.value}. Skipping.`)
continue
}
// Store with two decimal places
await redis.set(op.key, value.toFixed(2))
log(`Set ${op.key} to ${value.toFixed(2)}`)
await redis.set(key, value.toFixed(2))
log(`Set ${key} to ${value.toFixed(2)}`)
} else if (op.action === 'get' && op.key) {
const value = await redis.get(op.key)
log(`Got ${op.key}: ${value}`)
const value = await redis.get(key)
log(`Got ${key}: ${value}`)
} else {
log(`Invalid redis_op: ${JSON.stringify(op)}. Skipping.`)
}