refactor: consolidate config loading

This commit is contained in:
syntaxbullet
2026-01-08 16:21:25 +01:00
parent 2b641c952d
commit 3f028eb76a
3 changed files with 15 additions and 24 deletions

View File

@@ -1,8 +1,7 @@
import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder, PermissionFlagsBits, MessageFlags } from "discord.js";
import { createBaseEmbed } from "@lib/embeds";
import { configManager } from "@shared/lib/configManager";
import { config, reloadConfig } from "@shared/lib/config";
import { config, reloadConfig, toggleCommand } from "@shared/lib/config";
import { AuroraClient } from "@/lib/BotClient";
export const features = createCommand({
@@ -79,11 +78,11 @@ export const features = createCommand({
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
configManager.toggleCommand(commandName, enabled);
toggleCommand(commandName, enabled);
await interaction.editReply({ content: `✅ Command **${commandName}** has been ${enabled ? "enabled" : "disabled"}. Reloading configuration...` });
// Reload config from disk (which was updated by configManager)
// Reload config from disk (which was updated by toggleCommand)
reloadConfig();
await AuroraClient.loadCommands(true);

View File

@@ -2,7 +2,7 @@ import { readFileSync, existsSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { z } from 'zod';
const configPath = join(import.meta.dir, '..', '..', 'config', 'config.json');
const configPath = join(import.meta.dir, '..', 'config', 'config.json');
export interface GameConfigType {
leveling: {
@@ -202,3 +202,14 @@ export function saveConfig(newConfig: unknown) {
writeFileSync(configPath, jsonString, 'utf-8');
reloadConfig();
}
export function toggleCommand(commandName: string, enabled: boolean) {
const newConfig = {
...config,
commands: {
...config.commands,
[commandName]: enabled
}
};
saveConfig(newConfig);
}

View File

@@ -1,19 +0,0 @@
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
const configPath = join(process.cwd(), 'config', 'config.json');
export const configManager = {
toggleCommand: (commandName: string, enabled: boolean) => {
const raw = readFileSync(configPath, 'utf-8');
const data = JSON.parse(raw);
if (!data.commands) {
data.commands = {};
}
data.commands[commandName] = enabled;
writeFileSync(configPath, JSON.stringify(data, null, 4));
}
};