mic-bot/bot/bot.ts

32 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-10-19 10:00:35 +00:00
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";
2024-10-23 16:09:10 +00:00
import * as config from "../cfg/config.ts"
2024-10-19 10:00:35 +00:00
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 })
})
}