fix errors with no config found for new bot
Some checks failed
Builds / Discord-Node-Build (push) Has been cancelled
Builds / Discord-Ollama-Container-Build (push) Has been cancelled
Coverage / Discord-Node-Coverage (push) Has been cancelled

This commit is contained in:
2025-05-22 09:42:11 -04:00
parent 23a860a905
commit 2630f7d083
2 changed files with 110 additions and 63 deletions

View File

@@ -1,67 +1,91 @@
import { ChannelType } from 'discord.js'
import { UserMessage } from './index.js'
export interface UserConfiguration {
'message-stream'?: boolean,
'modify-capacity': number,
'switch-model': string
}
export interface ServerConfiguration {
'toggle-chat'?: boolean,
}
import { Configuration, ServerConfig, UserConfig, isServerConfigurationKey } from '../index.js'
import fs from 'fs/promises' // Use promises for async
import path from 'path'
/**
* Parent Configuration interface
* Method to open a file in the working directory and modify/create it
*
* @see ServerConfiguration server settings per guild
* @see UserConfiguration user configurations (only for the user for any server)
* @param filename name of the file
* @param key key value to access
* @param value new value to assign
*/
export interface Configuration {
readonly name: string
options: UserConfiguration | ServerConfiguration
export async function openConfig(filename: string, key: string, value: any): Promise<void> {
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;
}
}
/**
* User config to use outside of this file
* 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 interface UserConfig {
readonly name: string
options: UserConfiguration
}
export async function getServerConfig(filename: string, callback: (config: ServerConfig | undefined) => void): Promise<void> {
const fullFileName = `data/${filename}`;
export interface ServerConfig {
readonly name: string
options: ServerConfiguration
}
export interface Channel {
readonly id: string
readonly name: string
readonly user: string
messages: UserMessage[]
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);
}
}
/**
* The following 2 types is allow for better readability in commands
* Admin Command -> Don't run in Threads
* User Command -> Used anywhere
* 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 const AdminCommand = [
ChannelType.GuildText
]
export async function getUserConfig(filename: string, callback: (config: UserConfig | undefined) => void): Promise<void> {
const fullFileName = `data/${filename}`;
export const UserCommand = [
ChannelType.GuildText,
ChannelType.PublicThread,
ChannelType.PrivateThread
]
/**
* Check if the configuration we are editing/taking from is a Server Config
* @param key name of command we ran
* @returns true if command is from Server Config, false otherwise
*/
export function isServerConfigurationKey(key: string): key is keyof ServerConfiguration {
return ['toggle-chat'].includes(key);
}
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);
}
}