mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 11:56:06 -05:00
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
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
58
src/utils/configInterfaces.ts
Normal file
58
src/utils/configInterfaces.ts
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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<void> {
|
||||
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<void> {
|
||||
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
|
||||
*
|
||||
87
src/utils/handlers/configHandler.ts
Normal file
87
src/utils/handlers/configHandler.ts
Normal file
@@ -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<void> {
|
||||
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<void> {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
/**
|
||||
@@ -4,4 +4,9 @@ export * from './events.js'
|
||||
export * from './messageEmbed.js'
|
||||
export * from './messageNormal.js'
|
||||
export * from './commands.js'
|
||||
export * from './streamHandler.js'
|
||||
export * from './configInterfaces.js'
|
||||
|
||||
// handler imports
|
||||
export * from './handlers/chatHistoryHandler.js'
|
||||
export * from './handlers/configHandler.js'
|
||||
export * from './handlers/streamHandler.js'
|
||||
|
||||
Reference in New Issue
Block a user