refactor!: rewrite bot core; add AI (Ollama, Gemini), DB, new commands
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import path from "node:path";
|
||||
import {saveData} from "../db/database";
|
||||
|
||||
export class Environment {
|
||||
static BOT_TOKEN: string;
|
||||
static TEST_ENVIRONMENT: boolean;
|
||||
static ADMIN_IDS: Set<number> = new Set<number>();
|
||||
static CHAT_IDS_WHITELIST: Set<number> = new Set<number>();
|
||||
static BOT_PREFIX: string;
|
||||
static CREATOR_ID: number;
|
||||
static IS_DOCKER: boolean;
|
||||
static DATA_PATH: string;
|
||||
static DB_FILE_NAME: string = "database.db";
|
||||
static DB_PATH: string;
|
||||
|
||||
static USE_MOM: boolean;
|
||||
static USE_DAD: boolean;
|
||||
static USE_FU: boolean;
|
||||
|
||||
static OLLAMA_MODEL: string;
|
||||
static OLLAMA_ADDRESS: string;
|
||||
static OLLAMA_API_KEY?: string;
|
||||
static SYSTEM_PROMPT: string;
|
||||
|
||||
static GEMINI_API_KEY: string;
|
||||
|
||||
static waitText = "⏳ Дайте-ка подумать...";
|
||||
|
||||
static load() {
|
||||
Environment.BOT_TOKEN = process.env.BOT_TOKEN;
|
||||
Environment.TEST_ENVIRONMENT = process.env.TEST_ENVIRONMENT === "true";
|
||||
Environment.CHAT_IDS_WHITELIST = new Set(process.env.CHAT_IDS_WHITELIST?.split(",")?.map(e => parseInt(e.trim(), 10)) || []);
|
||||
Environment.BOT_PREFIX = process.env.BOT_PREFIX || "";
|
||||
Environment.CREATOR_ID = parseInt(process.env.CREATOR_ID || "");
|
||||
Environment.IS_DOCKER = process.env.IS_DOCKER == "true";
|
||||
Environment.DATA_PATH = Environment.IS_DOCKER ? path.join("", "config", "data") : "data";
|
||||
Environment.DB_PATH = "file:" + path.join(Environment.DATA_PATH, Environment.DB_FILE_NAME);
|
||||
|
||||
Environment.USE_MOM = process.env.USE_MOM == "true";
|
||||
Environment.USE_DAD = process.env.USE_DAD == "true";
|
||||
Environment.USE_FU = process.env.USE_FU == "true";
|
||||
|
||||
Environment.OLLAMA_MODEL = process.env.OLLAMA_MODEL || "llama3.2";
|
||||
Environment.OLLAMA_ADDRESS = process.env.OLLAMA_ADDRESS || "127.0.0.1";
|
||||
Environment.OLLAMA_API_KEY = process.env.OLLAMA_API_KEY;
|
||||
Environment.SYSTEM_PROMPT = (process.env.SYSTEM_PROMPT?.trim()) || "";
|
||||
|
||||
Environment.GEMINI_API_KEY = process.env.GEMINI_API_KEY;
|
||||
}
|
||||
|
||||
static setAdmins(admins: Set<number>) {
|
||||
this.ADMIN_IDS = admins;
|
||||
}
|
||||
|
||||
static async addAdmin(id: number): Promise<boolean> {
|
||||
const has = this.ADMIN_IDS.has(id);
|
||||
if (!has) {
|
||||
this.ADMIN_IDS.add(id);
|
||||
await saveData();
|
||||
}
|
||||
|
||||
return !has;
|
||||
}
|
||||
|
||||
static async removeAdmin(id: number): Promise<boolean> {
|
||||
const has = this.ADMIN_IDS.has(id);
|
||||
if (has) {
|
||||
this.ADMIN_IDS.delete(id);
|
||||
await saveData();
|
||||
}
|
||||
|
||||
return has;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export type MessagePart = {
|
||||
bot: boolean;
|
||||
name?: string;
|
||||
content: string;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import {StoredMessage} from "../model/stored-message";
|
||||
import {Message} from "typescript-telegram-bot-api";
|
||||
import {extractTextMessage} from "../util/utils";
|
||||
import {Environment} from "./environment";
|
||||
import {messageDao} from "../index";
|
||||
|
||||
export class MessageStore {
|
||||
private static map = new Map<string, StoredMessage>();
|
||||
|
||||
private static key(chatId: number, messageId: number) {
|
||||
return `${chatId}:${messageId}`;
|
||||
}
|
||||
|
||||
static all(): Map<string, StoredMessage> {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
static async put(m: Message, prefix: string = Environment.BOT_PREFIX) {
|
||||
const msg: StoredMessage = {
|
||||
chatId: m.chat.id,
|
||||
messageId: m.message_id,
|
||||
replyToMessageId: m.reply_to_message?.message_id ?? null,
|
||||
fromId: m.from.id,
|
||||
text: extractTextMessage(m, prefix),
|
||||
date: m.date ?? 0,
|
||||
};
|
||||
|
||||
this.map.set(this.key(m.chat.id, m.message_id), msg);
|
||||
|
||||
await messageDao.insert(messageDao.mapTo([m]));
|
||||
}
|
||||
|
||||
static async get(chatId: number, messageId: number): Promise<StoredMessage | null> {
|
||||
const message = await messageDao.getById({chatId: chatId, id: messageId});
|
||||
if (!message) return null;
|
||||
|
||||
this.map.set(this.key(message.chatId, messageId), message);
|
||||
return message;
|
||||
}
|
||||
|
||||
static clear() {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import {User} from "typescript-telegram-bot-api";
|
||||
import {userDao} from "../index";
|
||||
import {StoredUser} from "../model/stored-user";
|
||||
|
||||
export class UserStore {
|
||||
private static map = new Map<number, StoredUser>();
|
||||
|
||||
static all(): Map<number, StoredUser> {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
static async put(u: User) {
|
||||
const user: StoredUser = {
|
||||
id: u.id,
|
||||
isBot: u.is_bot,
|
||||
firstName: u.first_name,
|
||||
lastName: u.last_name,
|
||||
userName: u.username,
|
||||
isPremium: u.is_premium,
|
||||
};
|
||||
|
||||
this.map.set(u.id, user);
|
||||
|
||||
await userDao.insert(userDao.mapTo([u]));
|
||||
}
|
||||
|
||||
static async get(id: number): Promise<StoredUser | null> {
|
||||
const user = await userDao.getById({id: id});
|
||||
if (!user) return null;
|
||||
|
||||
this.map.set(id, user);
|
||||
return user;
|
||||
}
|
||||
|
||||
static clear() {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user