import { Configuration, ServerConfig, UserConfig, isServerConfigurationKey } from '../index.js' import fs from 'fs/promises' // Use promises for async import path from 'path' /** * Method to open a file in the working directory and modify/create it * * @param filename name of the file * @param key key value to access * @param value new value to assign */ export async function openConfig(filename: string, key: string, value: any): Promise { const fullFileName = `data/${filename}` let object: Configuration; try { if (await fs.access(fullFileName).then(() => true).catch(() => false)) { const data = await fs.readFile(fullFileName, 'utf8'); object = JSON.parse(data); object['options'][key] = value; } else { // Create new config object = { name: isServerConfigurationKey(key) ? "Server Configurations" : "User Configurations", options: { [key]: value } }; const directory = path.dirname(fullFileName); await fs.mkdir(directory, { recursive: true }); } await fs.writeFile(fullFileName, JSON.stringify(object, null, 2)); console.log(`[Util: openConfig] Updated/Created '${filename}' in working directory`); } catch (error) { console.error(`[Error: openConfig] Failed to process config file: ${error}`); throw error; } } /** * Method to obtain the configurations of the message chat/thread * * @param filename name of the configuration file to get * @param callback function to allow a promise from getting the config */ export async function getServerConfig(filename: string, callback: (config: ServerConfig | undefined) => void): Promise { const fullFileName = `data/${filename}`; try { if (await fs.access(fullFileName).then(() => true).catch(() => false)) { const data = await fs.readFile(fullFileName, 'utf8'); callback(JSON.parse(data)); } else { // Create default server config const defaultConfig: ServerConfig = { name: "Server Configurations", options: { 'toggle-chat': true } }; const directory = path.dirname(fullFileName); await fs.mkdir(directory, { recursive: true }); await fs.writeFile(fullFileName, JSON.stringify(defaultConfig, null, 2)); console.log(`[Util: getServerConfig] Created default config '${filename}'`); callback(defaultConfig); } } catch (error) { console.error(`[Error: getServerConfig] Failed to read or create config: ${error}`); callback(undefined); } } /** * Method to obtain the configurations of the message chat/thread * * @param filename name of the configuration file to get * @param callback function to allow a promise from getting the config */ export async function getUserConfig(filename: string, callback: (config: UserConfig | undefined) => void): Promise { const fullFileName = `data/${filename}`; try { if (await fs.access(fullFileName).then(() => true).catch(() => false)) { const data = await fs.readFile(fullFileName, 'utf8'); callback(JSON.parse(data)); } else { callback(undefined); // User config handled by Redis in messageCreate.ts } } catch (error) { console.error(`[Error: getUserConfig] Failed to read config: ${error}`); callback(undefined); } }