shitton of the ai changes
This commit is contained in:
+59
-30
@@ -3,6 +3,7 @@ import {Environment} from "../common/environment";
|
||||
import {logError} from "../util/utils";
|
||||
import {Answers} from "../model/answers";
|
||||
import path from "node:path";
|
||||
import {KeyedAsyncLock} from "../util/async-lock";
|
||||
|
||||
type DataJsonFile = {
|
||||
admins: number[]
|
||||
@@ -11,9 +12,42 @@ type DataJsonFile = {
|
||||
|
||||
export let jsonFile: DataJsonFile;
|
||||
|
||||
const DEFAULT_DATA: DataJsonFile = {
|
||||
admins: [],
|
||||
muted: [],
|
||||
};
|
||||
|
||||
const DEFAULT_ANSWERS: Answers = {
|
||||
test: ["a"],
|
||||
prefix: ["?"],
|
||||
better: ["Better"],
|
||||
who: [],
|
||||
kick: [],
|
||||
invite: [],
|
||||
day: [],
|
||||
};
|
||||
|
||||
const dataFileLock = new KeyedAsyncLock();
|
||||
|
||||
function ensureDataPath(): void {
|
||||
fs.mkdirSync(Environment.DATA_PATH, {recursive: true});
|
||||
}
|
||||
|
||||
function readJsonFile<T>(fileName: string, defaultValue: T): T {
|
||||
ensureDataPath();
|
||||
|
||||
const filePath = `${Environment.DATA_PATH}/${fileName}`;
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, JSON.stringify(defaultValue, null, 2));
|
||||
return structuredClone(defaultValue);
|
||||
}
|
||||
|
||||
return JSON.parse(fs.readFileSync(filePath).toString()) as T;
|
||||
}
|
||||
|
||||
export async function readData(): Promise<void> {
|
||||
try {
|
||||
jsonFile = JSON.parse(fs.readFileSync(`${Environment.DATA_PATH}/data.json`).toString());
|
||||
jsonFile = readJsonFile("data.json", DEFAULT_DATA);
|
||||
|
||||
const admins = jsonFile.admins || [];
|
||||
admins.unshift(Environment.CREATOR_ID);
|
||||
@@ -23,48 +57,43 @@ export async function readData(): Promise<void> {
|
||||
|
||||
return Promise.resolve();
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
logError(e instanceof Error ? e : String(e));
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function readPrompts(): Promise<void> {
|
||||
try {
|
||||
const prompt = fs.readFileSync(path.join(Environment.DATA_PATH, "system_prompt.txt")).toString().trim();
|
||||
if (prompt.length) {
|
||||
Environment.setSystemPrompt(prompt);
|
||||
}
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
export async function saveData(): Promise<void> {
|
||||
const adminIds: number[] = [];
|
||||
Environment.ADMIN_IDS.forEach(id => adminIds.push(id));
|
||||
jsonFile.admins = adminIds;
|
||||
return dataFileLock.runExclusive("data.json", async () => {
|
||||
ensureDataPath();
|
||||
jsonFile ??= structuredClone(DEFAULT_DATA);
|
||||
|
||||
const mutedList: number[] = [];
|
||||
Environment.MUTED_IDS.forEach(id => mutedList.push(id));
|
||||
jsonFile.muted = mutedList;
|
||||
const adminIds: number[] = [];
|
||||
Environment.ADMIN_IDS.forEach(id => adminIds.push(id));
|
||||
jsonFile.admins = adminIds;
|
||||
|
||||
try {
|
||||
fs.writeFileSync(`${Environment.DATA_PATH}/data.json`, JSON.stringify(jsonFile));
|
||||
return readData();
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
const mutedList: number[] = [];
|
||||
Environment.MUTED_IDS.forEach(id => mutedList.push(id));
|
||||
jsonFile.muted = mutedList;
|
||||
|
||||
try {
|
||||
const filePath = path.join(Environment.DATA_PATH, "data.json");
|
||||
const tmpPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
||||
fs.writeFileSync(tmpPath, JSON.stringify(jsonFile));
|
||||
fs.renameSync(tmpPath, filePath);
|
||||
return readData();
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function retrieveAnswers(): Promise<void> {
|
||||
try {
|
||||
const json: Answers = JSON.parse(fs.readFileSync(`${Environment.DATA_PATH}/answers.json`).toString());
|
||||
const json = readJsonFile("answers.json", DEFAULT_ANSWERS);
|
||||
Environment.setAnswers(json);
|
||||
return Promise.resolve();
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
logError(e instanceof Error ? e : String(e));
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user