diff --git a/src/functions/start.ts b/src/functions/start.ts index c6928e0..ba969b6 100644 --- a/src/functions/start.ts +++ b/src/functions/start.ts @@ -16,8 +16,8 @@ export const startLoop = async (init = false) => { try { if (!operatorName) { logger.error(`No operator name given`); - process.exit(3); - // return; + await new Promise((resolve) => setTimeout(resolve, 15000)); + return; } if (init) { logger.info(`System ready, listening for state changes of ${operatorName} validators...`); @@ -28,8 +28,8 @@ export const startLoop = async (init = false) => { if (!operatorIndex) { logger.error(`Operator ${operatorName} unknown`); - process.exit(3); - // return; + await new Promise((resolve) => setTimeout(resolve, 15000)); + return; } const validatorChain = validatorChains.find((e) => e.key == kapiStatus.chainId); diff --git a/src/index.ts b/src/index.ts index c6cb43a..f5924db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,27 +2,47 @@ import schedule from "node-schedule"; import bot from "./config/bot"; import logger from "./config/logger"; import { startLoop } from "./functions/start"; +import { GrammyError } from "grammy"; logger.info(`Starting Slashing Guard (with log level: ${logger.level})`); bot.start({ onStart: async (info) => { logger.info(`Starting Telegram bot @${info.username}`); - await bot.api.setMyCommands([ - { - command: "start", - description: `Start bot ${info.username}`, - }, - { command: "status", description: `Show service status` }, - { command: "operator", description: `Show operator status` }, - { - command: "stop", - description: `Stop bot ${info.username}`, - }, - ]); - await startLoop(true); - schedule.scheduleJob("*/12 * * * * *", async () => { - await startLoop(); - }); + try { + await bot.api.setMyCommands([ + { + command: "start", + description: `Start bot ${info.username}`, + }, + { command: "status", description: `Show service status` }, + { command: "operator", description: `Show operator status` }, + { + command: "stop", + description: `Stop bot ${info.username}`, + }, + ]); + await startLoop(true); + schedule.scheduleJob("*/12 * * * * *", async () => { + await startLoop(); + }); + } catch (error: unknown) { + if (error instanceof GrammyError) { + const errnum = error.error_code; + const errmsg = error.description; + const errmth = error.method; + const errmprm = error.parameters; + const bansec = errmprm.retry_after; + if (errnum == 429) { + logger.debug( + `App blocked by Telegram API for spamming too many requests - ${bansec} seconds left ([${errnum}]: ${errmsg})` + ); + } else { + logger.debug(`Grammy Error [${errnum}]: ${errmsg} (Method: ${errmth}] | Parameters: ${errmprm}])`); + } + } else { + logger.error(error); + } + } }, });