mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 21:56:17 +00:00
fix(bots/discord): fix reload not working
This commit is contained in:
@@ -1,8 +1,5 @@
|
|||||||
import { dirname, join } from 'path'
|
|
||||||
import { AdminCommand } from '$/classes/Command'
|
import { AdminCommand } from '$/classes/Command'
|
||||||
|
|
||||||
import type { Config } from 'config.schema'
|
|
||||||
|
|
||||||
export default new AdminCommand({
|
export default new AdminCommand({
|
||||||
name: 'reload',
|
name: 'reload',
|
||||||
description: 'Reload configuration',
|
description: 'Reload configuration',
|
||||||
@@ -10,10 +7,8 @@ export default new AdminCommand({
|
|||||||
const { api, logger, discord } = context
|
const { api, logger, discord } = context
|
||||||
logger.info(`Reload triggered by ${context.executor.tag} (${context.executor.id})`)
|
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
|
logger.debug('Invalidating previous config...')
|
||||||
const path = `${Bun.pathToFileURL(join(dirname(Bun.main), '..', 'config.js')).toString()}?cache=${Date.now()}`
|
context.config.invalidate()
|
||||||
logger.debug(`Reloading configuration from: ${path}`)
|
|
||||||
context.config = ((await import(path)) as { default: Config }).default
|
|
||||||
|
|
||||||
if ('deferReply' in trigger) await trigger.deferReply({ ephemeral: true })
|
if ('deferReply' in trigger) await trigger.deferReply({ ephemeral: true })
|
||||||
|
|
||||||
@@ -28,7 +23,7 @@ export default new AdminCommand({
|
|||||||
api.client.disconnect(true)
|
api.client.disconnect(true)
|
||||||
api.disconnectCount = 0
|
api.disconnectCount = 0
|
||||||
api.intentionallyDisconnecting = false
|
api.intentionallyDisconnecting = false
|
||||||
await api.client.connect()
|
api.client.connect()
|
||||||
|
|
||||||
logger.info('Reinitializing Discord client to reload configuration...')
|
logger.info('Reinitializing Discord client to reload configuration...')
|
||||||
await discord.client.destroy()
|
await discord.client.destroy()
|
||||||
|
|||||||
@@ -1,23 +1,49 @@
|
|||||||
import { Database } from 'bun:sqlite'
|
import { Database } from 'bun:sqlite'
|
||||||
import { existsSync, readFileSync, readdirSync } from 'fs'
|
import { existsSync, readFileSync, readdirSync } from 'fs'
|
||||||
import { join } from 'path'
|
import { dirname, join } from 'path'
|
||||||
import { Client as APIClient } from '@revanced/bot-api'
|
import { Client as APIClient } from '@revanced/bot-api'
|
||||||
import { createLogger } from '@revanced/bot-shared'
|
import { createLogger } from '@revanced/bot-shared'
|
||||||
import { Client as DiscordClient, type Message, Partials } from 'discord.js'
|
import { Client as DiscordClient, type Message, Partials } from 'discord.js'
|
||||||
import { drizzle } from 'drizzle-orm/bun-sqlite'
|
import { drizzle } from 'drizzle-orm/bun-sqlite'
|
||||||
|
import type { default as Command, CommandOptionsOptions, CommandType } from './classes/Command'
|
||||||
|
import * as schemas from './database/schemas'
|
||||||
|
|
||||||
// Export some things first, as commands require them
|
// Export some things first, as commands require them
|
||||||
import config from '../config.js'
|
import _firstConfig from '../config.js'
|
||||||
export { config }
|
|
||||||
|
let currentConfig = _firstConfig
|
||||||
|
|
||||||
|
// Other parts of the code will access properties of this proxy, they don't care what the target looks like
|
||||||
|
export const config = new Proxy(
|
||||||
|
{
|
||||||
|
INSPECTION_WARNING: 'Run `context.__getConfig()` to inspect the latest config.',
|
||||||
|
} as unknown as typeof currentConfig,
|
||||||
|
{
|
||||||
|
get(_, p, receiver) {
|
||||||
|
if (p === 'invalidate')
|
||||||
|
return async () => {
|
||||||
|
const path = join(dirname(Bun.main), '..', 'config.js')
|
||||||
|
Loader.registry.delete(path)
|
||||||
|
currentConfig = (await import(path)).default
|
||||||
|
logger.debug('New config set')
|
||||||
|
}
|
||||||
|
|
||||||
|
return Reflect.get(currentConfig, p, receiver)
|
||||||
|
},
|
||||||
|
set(_, p, newValue, receiver) {
|
||||||
|
return Reflect.set(currentConfig, p, newValue, receiver)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
) as typeof _firstConfig & { invalidate(): void }
|
||||||
|
|
||||||
|
export const __getConfig = () => currentConfig
|
||||||
|
|
||||||
export const logger = createLogger({
|
export const logger = createLogger({
|
||||||
level: config.logLevel === 'none' ? Number.MAX_SAFE_INTEGER : config.logLevel,
|
level: config.logLevel === 'none' ? Number.MAX_SAFE_INTEGER : config.logLevel,
|
||||||
})
|
})
|
||||||
|
|
||||||
import * as commands from './commands'
|
// Importing later because config needs to be exported before
|
||||||
import * as schemas from './database/schemas'
|
const commands = await import('./commands')
|
||||||
|
|
||||||
import type { default as Command, CommandOptionsOptions, CommandType } from './classes/Command'
|
|
||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
client: new APIClient({
|
client: new APIClient({
|
||||||
|
|||||||
Reference in New Issue
Block a user