feat: variable chatter chance/brain save rate

This commit is contained in:
2024-08-09 13:59:10 +03:00
parent 43d7b9b706
commit ab1e4054a7
4 changed files with 32 additions and 13 deletions

View File

@@ -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: [

View File

@@ -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}`,

View File

@@ -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<string, number> = {}
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<number>('chat.brainSaveRate')) ?? DEFAULT_SAVE_RATE
chatterChance = (await getSetting<number>('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(/<error>/g, ''))
msgCount[key]! = 0
}

View File

@@ -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)
}
}