feat(bots/discord): improve admin commands

- The reload command now properly reloads configuration changes by skipping the configuration cache
- The eval command now sends a file if the output is too long
- The eval command now restricts access to the bot token by removing it and sandboxing the input code execution
This commit is contained in:
PalmDevs
2024-09-25 06:30:51 +07:00
parent e0e40237fa
commit 0346741188
3 changed files with 79 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
import { dirname, join } from 'path'
import { AdminCommand } from '$/classes/Command'
import { join, dirname } from 'path'
import type { Config } from 'config.schema'
@@ -8,7 +8,12 @@ export default new AdminCommand({
description: 'Reload configuration',
async execute(context, trigger) {
const { api, logger, discord } = context
context.config = ((await import(join(dirname(Bun.main), '..', 'config.js'))) as { default: Config }).default
logger.info(`Reload triggered by ${context.executor.tag} (${context.executor.id})`)
// Apparently the query strings only work with non-Windows "URLs", otherwise it'd just infinitely hang
const path = `${Bun.pathToFileURL(join(dirname(Bun.main), '..', 'config.js')).toString()}?cache=${Date.now()}`
logger.debug(`Reloading configuration from: ${path}`)
context.config = ((await import(path)) as { default: Config }).default
if ('deferReply' in trigger) await trigger.deferReply({ ephemeral: true })
@@ -32,6 +37,6 @@ export default new AdminCommand({
await discord.client.login(process.env['DISCORD_TOKEN'])
// @ts-expect-error: TypeScript dum
await trigger[('deferReply' in trigger ? 'editReply' : 'reply')]({ content: 'Reloaded configuration' })
await trigger['deferReply' in trigger ? 'editReply' : 'reply']({ content: 'Reloaded configuration' })
},
})