limit bot-to-bot chat
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-22 09:36:20 -04:00
parent d3fd88da04
commit 23a860a905
2 changed files with 52 additions and 23 deletions

View File

@@ -0,0 +1,33 @@
services:
discord2:
build: ./
container_name: discord2
restart: always
image: gitea.matrixwide.com/alex/discord-aidolls:0.1.1
environment:
CLIENT_TOKEN: ${CLIENT_TOKEN}
OLLAMA_IP: ${OLLAMA_IP}
OLLAMA_PORT: ${OLLAMA_PORT}
REDIS_IP: ${REDIS_IP}
REDIS_PORT: ${REDIS_PORT}
MODEL: ${MODEL}
networks:
redis_discord-net:
ipv4_address: ${DISCORD_IP}
volumes:
- ./discord_data2:/app/data
- ./src:/app/src
healthcheck:
test: ["CMD", "redis-cli", "-h", "${REDIS_IP}", "-p", "${REDIS_PORT}", "PING"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
networks:
redis_discord-net:
external: true
name: redis_discord-net
volumes:
discord_data2:

View File

@@ -61,18 +61,18 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
}
}
// Check if last response was to a bot and require user message
const lastResponseToBotKey = `bot:${clientId}:last_response_to_bot`
// Check if bot has recently replied to a user for bot-to-bot eligibility
const repliedToUserKey = `bot:${clientId}:replied_to_user`
let shouldRespond = true
if (isBotMessage) {
try {
const lastResponseToBot = await redis.get(lastResponseToBotKey)
if (lastResponseToBot === 'true') {
log(`Skipping bot message: Last response was to a bot. Waiting for user message.`)
const hasRepliedToUser = await redis.get(repliedToUserKey)
if (hasRepliedToUser !== 'true') {
log(`Skipping bot message: Bot has not replied to a user recently.`)
return
}
} catch (error) {
log(`Failed to check last response to bot: ${error}`)
log(`Failed to check replied_to_user status: ${error}`)
}
}
@@ -97,16 +97,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
if (!shouldRespond) return
// Reset last_response_to_bot flag if this is a user message
if (!isBotMessage) {
try {
await redis.set(lastResponseToBotKey, 'false')
log(`Reset last_response_to_bot flag for bot ${clientId}`)
} catch (error) {
log(`Failed to reset last_response_to_bot flag: ${error}`)
}
}
// Log response trigger
log(isMentioned ? 'Responding to mention' : isBotMessage ? 'Responding to bot message' : 'Responding due to random chance')
@@ -394,16 +384,22 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
// Send reply to Discord and mark as bot response
const reply = jsonResponse.reply || 'Sorry, I didnt get that. Can you try again?'
const replyMessage = await message.reply(reply)
if (isBotMessage) {
try {
// Mark message as bot response if replying to a bot
if (isBotMessage) {
await redis.set(`message:${replyMessage.id}:is_bot_response`, 'true', { EX: 3600 }) // 1 hour TTL
log(`Marked message ${replyMessage.id} as bot response`)
// Set flag indicating last response was to a bot
await redis.set(lastResponseToBotKey, 'true')
log(`Set last_response_to_bot flag for bot ${clientId}`)
} catch (error) {
log(`Failed to mark message as bot response or set last_response_to_bot flag: ${error}`)
// Reset replied_to_user flag after bot-to-bot reply
await redis.set(repliedToUserKey, 'false')
log(`Reset replied_to_user flag for bot ${clientId} after bot-to-bot reply`)
}
// Set replied_to_user flag if replying to a user
if (!isBotMessage) {
await redis.set(repliedToUserKey, 'true')
log(`Set replied_to_user flag for bot ${clientId} after user reply`)
}
} catch (error) {
log(`Failed to set message flags: ${error}`)
}
// Update message history in Redis