Files
tg-chat-bot/src/db/database.ts
T
melod1n b74e0a0f3e refactor(bot): centralize runtime state; support albums + safer vision handling
- make MessageStore.put() return StoredMessage and allow collectReplyChainText() to work with StoredMessage
- move muted users + answers loading into Environment (add Answers model + GEMINI_IMAGE_MODEL)
- extract message handling into processNewMessage() and add media-group (album) caching/downloading by unique_file_id
- for Ollama: check model capabilities before sending images; use replyToMessage helper consistently
- add /geminiGenImage command stub for Gemini image generation
2026-01-29 20:01:30 +03:00

56 lines
1.5 KiB
TypeScript

import * as fs from "fs";
import {Environment} from "../common/environment";
import {logError} from "../util/utils";
import {Answers} from "../model/answers";
type DataJsonFile = {
admins: number[]
muted: number[]
}
export let jsonFile: DataJsonFile;
export async function readData(): Promise<void> {
try {
jsonFile = JSON.parse(fs.readFileSync(`${Environment.DATA_PATH}/data.json`).toString());
const admins = jsonFile.admins || [];
admins.unshift(Environment.CREATOR_ID);
Environment.setAdmins(new Set<number>(admins));
Environment.setMuted(new Set<number>(jsonFile.muted || []));
return Promise.resolve();
} catch (e) {
logError(e);
return Promise.reject(e);
}
}
export async function saveData(): Promise<void> {
const adminIds: number[] = [];
Environment.ADMIN_IDS.forEach(id => adminIds.push(id));
jsonFile.admins = adminIds;
const mutedList: number[] = [];
Environment.MUTED_IDS.forEach(id => mutedList.push(id));
jsonFile.muted = mutedList;
try {
fs.writeFileSync(`${Environment.DATA_PATH}/data.json`, JSON.stringify(jsonFile));
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());
Environment.setAnswers(json);
return Promise.resolve();
} catch (e) {
logError(e);
return Promise.reject(e);
}
}