everything is broken yay
This commit is contained in:
@@ -1,67 +1,119 @@
|
||||
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 { TextChannel, ThreadChannel } from 'discord.js'
|
||||
import { Channel, UserMessage } from '../index.js'
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
|
||||
/**
|
||||
* Parent Configuration interface
|
||||
* Method to check if a thread history file exists
|
||||
*
|
||||
* @see ServerConfiguration server settings per guild
|
||||
* @see UserConfiguration user configurations (only for the user for any server)
|
||||
* @param channel parent thread of the requested thread (can be GuildText)
|
||||
* @returns true if channel exists, false otherwise
|
||||
*/
|
||||
export interface Configuration {
|
||||
readonly name: string
|
||||
options: UserConfiguration | ServerConfiguration
|
||||
async function checkChannelInfoExists(channel: TextChannel, user: string) {
|
||||
const doesExists: boolean = await new Promise((resolve) => {
|
||||
getChannelInfo(`${channel.id}-${user}.json`, (channelInfo) => {
|
||||
if (channelInfo?.messages) {
|
||||
resolve(true)
|
||||
} else {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
return doesExists
|
||||
}
|
||||
|
||||
/**
|
||||
* User config to use outside of this file
|
||||
* Method to clear channel history for requesting user
|
||||
*
|
||||
* @param filename guild id string
|
||||
* @param channel the TextChannel in the Guild
|
||||
* @param user username or ID of user
|
||||
* @returns true if history was cleared, false if already empty or not found
|
||||
*/
|
||||
export interface UserConfig {
|
||||
readonly name: string
|
||||
options: UserConfiguration
|
||||
}
|
||||
export async function clearChannelInfo(filename: string, channel: TextChannel, user: string): Promise<boolean> {
|
||||
const channelInfoExists: boolean = await checkChannelInfoExists(channel, user)
|
||||
|
||||
export interface ServerConfig {
|
||||
readonly name: string
|
||||
options: ServerConfiguration
|
||||
}
|
||||
// If thread does not exist, file can't be found
|
||||
if (!channelInfoExists) return false
|
||||
|
||||
export interface Channel {
|
||||
readonly id: string
|
||||
readonly name: string
|
||||
readonly user: string
|
||||
messages: UserMessage[]
|
||||
// Attempt to clear user channel history
|
||||
const fullFileName = path.join('/app/data', `${filename}-${user}.json`)
|
||||
try {
|
||||
const data = await fs.readFile(fullFileName, 'utf8')
|
||||
const object = JSON.parse(data)
|
||||
if (object['messages'].length === 0) {
|
||||
console.log(`[Util: clearChannelInfo] History already empty for ${fullFileName}`)
|
||||
return false
|
||||
}
|
||||
object['messages'] = []
|
||||
await fs.writeFile(fullFileName, JSON.stringify(object, null, 2), { flag: 'w', mode: 0o600 })
|
||||
console.log(`[Util: clearChannelInfo] Cleared history for ${fullFileName}`)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.log(`[Util: clearChannelInfo] Failed to clear ${fullFileName}: ${error}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The following 2 types is allow for better readability in commands
|
||||
* Admin Command -> Don't run in Threads
|
||||
* User Command -> Used anywhere
|
||||
* Method to open the channel history
|
||||
*
|
||||
* @param filename name of the json file for the channel by user (without path)
|
||||
* @param channel the text channel info
|
||||
* @param user the user's ID or clientId for bots
|
||||
* @param messages their messages
|
||||
*/
|
||||
export const AdminCommand = [
|
||||
ChannelType.GuildText
|
||||
]
|
||||
|
||||
export const UserCommand = [
|
||||
ChannelType.GuildText,
|
||||
ChannelType.PublicThread,
|
||||
ChannelType.PrivateThread
|
||||
]
|
||||
export async function openChannelInfo(filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
||||
const fullFileName = path.join('/app/data', `${filename}-${user}.json`)
|
||||
console.log(`[Util: openChannelInfo] Attempting to create/open ${fullFileName}`)
|
||||
try {
|
||||
if (await fs.access(fullFileName).then(() => true).catch(() => false)) {
|
||||
const data = await fs.readFile(fullFileName, 'utf8')
|
||||
const object = JSON.parse(data)
|
||||
if (messages.length > 0) {
|
||||
object['messages'] = messages
|
||||
}
|
||||
await fs.writeFile(fullFileName, JSON.stringify(object, null, 2), { flag: 'w', mode: 0o600 })
|
||||
console.log(`[Util: openChannelInfo] Updated ${fullFileName}`)
|
||||
} else {
|
||||
const object: Channel = {
|
||||
id: channel?.id || filename,
|
||||
name: channel?.name || 'unknown',
|
||||
user,
|
||||
messages: messages
|
||||
}
|
||||
const directory = path.dirname(fullFileName)
|
||||
await fs.mkdir(directory, { recursive: true })
|
||||
await fs.writeFile(fullFileName, JSON.stringify(object, null, 2), { flag: 'w', mode: 0o600 })
|
||||
console.log(`[Util: openChannelInfo] Created '${fullFileName}' in working directory`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`[Util: openChannelInfo] Failed to write ${fullFileName}: ${error}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Method to get the channel information/history
|
||||
*
|
||||
* @param filename name of the json file for the channel by user (without path)
|
||||
* @param callback function to handle resolving message history
|
||||
*/
|
||||
export function isServerConfigurationKey(key: string): key is keyof ServerConfiguration {
|
||||
return ['toggle-chat'].includes(key);
|
||||
}
|
||||
export async function getChannelInfo(filename: string, callback: (config: Channel | undefined) => void): Promise<void> {
|
||||
const fullFileName = path.join('/app/data', filename)
|
||||
console.log(`[Util: getChannelInfo] Reading ${fullFileName}`)
|
||||
try {
|
||||
const data = await fs.readFile(fullFileName, 'utf8')
|
||||
const config = JSON.parse(data)
|
||||
if (!config || !Array.isArray(config.messages)) {
|
||||
console.log(`[Util: getChannelInfo] Invalid or empty config in ${fullFileName}: ${JSON.stringify(config)}`)
|
||||
callback(undefined)
|
||||
} else {
|
||||
console.log(`[Util: getChannelInfo] Successfully read ${fullFileName}`)
|
||||
callback(config)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`[Util: getChannelInfo] Failed to read ${fullFileName}: ${error}`)
|
||||
callback(undefined)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user