From efc23049a8e3aba42f805f8145648aa00feb3963 Mon Sep 17 00:00:00 2001 From: Kurbanov Bulat Date: Sun, 2 Jan 2022 15:43:02 +0300 Subject: [PATCH] Fix setMyCommands --- src/bots/factory/bots/approved/index.ts | 24 +++++++++++++++++------- src/main.ts | 3 +-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bots/factory/bots/approved/index.ts b/src/bots/factory/bots/approved/index.ts index 14a2b9b..420e4bd 100644 --- a/src/bots/factory/bots/approved/index.ts +++ b/src/bots/factory/bots/approved/index.ts @@ -1,4 +1,4 @@ -import { Context, Telegraf, Markup } from 'telegraf'; +import { Context, Telegraf, Markup, TelegramError } from 'telegraf'; import { BotState, Cache } from '@/bots/manager'; @@ -25,12 +25,22 @@ export async function createApprovedBot(token: string, state: BotState): Promise } }); - await bot.telegram.setMyCommands([ - {command: "random", description: "Попытать удачу"}, - {command: "update_log", description: "Информация об обновлении каталога"}, - {command: "settings", description: "Настройки"}, - {command: "help", description: "Помощь"}, - ]); + async function setMyCommands() { + await bot.telegram.setMyCommands([ + {command: "random", description: "Попытать удачу"}, + {command: "update_log", description: "Информация об обновлении каталога"}, + {command: "settings", description: "Настройки"}, + {command: "help", description: "Помощь"}, + ]); + } + + try { + await setMyCommands(); + } catch (e: unknown) { + if (e instanceof TelegramError && e.response.error_code === 429) { + setTimeout(() => setMyCommands(), 1000 * (e.response.parameters?.retry_after || 5)); + } + } bot.use(async (ctx: Context, next) => { if (ctx.from) { diff --git a/src/main.ts b/src/main.ts index ea8d345..86916c7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,3 @@ import BotsManager from './bots/manager'; - -BotsManager.start(); +setTimeout(() => BotsManager.start(), 5000);