Fix manager

This commit is contained in:
2022-01-12 23:24:42 +03:00
parent 97f44459f8
commit 7a3d70d1f6

View File

@@ -43,7 +43,7 @@ async function _makeSyncRequest(): Promise<BotState[] | null> {
export default class BotsManager { export default class BotsManager {
static bots: {[key: number]: Telegraf} = {}; static bots: {[key: number]: Telegraf} = {};
static botsStates: {[key: number]: BotStatuses} = {}; static botsStates: {[key: number]: BotState} = {};
static syncInterval: NodeJS.Timer | null = null; static syncInterval: NodeJS.Timer | null = null;
static server: Server | null = null; static server: Server | null = null;
@@ -69,23 +69,23 @@ export default class BotsManager {
static async updateBotState(state: BotState) { static async updateBotState(state: BotState) {
const isExists = this.bots[state.id] !== undefined; 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; return;
} }
const bot = await getBot(state.token, state);
this.bots[state.id] = bot;
this.botsStates[state.id] = state.status;
try { try {
const oldBot = new Telegraf(bot.telegram.token); const oldBot = new Telegraf(state.token);
await oldBot.telegram.deleteWebhook(); await oldBot.telegram.deleteWebhook();
await oldBot.telegram.logOut(); await oldBot.telegram.logOut();
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
const bot = await getBot(state.token, state);
const dockerIp = await dockerIpTools.getContainerIp(); const dockerIp = await dockerIpTools.getContainerIp();
await bot.telegram.setWebhook( await bot.telegram.setWebhook(
@@ -93,6 +93,9 @@ export default class BotsManager {
ip_address: dockerIp, ip_address: dockerIp,
} }
); );
this.bots[state.id] = bot;
this.botsStates[state.id] = state;
} }
static async handleUpdate(req: Request, res: Response, next: NextFunction) { static async handleUpdate(req: Request, res: Response, next: NextFunction) {