From e60c2f88b83e5c80d42362bd318d4c9d75c2201d Mon Sep 17 00:00:00 2001 From: Kevin Dang <77701718+kevinthedang@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:59:54 -0700 Subject: [PATCH] Handlers Directory and Universal Import Fix (#86) * Update: split jsonHandler.ts to different files * Add: handlers folder and moved some files there * Update: interface file name --- src/commands/capacity.ts | 2 +- src/commands/channelToggle.ts | 2 +- src/commands/disable.ts | 2 +- src/commands/messageStream.ts | 2 +- src/commands/messageStyle.ts | 2 +- src/commands/threadCreate.ts | 2 +- src/commands/threadPrivateCreate.ts | 2 +- src/events/messageCreate.ts | 2 +- src/utils/configInterfaces.ts | 58 +++++++ .../chatHistoryHandler.ts} | 143 +----------------- src/utils/handlers/configHandler.ts | 87 +++++++++++ src/utils/{ => handlers}/streamHandler.ts | 2 +- src/utils/index.ts | 7 +- 13 files changed, 161 insertions(+), 152 deletions(-) create mode 100644 src/utils/configInterfaces.ts rename src/utils/{jsonHandler.ts => handlers/chatHistoryHandler.ts} (53%) create mode 100644 src/utils/handlers/configHandler.ts rename src/utils/{ => handlers}/streamHandler.ts (96%) diff --git a/src/commands/capacity.ts b/src/commands/capacity.ts index d6bb5d3..9b568c5 100644 --- a/src/commands/capacity.ts +++ b/src/commands/capacity.ts @@ -1,6 +1,6 @@ import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openConfig } from '../utils/jsonHandler.js' +import { openConfig } from '../utils/index.js' export const Capacity: SlashCommand = { name: 'modify-capacity', diff --git a/src/commands/channelToggle.ts b/src/commands/channelToggle.ts index 78b63d6..1e1aaa6 100644 --- a/src/commands/channelToggle.ts +++ b/src/commands/channelToggle.ts @@ -1,6 +1,6 @@ import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openConfig } from '../utils/jsonHandler.js' +import { openConfig } from '../utils/index.js' export const ChannelToggle: SlashCommand = { name: 'channel-toggle', diff --git a/src/commands/disable.ts b/src/commands/disable.ts index ca0bd81..a2dd86d 100644 --- a/src/commands/disable.ts +++ b/src/commands/disable.ts @@ -1,6 +1,6 @@ import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openConfig } from '../utils/jsonHandler.js' +import { openConfig } from '../utils/index.js' export const Disable: SlashCommand = { name: 'toggle-chat', diff --git a/src/commands/messageStream.ts b/src/commands/messageStream.ts index 254dd2e..fe3fe95 100644 --- a/src/commands/messageStream.ts +++ b/src/commands/messageStream.ts @@ -1,6 +1,6 @@ import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openConfig } from '../utils/jsonHandler.js' +import { openConfig } from '../utils/index.js' export const MessageStream: SlashCommand = { name: 'message-stream', diff --git a/src/commands/messageStyle.ts b/src/commands/messageStyle.ts index 7833fb6..c4204ed 100644 --- a/src/commands/messageStyle.ts +++ b/src/commands/messageStyle.ts @@ -1,6 +1,6 @@ import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openConfig } from '../utils/jsonHandler.js' +import { openConfig } from '../utils/index.js' export const MessageStyle: SlashCommand = { name: 'message-style', diff --git a/src/commands/threadCreate.ts b/src/commands/threadCreate.ts index b87f65c..9592c87 100644 --- a/src/commands/threadCreate.ts +++ b/src/commands/threadCreate.ts @@ -1,6 +1,6 @@ import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openThreadInfo } from '../utils/jsonHandler.js' +import { openThreadInfo } from '../utils/index.js' export const ThreadCreate: SlashCommand = { name: 'thread', diff --git a/src/commands/threadPrivateCreate.ts b/src/commands/threadPrivateCreate.ts index 6ebd2ef..49bda35 100644 --- a/src/commands/threadPrivateCreate.ts +++ b/src/commands/threadPrivateCreate.ts @@ -1,6 +1,6 @@ import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import { openThreadInfo } from '../utils/jsonHandler.js' +import { openThreadInfo } from '../utils/index.js' export const PrivateThreadCreate: SlashCommand = { name: 'private-thread', diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index d13bacc..812e74b 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,5 +1,5 @@ import { embedMessage, event, Events, normalMessage, UserMessage } from '../utils/index.js' -import { getChannelInfo, getServerConfig, getThread, getUserConfig, openChannelInfo, openConfig, openThreadInfo, ServerConfig, UserConfig } from '../utils/jsonHandler.js' +import { getChannelInfo, getServerConfig, getThread, getUserConfig, openChannelInfo, openConfig, openThreadInfo, ServerConfig, UserConfig } from '../utils/index.js' import { clean } from '../utils/mentionClean.js' import { TextChannel, ThreadChannel } from 'discord.js' diff --git a/src/utils/configInterfaces.ts b/src/utils/configInterfaces.ts new file mode 100644 index 0000000..6ba23eb --- /dev/null +++ b/src/utils/configInterfaces.ts @@ -0,0 +1,58 @@ +import { UserMessage } from './events.js' + +export interface UserConfiguration { + 'message-stream'?: boolean, + 'message-style'?: boolean, + 'modify-capacity': number +} + +export interface ServerConfiguration { + 'toggle-chat'?: boolean, + 'channel-toggle'?: boolean +} + +/** + * Parent Configuration interface + * + * @see ServerConfiguration server settings per guild + * @see UserConfiguration user configurations (only for the user for any server) + */ +export interface Configuration { + readonly name: string + options: UserConfiguration | ServerConfiguration +} + +/** + * User config to use outside of this file + */ +export interface UserConfig { + readonly name: string + options: UserConfiguration +} + +export interface ServerConfig { + readonly name: string + options: ServerConfiguration +} + +export interface Thread { + readonly id: string + readonly name: string + messages: UserMessage[] +} + +export interface Channel { + readonly id: string + readonly name: string + readonly user: string + messages: UserMessage[] +} + +/** + * 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', 'channel-toggle'].includes(key); +} \ No newline at end of file diff --git a/src/utils/jsonHandler.ts b/src/utils/handlers/chatHistoryHandler.ts similarity index 53% rename from src/utils/jsonHandler.ts rename to src/utils/handlers/chatHistoryHandler.ts index 85ff44e..0f9ef76 100644 --- a/src/utils/jsonHandler.ts +++ b/src/utils/handlers/chatHistoryHandler.ts @@ -1,149 +1,8 @@ import { TextChannel, ThreadChannel } from 'discord.js' -import { UserMessage } from './events.js' +import { Configuration, Thread, Channel, UserMessage } from '../index.js' import fs from 'fs' import path from 'path' -export interface UserConfiguration { - 'message-stream'?: boolean, - 'message-style'?: boolean, - 'modify-capacity': number -} - -export interface ServerConfiguration { - 'toggle-chat'?: boolean, - 'channel-toggle'?: boolean -} - -/** - * Parent Configuration interface - * - * @see ServerConfiguration server settings per guild - * @see UserConfiguration user configurations (only for the user for any server) - */ -export interface Configuration { - readonly name: string - options: UserConfiguration | ServerConfiguration -} - -/** - * User config to use outside of this file - */ -export interface UserConfig { - readonly name: string - options: UserConfiguration -} - -export interface ServerConfig { - readonly name: string - options: ServerConfiguration -} - -export interface Thread { - readonly id: string - readonly name: string - messages: UserMessage[] -} - -export interface Channel { - readonly id: string - readonly name: string - readonly user: string - messages: UserMessage[] -} - -function isUserConfigurationKey(key: string): key is keyof UserConfiguration { - return ['message-stream', 'message-style', 'modify-capacity'].includes(key); -} - -function isServerConfigurationKey(key: string): key is keyof ServerConfiguration { - return ['toggle-chat', 'channel-toggle'].includes(key); -} - -/** - * 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 - */ -// add type of change (server, user) -export function openConfig(filename: string, key: string, value: any) { - const fullFileName = `data/${filename}` - - // check if the file exists, if not then make the config file - if (fs.existsSync(fullFileName)) { - fs.readFile(fullFileName, 'utf8', (error, data) => { - if (error) - console.log(`[Error: openConfig] Incorrect file format`) - else { - const object = JSON.parse(data) - object['options'][key] = value - fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2)) - } - }) - } else { // work on dynamic file creation - let object: Configuration - if (isServerConfigurationKey(key)) - object = JSON.parse('{ \"name\": \"Server Confirgurations\" }') - else - object = JSON.parse('{ \"name\": \"User Confirgurations\" }') - - // set standard information for config file and options - object['options'] = { - [key]: value - } - - fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2)) - console.log(`[Util: openConfig] Created '${filename}' in working directory`) - } -} - -/** - * 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}` - - // attempt to read the file and get the configuration - if (fs.existsSync(fullFileName)) { - fs.readFile(fullFileName, 'utf8', (error, data) => { - if (error) { - callback(undefined) - return // something went wrong... stop - } - callback(JSON.parse(data)) - }) - } else { - callback(undefined) // file not found - } -} - -/** - * 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}` - - // attempt to read the file and get the configuration - if (fs.existsSync(fullFileName)) { - fs.readFile(fullFileName, 'utf8', (error, data) => { - if (error) { - callback(undefined) - return // something went wrong... stop - } - callback(JSON.parse(data)) - }) - } else { - callback(undefined) // file not found - } -} - /** * Method to open/create and modify a json file containing thread information * diff --git a/src/utils/handlers/configHandler.ts b/src/utils/handlers/configHandler.ts new file mode 100644 index 0000000..ee9746e --- /dev/null +++ b/src/utils/handlers/configHandler.ts @@ -0,0 +1,87 @@ +import { Configuration, ServerConfig, UserConfig, isServerConfigurationKey } from '../index.js' +import fs from 'fs' + +/** + * 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 + */ +// add type of change (server, user) +export function openConfig(filename: string, key: string, value: any) { + const fullFileName = `data/${filename}` + + // check if the file exists, if not then make the config file + if (fs.existsSync(fullFileName)) { + fs.readFile(fullFileName, 'utf8', (error, data) => { + if (error) + console.log(`[Error: openConfig] Incorrect file format`) + else { + const object = JSON.parse(data) + object['options'][key] = value + fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2)) + } + }) + } else { // work on dynamic file creation + let object: Configuration + if (isServerConfigurationKey(key)) + object = JSON.parse('{ \"name\": \"Server Confirgurations\" }') + else + object = JSON.parse('{ \"name\": \"User Confirgurations\" }') + + // set standard information for config file and options + object['options'] = { + [key]: value + } + + fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2)) + console.log(`[Util: openConfig] Created '${filename}' in working directory`) + } +} + +/** + * 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}` + + // attempt to read the file and get the configuration + if (fs.existsSync(fullFileName)) { + fs.readFile(fullFileName, 'utf8', (error, data) => { + if (error) { + callback(undefined) + return // something went wrong... stop + } + callback(JSON.parse(data)) + }) + } else { + callback(undefined) // file not found + } +} + +/** + * 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}` + + // attempt to read the file and get the configuration + if (fs.existsSync(fullFileName)) { + fs.readFile(fullFileName, 'utf8', (error, data) => { + if (error) { + callback(undefined) + return // something went wrong... stop + } + callback(JSON.parse(data)) + }) + } else { + callback(undefined) // file not found + } +} diff --git a/src/utils/streamHandler.ts b/src/utils/handlers/streamHandler.ts similarity index 96% rename from src/utils/streamHandler.ts rename to src/utils/handlers/streamHandler.ts index f6f98d7..4b6b4f4 100644 --- a/src/utils/streamHandler.ts +++ b/src/utils/handlers/streamHandler.ts @@ -1,5 +1,5 @@ import { ChatResponse } from "ollama" -import { ChatParams } from "./index.js" +import { ChatParams } from "../index.js" import { AbortableAsyncIterator } from "ollama/src/utils.js" /** diff --git a/src/utils/index.ts b/src/utils/index.ts index b5ff26f..edae3a1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,4 +4,9 @@ export * from './events.js' export * from './messageEmbed.js' export * from './messageNormal.js' export * from './commands.js' -export * from './streamHandler.js' \ No newline at end of file +export * from './configInterfaces.js' + +// handler imports +export * from './handlers/chatHistoryHandler.js' +export * from './handlers/configHandler.js' +export * from './handlers/streamHandler.js'