move config creation to config.js

This commit is contained in:
FullGreaM 2025-10-26 18:28:07 +03:00
parent 72f70d1258
commit 37e2b3e4f9
2 changed files with 52 additions and 24 deletions

View File

@ -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 <params> <container>",
);
}
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();

33
src/config.js Normal file
View File

@ -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 <params> <container>",
);
}
config.admins = Object.fromEntries(config.admins.map((x) => [x, true]));
config.whitelist = Object.fromEntries(config.whitelist.map((x) => [x, true]));
module.exports = config;