refactor: allow lazy loading description/examples

This commit is contained in:
2024-08-09 14:11:30 +03:00
parent ab1e4054a7
commit 47cf394345
6 changed files with 30 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import {
getMegahalBrainSize,
isMuted,
megahal,
reloadSettings,
saveBrain,
setMuted,
} from '@/core/megahal'
@@ -15,12 +16,12 @@ import { isAdministrator } from '@/utils/discord_utils'
export default command({
command: 'chat',
aliases: ['c'],
description:
description: () =>
`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: [
examples: () => [
`\`${DEFAULT_COMMAND_PREFIX}chat mute\` - shuts him up`,
`\`${DEFAULT_COMMAND_PREFIX}chat unmute\` - unmutes him`,
`\`${DEFAULT_COMMAND_PREFIX}chat save\` - backs up the brain immediately`,
@@ -30,6 +31,7 @@ export default command({
`\`${DEFAULT_COMMAND_PREFIX}chat whitelist <get|add|remove> <guild|channel>\` - get/update whitelist for ` +
`guild/channel (see \`${DEFAULT_COMMAND_PREFIX}help whitelist\` for more information (admins only).`,
`\`${DEFAULT_COMMAND_PREFIX}chat train <lines>\` - feed megahal multiple lines to train (admins only)`,
`\`${DEFAULT_COMMAND_PREFIX}chat reload-settings\` - reload settings after a change (admins only)`,
],
execute: async (message, args) => {
if (!args.length) {
@@ -85,6 +87,10 @@ export default command({
message.reply('Trained successfully')
break
}
case 'reload-settings':
await reloadSettings()
message.reply('Settings reloaded')
break
case 'size': {
const size = await getMegahalBrainSize('string')
message.reply(`Brain size: ${size}`)

View File

@@ -4,6 +4,7 @@ import { DEFAULT_COMMAND_PREFIX } from '@/env'
import { logger } from '@/core/logger'
import { isWhitelisted } from '@/lib/whitelist'
import { isAdministrator } from '@/utils/discord_utils'
import { resolver } from '@/utils/object_utils'
interface HelpMessage {
command: string
@@ -38,7 +39,8 @@ export default command({
let description = `\`${DEFAULT_COMMAND_PREFIX}${cmd.command}\``
if (cmd.description) {
const wrapped = cmd.description.replace(/\n/g, '\n\t\t\t')
const _description = resolver(cmd.description)
const wrapped = _description.replace(/\n/g, '\n\t\t\t')
description += ` - ${wrapped}`
}
if (cmd.aliases.length) {
@@ -48,7 +50,8 @@ export default command({
}
if (name) {
cmd.examples.forEach((example) => {
const examples = resolver(cmd.examples)
examples.forEach((example) => {
description += `\n\t\t\t*e.g.:* ${example}`
})
} else if (cmd.command !== 'help') {

View File

@@ -3,6 +3,7 @@ import path from 'node:path'
import fs from 'node:fs/promises'
import { COMMAND_TRIGGERS } from '@/env'
import { logger } from './logger'
import { Resolver } from '@/utils/object_utils'
export interface Command {
/** Command name */
@@ -10,9 +11,9 @@ export interface Command {
/** Command aliases */
aliases: string[]
/** Command description - shown in help */
description: string
description: Resolver<string>
/** Command examples - shown in help */
examples: string[]
examples: Resolver<string[]>
/** Global commands are available on non-whitelisted channels (default: false) */
global?: boolean
/** Admin-only commands are only available to whitelisted users/permissions/roles (default: false) */

View File

@@ -1,8 +1,9 @@
import { MONGODB_URI } from '@/env'
import { MongoClient } from 'mongodb'
import { logger } from '@/core/logger'
const client = new MongoClient(MONGODB_URI)
client.on('open', () => {
console.log('Connected to MongoDB')
logger.log('Connected to MongoDB')
})
export const db = client.db()

View File

@@ -36,8 +36,7 @@ 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
await reloadSettings()
const exists = await fileExists(BRAIN_FILE)
@@ -47,6 +46,11 @@ async function loadBrain() {
return megahal.load(BRAIN_FILE).then(() => logger.log('Brain loaded from', BRAIN_FILE))
}
export async function reloadSettings() {
saveRate = (await getSetting<number>('chat.brainSaveRate')) ?? DEFAULT_SAVE_RATE
chatterChance = (await getSetting<number>('chat.chatterChance')) ?? DEFAULT_CHATTER_CHANCE
}
export async function saveBrain() {
const success = await megahal.save(BRAIN_FILE)
if (success) {

View File

@@ -0,0 +1,6 @@
/** Resolver type - can be type T or a function that returns T to be loaded lazily */
export type Resolver<T> = T | (() => T)
export function resolver<T>(resolver: Resolver<T>): T {
return typeof resolver === 'function' ? (resolver as () => T)() : resolver
}