From ab1e4054a79d5060221ec4f092c438dd617e5de3 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 9 Aug 2024 13:59:10 +0300 Subject: [PATCH] feat: variable chatter chance/brain save rate --- src/commands/chat.command.ts | 6 +++--- src/commands/see.command.ts | 3 ++- src/core/megahal.ts | 32 +++++++++++++++++++++++++------- src/core/message_handler.ts | 4 ++-- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/commands/chat.command.ts b/src/commands/chat.command.ts index 44f4ff1..9394a7c 100644 --- a/src/commands/chat.command.ts +++ b/src/commands/chat.command.ts @@ -1,7 +1,7 @@ import { command } from '@/core/commands' import { logger } from '@/core/logger' import { - CHATTER_REPLY_CHANCE, + chatterChance, getMegahalBrainSize, isMuted, megahal, @@ -16,8 +16,8 @@ export default command({ command: 'chat', aliases: ['c'], description: - `Manage chatting with Venom. Venom will have a ${CHATTER_REPLY_CHANCE * 100}% (around every ` + - `${Math.round(1 / CHATTER_REPLY_CHANCE)} messages) chance to reply to any incoming message on ` + + `Manage chatting with Venom. Venom will have a ${chatterChance * 100}% (around every ` + + `${Math.round(1 / chatterChance)} messages) chance to reply to any incoming message on ` + `a channel unless muted. To change this value, contact the one of the server staff.\n` + 'Muting completely disables chatting, to avoid bugs relating to infinite triggers, or any other reason.', examples: [ diff --git a/src/commands/see.command.ts b/src/commands/see.command.ts index fcd6ae7..c85c342 100644 --- a/src/commands/see.command.ts +++ b/src/commands/see.command.ts @@ -6,7 +6,8 @@ export default command({ description: 'See your username and ID', examples: ['`!s`'], async execute(message) { - return message.author.send( + message.reply("I've sent you a DM with your info.") + message.author.send( [ '**Server info:**', `ID: ${message.guild!.id}`, diff --git a/src/core/megahal.ts b/src/core/megahal.ts index 99e02f1..5b6617a 100644 --- a/src/core/megahal.ts +++ b/src/core/megahal.ts @@ -8,15 +8,25 @@ import { formatBytes } from '@/utils/string_utils' import { isWhitelisted } from '@/lib/whitelist' import { replaceUserMentions } from '@/utils/discord_utils' import '@/lib/venom-personality' +import { getSetting } from '@/lib/settings' + +//-------------------------------------------------------------------------------- +// Consts +//-------------------------------------------------------------------------------- + +const BRAIN_FILE = path.resolve(process.cwd(), 'data', 'brain.dat') +const DEFAULT_SAVE_RATE = 20 +const DEFAULT_CHATTER_CHANCE = 0.02 + +//-------------------------------------------------------------------------------- +// Bot settings +//-------------------------------------------------------------------------------- let muted = false -const BRAIN_FILE = path.resolve(process.cwd(), 'data', 'brain.dat') -// every 20 messages -const SAVE_RATE = 20 const msgCount: Record = {} +let saveRate: number = 0 let totalMsgCount = 0 -// chance to reply - 0.02 ~ every 50 messages -export const CHATTER_REPLY_CHANCE = 0.02 +export let chatterChance: number = DEFAULT_CHATTER_CHANCE logger.log('Initializing MegaHAL') const start = Date.now() @@ -26,6 +36,9 @@ logger.log('MegaHAL initialized in', duration, 'ms') loadBrain() async function loadBrain() { + saveRate = (await getSetting('chat.brainSaveRate')) ?? DEFAULT_SAVE_RATE + chatterChance = (await getSetting('chat.chatterChance')) ?? DEFAULT_CHATTER_CHANCE + const exists = await fileExists(BRAIN_FILE) if (!exists) { @@ -83,7 +96,7 @@ export async function trainMegahal(message: Discord.Message, replyChance: number const input = replaceUserMentions(message, unprefixedInput) logger.debug('Learning from message:', JSON.stringify(input)) - if (totalMsgCount >= SAVE_RATE) { + if (totalMsgCount >= saveRate) { saveBrain() totalMsgCount = 0 } @@ -93,7 +106,12 @@ export async function trainMegahal(message: Discord.Message, replyChance: number const response = megahal.reply(input) if (Math.random() < replyChance && !isMuted()) { - logger.log('Chatter chance reached, replying:', JSON.stringify(response)) + const isSameAsChatter = replyChance === chatterChance + const replyIsForced = replyChance >= 1 + const isTrigger = replyIsForced && !isSameAsChatter + const prefix = isTrigger ? 'Manual reply triggered,' : 'Chatter chance reached,' + + logger.log(prefix, 'replying:', JSON.stringify(response)) message.reply(response.replace(//g, '')) msgCount[key]! = 0 } diff --git a/src/core/message_handler.ts b/src/core/message_handler.ts index d5a50d8..1e5fb2d 100644 --- a/src/core/message_handler.ts +++ b/src/core/message_handler.ts @@ -2,7 +2,7 @@ import Discord from 'discord.js' import { CHAT_TRIGGERS, COMMAND_TRIGGERS } from '@/env' import { parseArguments, parseCommand } from '@/core/commands' import { logger } from '@/core/logger' -import { CHATTER_REPLY_CHANCE, trainMegahal } from '@/core/megahal' +import { chatterChance, trainMegahal } from '@/core/megahal' import { isWhitelisted } from '@/lib/whitelist' import { isAdministrator } from '@/utils/discord_utils' @@ -61,6 +61,6 @@ export async function handleMessage(message: Discord.Message) { if (!triggered) { const isTriggerPrefix = CHAT_TRIGGERS.some((p) => message.content.toLowerCase().startsWith(p)) - trainMegahal(message, isTriggerPrefix ? 1 : CHATTER_REPLY_CHANCE) + trainMegahal(message, isTriggerPrefix ? 1 : chatterChance) } }