From 37e2b3e4f997fa2ecf285566cd84a79898e19a4b Mon Sep 17 00:00:00 2001 From: FullGreaM Date: Sun, 26 Oct 2025 18:28:07 +0300 Subject: [PATCH] move config creation to config.js --- src/bot.js | 43 +++++++++++++++++++------------------------ src/config.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 src/config.js diff --git a/src/bot.js b/src/bot.js index 57e7a83..96ae54a 100644 --- a/src/bot.js +++ b/src/bot.js @@ -2,8 +2,10 @@ const TelegramBot = require("node-telegram-bot-api"); const fs = require("fs"); const mime = require("mime"); const path = require("path"); -const { DownloadVideo, YtdlUpdater, downloadFile } = require("./download"); const https = require("https"); +const config = require("./config"); +const { DownloadVideo, YtdlUpdater, downloadFile } = require("./download"); +const { MTProtoUser } = require("./mtproto"); async function getImage(url) { return new Promise((resolve, reject) => { @@ -22,29 +24,6 @@ async function getImage(url) { }); } -const config = { - token: process.env.BOT_TOKEN, - admins: process.env.ADMIN_LIST?.split(/\s*,\s*/g).map((x) => +x) ?? [], - allFormats: false -}; -config.whitelist = [].concat( - process.env.WHITELIST?.split(/\s*,\s*/g).map((x) => +x) ?? [], - config.admins, -); -config.whitelist = Object.keys( - Object.fromEntries(config.whitelist.map((x) => [x, true])), -).map((x) => +x); - -console.info(config); -if (!process.env.BOT_TOKEN) { - throw new Error( - "BOT_TOKEN not defined. Please, run docker container: docker run --env-file .env ", - ); -} - -config.admins = Object.fromEntries(config.admins.map((x) => [x, true])); -config.whitelist = Object.fromEntries(config.whitelist.map((x) => [x, true])); - try { fs.rmdirSync(path.join(__dirname, "tmp"), { recursive: true, force: true }, () => {}); } catch (_) {} @@ -238,6 +217,21 @@ class TgBot { this.bot.on("callback_query", (m) => this.cbHandler(m)); this.videosGC = new VideosGarbageCollector(); this.updater = new YtdlUpdater(); + this.mtproto = null; + if (config.mtproto !== null) { + this.mtproto = new MTProtoUser( + config.mtproto.apiId, + config.mtproto.apiHash, + config.mtproto.session, + ); + } + } + + async init () { + if (this.mtproto === null) + console.warn("API_ID or API_HASH not defined. MTProto API not used"); + else + await this.mtproto.init(); } async cbHandler(query) { @@ -394,3 +388,4 @@ class TgBot { } const bot = new TgBot(); +bot.init(); diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..6b01747 --- /dev/null +++ b/src/config.js @@ -0,0 +1,33 @@ +const config = { + token: process.env.BOT_TOKEN, + admins: process.env.ADMIN_LIST?.split(/\s*,\s*/g).map((x) => +x) ?? [], + allFormats: false, + mtproto: { + apiId: process.env.API_ID, + apiHash: process.env.API_HASH, + session: process.env.API_SESSION, + }, +}; +config.whitelist = [].concat( + process.env.WHITELIST?.split(/\s*,\s*/g).map((x) => +x) ?? [], + config.admins, +); +config.whitelist = Object.keys( + Object.fromEntries(config.whitelist.map((x) => [x, true])), +).map((x) => +x); + +if (!config.mtproto.apiId || !config.mtproto.apiHash || !config.mtproto.session) { + config.mtproto = null; +} + +console.info(config); +if (!process.env.BOT_TOKEN) { + throw new Error( + "BOT_TOKEN not defined. Please, run docker container: docker run --env-file .env ", + ); +} + +config.admins = Object.fromEntries(config.admins.map((x) => [x, true])); +config.whitelist = Object.fromEntries(config.whitelist.map((x) => [x, true])); + +module.exports = config;