mic-bot/bot/bot.ts
2024-10-19 12:00:35 +02:00

32 lines
1.0 KiB
TypeScript

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<Context>;
}
class BotUnknownOnStartErr extends Err {
code: ERR_CODES = ERR_CODES.UnknownErr;
}
export const runBot = async (cfg: config.BotConfig, initMode: (bot: Bot<Ctx>) => void) => {
// Bot initialization & setup
const bot = new Bot<Ctx>(cfg.bot_token)
bot.use(session({ initial: defaultSessionData }))
bot.catch((err: BotError<Context>) => {
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 })
})
}