multi-bot replies
This commit is contained in:
@@ -33,17 +33,31 @@ 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
|
||||
|
||||
// Check if message mentions the bot or passes random chance (10%)
|
||||
// Check if message is from a bot (not self), mentions the bot, or passes random chance
|
||||
const isBotMessage = message.author.bot && message.author.id !== clientId
|
||||
const isMentioned = message.mentions.has(clientId)
|
||||
const isCommand = message.content.startsWith('/')
|
||||
const randomChance = Math.random() < 0.1 // 10% chance
|
||||
if (!isMentioned && (isCommand || !randomChance)) return
|
||||
const randomChance = Math.random() < 0.1 // 10% chance for non-directed or bot messages
|
||||
if (!isMentioned && !isBotMessage && (isCommand || !randomChance)) return
|
||||
|
||||
// Check cooldown for bot-to-bot responses
|
||||
const botResponseCooldownKey = `bot:${clientId}:last_bot_response`
|
||||
const cooldownPeriod = 60 // 60 seconds cooldown
|
||||
if (isBotMessage) {
|
||||
try {
|
||||
const lastResponseTime = await redis.get(botResponseCooldownKey)
|
||||
const currentTime = Math.floor(Date.now() / 1000)
|
||||
if (lastResponseTime && (currentTime - parseInt(lastResponseTime)) < cooldownPeriod) {
|
||||
log(`Bot ${clientId} is in cooldown for bot-to-bot response. Skipping.`)
|
||||
return
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Failed to check bot response cooldown: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Log response trigger
|
||||
log(isMentioned ? 'Responding to mention' : 'Responding due to random chance')
|
||||
log(isMentioned ? 'Responding to mention' : isBotMessage ? 'Responding to bot message' : 'Responding due to random chance')
|
||||
|
||||
// Default stream to false
|
||||
let shouldStream = false
|
||||
@@ -177,24 +191,41 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
personality = 'You are a friendly and helpful AI assistant.'
|
||||
}
|
||||
|
||||
// Get user and bot sentiment from Redis
|
||||
// Get user or bot sentiment from Redis
|
||||
const userSentimentKey = `user:${message.author.id}:sentiment`
|
||||
const botSentimentKey = `bot:self_sentiment`
|
||||
let userSentiment: number
|
||||
let botSentiment: number
|
||||
|
||||
try {
|
||||
const userSentimentRaw = await redis.get(userSentimentKey)
|
||||
userSentiment = parseFloat(userSentimentRaw || '0.50')
|
||||
if (isNaN(userSentiment) || userSentiment < 0 || userSentiment > 1) {
|
||||
log(`Invalid user sentiment for ${message.author.id}: ${userSentimentRaw}. Using default 0.50.`)
|
||||
// Handle sentiment for bot or user messages
|
||||
if (isBotMessage) {
|
||||
try {
|
||||
const botSentimentRaw = await redis.get(userSentimentKey)
|
||||
userSentiment = parseFloat(botSentimentRaw || '0.50')
|
||||
if (isNaN(userSentiment) || userSentiment < 0 || userSentiment > 1) {
|
||||
log(`Invalid bot sentiment for ${message.author.id}: ${botSentimentRaw}. Using default 0.50.`)
|
||||
userSentiment = 0.50
|
||||
await redis.set(userSentimentKey, '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}`)
|
||||
userSentiment = 0.50
|
||||
await redis.set(userSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default bot sentiment: ${err.message}`))
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const userSentimentRaw = await redis.get(userSentimentKey)
|
||||
userSentiment = parseFloat(userSentimentRaw || '0.50')
|
||||
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}`))
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Failed to get user sentiment from Redis: ${error}`)
|
||||
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.50
|
||||
await redis.set(userSentimentKey, '0.50').catch((err: Error) => log(`Failed to set default user sentiment: ${err.message}`))
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -221,8 +252,13 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
// Construct sentiment data for prompt
|
||||
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}`
|
||||
// Add context for bot-to-bot interaction
|
||||
const messageContext = isBotMessage
|
||||
? `Responding to another bot (${message.author.tag})`
|
||||
: `Responding to user ${message.author.tag}`
|
||||
|
||||
// Construct prompt with [CHARACTER], [SENTIMENT], and [CONTEXT]
|
||||
const prompt = `[CHARACTER]\n${personality}\n[SENTIMENT]\n${sentimentData}\n[CONTEXT]\n${messageContext}\n[USER_INPUT]\n${cleanedMessage}`
|
||||
|
||||
// Set up message history queue
|
||||
msgHist.setQueue(chatMessages)
|
||||
@@ -312,6 +348,17 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
|
||||
// Save updated history
|
||||
openChannelInfo(message.channelId, message.channel as TextChannel, message.author.tag, msgHist.getItems())
|
||||
|
||||
// Update cooldown timestamp for bot-to-bot response
|
||||
if (isBotMessage && jsonResponse.status === 'success') {
|
||||
try {
|
||||
const currentTime = Math.floor(Date.now() / 1000)
|
||||
await redis.set(botResponseCooldownKey, currentTime.toString(), { EX: cooldownPeriod })
|
||||
log(`Set bot ${clientId} cooldown until ${currentTime + cooldownPeriod}`)
|
||||
} catch (error) {
|
||||
log(`Failed to set bot response cooldown: ${error}`)
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
msgHist.pop()
|
||||
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
||||
|
||||
Reference in New Issue
Block a user