import { Bot, Context, session } from "https://deno.land/x/grammy/mod.ts"; import { BotError } from "https://deno.land/x/grammy@v1.30.0/bot.ts"; import { Ctx, defaultSessionData } from "./ctx.ts"; import * as config from "../cfg/exports.ts" import { ERR_CODES, Err } from "../utils/errors.ts"; class BotUnknownRuntimeErr extends Err { code: ERR_CODES = ERR_CODES.UnknownErr; override cause?: BotError; } class BotUnknownOnStartErr extends Err { code: ERR_CODES = ERR_CODES.UnknownErr; } export const runBot = async (cfg: config.BotConfig, initMode: (bot: Bot) => void) => { // Bot initialization & setup const bot = new Bot(cfg.bot_token) bot.use(session({ initial: defaultSessionData })) bot.catch((err: BotError) => { throw new BotUnknownRuntimeErr("Unknown error while running the bot", { cause: err }) }) initMode(bot) console.log("Starting bot") await bot.start().catch(err => { throw new BotUnknownOnStartErr("Unknown error while starting the bot", { cause: err }) }) }