45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import {Command} from "../base/command";
|
|
import {FileOptions, Message} from "typescript-telegram-bot-api";
|
|
import {Requirements} from "../base/requirements";
|
|
import {Requirement} from "../base/requirement";
|
|
import {Environment} from "../common/environment";
|
|
import fs from "node:fs";
|
|
import {logError, replyToMessage, sendErrorPlaceholder} from "../util/utils";
|
|
import {bot} from "../index";
|
|
import {enqueueTelegramApiCall} from "../util/telegram-api-queue";
|
|
|
|
export class ExportDb extends Command {
|
|
|
|
command = ["exportdb"];
|
|
|
|
argsMode = "none" as const;
|
|
|
|
requirements = Requirements.Build(Requirement.BOT_CREATOR);
|
|
|
|
async execute(msg: Message): Promise<void> {
|
|
const fullPath = Environment.DB_PATH.substring(5);
|
|
if (!fs.existsSync(fullPath)) {
|
|
await sendErrorPlaceholder(msg);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await enqueueTelegramApiCall(
|
|
() => bot.sendDocument({
|
|
chat_id: Environment.CREATOR_ID,
|
|
document: new FileOptions(fs.createReadStream(fullPath), {filename: "database.db", contentType: "application/sql"}),
|
|
caption: Environment.databaseBackupCaption,
|
|
}),
|
|
{method: "sendDocument", chatId: Environment.CREATOR_ID, chatType: "private"}
|
|
);
|
|
|
|
if (msg.chat.id !== Environment.CREATOR_ID) {
|
|
await replyToMessage({message: msg, text: Environment.databaseBackupSentText});
|
|
}
|
|
} catch (e) {
|
|
logError(e);
|
|
await sendErrorPlaceholder(msg);
|
|
}
|
|
}
|
|
}
|