UNPKG

ams-ssk

Version:

NestJS AMS Library for file management

108 lines 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BotUtils = void 0; const config_1 = require("@nestjs/config"); class BotUtils { static initialize(config) { this.config = config; } static getChannelById(channelId) { return this.config?.channels.find((channel) => channel.channelId === channelId); } static getChannelsByBotToken(botToken) { return this.config?.channels.filter((channel) => channel.botTokens.includes(botToken)) || []; } static canBotAccessChannel(botToken, channelId) { const channel = this.getChannelById(channelId); return channel ? channel.botTokens.includes(botToken) : false; } static getBotsForChannel(channelId) { const channel = this.getChannelById(channelId); if (!channel) return []; return this.config?.bots.filter((bot) => channel.botTokens.includes(bot.token)) || []; } static getAllChannels() { return this.config?.channels || []; } static getAllBots() { return this.config?.bots || []; } static getAdminChatId() { return this.config?.adminChatId; } } exports.BotUtils = BotUtils; exports.default = (0, config_1.registerAs)('bot', () => { const maxOps = parseInt(process.env.bot_BOT_MAX_OPERATIONS || '10'); const adminChatId = process.env.bot_ADMIN_CHAT_ID; const channelConfigs = parseChannelConfigs(); if (!channelConfigs.length) { console.warn('No bot channel configurations found!'); } else { console.log('Parsed channel configurations:', channelConfigs.map((channel) => ({ channelId: channel.channelId, description: channel.description || 'no description', botCount: channel.botTokens.length, }))); } const uniqueBotTokens = new Set(); const botChannelMapping = new Map(); channelConfigs.forEach((channel) => { channel.botTokens.forEach((token) => { uniqueBotTokens.add(token); const channels = botChannelMapping.get(token) || []; channels.push(channel.channelId); botChannelMapping.set(token, channels); }); }); if (!uniqueBotTokens.size) { console.warn('No bot bot tokens found in channel configurations!'); } const config = { bots: Array.from(uniqueBotTokens).map((token) => ({ token: token.trim(), maxConcurrentOperations: maxOps, })), channels: channelConfigs, adminChatId, }; BotUtils.initialize(config); console.log('Loaded bot configuration:', { botsCount: config.bots.length, channelsCount: config.channels.length, botMappings: Array.from(botChannelMapping.entries()).map(([token, channels]) => ({ botToken: `${token.slice(0, 6)}...`, channels, })), hasAdminChatId: !!config.adminChatId, maxOpsPerBot: maxOps, }); return config; }); function parseChannelConfigs() { const channelConfigs = []; const envVars = Object.keys(process.env); const channelConfigVars = envVars.filter((key) => key.startsWith('TELEGRAM_CHANNEL_')); for (const configVar of channelConfigVars) { const channelConfig = process.env[configVar]; if (!channelConfig) continue; const [channelId, description, botTokensStr] = channelConfig.split('::'); if (!channelId || !botTokensStr) { console.warn(`Invalid channel configuration format in ${configVar}`); continue; } channelConfigs.push({ channelId: channelId.trim(), description: description?.trim(), botTokens: botTokensStr .split(',') .map((token) => token.trim()) .filter(Boolean), }); } return channelConfigs; } //# sourceMappingURL=bot.config.js.map