From 7a3d70d1f6664051f51435fe253e5c0212785f35 Mon Sep 17 00:00:00 2001 From: Kurbanov Bulat Date: Wed, 12 Jan 2022 23:24:42 +0300 Subject: [PATCH] Fix manager --- src/bots/manager.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bots/manager.ts b/src/bots/manager.ts index f0d8182..9ccd0e5 100644 --- a/src/bots/manager.ts +++ b/src/bots/manager.ts @@ -43,7 +43,7 @@ async function _makeSyncRequest(): Promise { export default class BotsManager { static bots: {[key: number]: Telegraf} = {}; - static botsStates: {[key: number]: BotStatuses} = {}; + static botsStates: {[key: number]: BotState} = {}; static syncInterval: NodeJS.Timer | null = null; static server: Server | null = null; @@ -69,23 +69,23 @@ export default class BotsManager { static async updateBotState(state: BotState) { const isExists = this.bots[state.id] !== undefined; - if (isExists && this.botsStates[state.id] === state.status) { + if (isExists && + this.botsStates[state.id].status === state.status && + this.botsStates[state.id].cache === state.cache + ) { return; } - const bot = await getBot(state.token, state); - - this.bots[state.id] = bot; - this.botsStates[state.id] = state.status; - try { - const oldBot = new Telegraf(bot.telegram.token); + const oldBot = new Telegraf(state.token); await oldBot.telegram.deleteWebhook(); await oldBot.telegram.logOut(); } catch (e) { console.log(e); } + const bot = await getBot(state.token, state); + const dockerIp = await dockerIpTools.getContainerIp(); await bot.telegram.setWebhook( @@ -93,6 +93,9 @@ export default class BotsManager { ip_address: dockerIp, } ); + + this.bots[state.id] = bot; + this.botsStates[state.id] = state; } static async handleUpdate(req: Request, res: Response, next: NextFunction) {