32 lines
1.0 KiB
TypeScript
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/config.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) => {
|
|
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)
|
|
await bot.init()
|
|
|
|
console.log(`starting bot ${bot.botInfo.username}`)
|
|
await bot.start().catch(err => {
|
|
throw new BotUnknownOnStartErr("Unknown error while starting the bot",
|
|
{ cause: err })
|
|
})
|
|
}
|