mic-bot/bot/normal_mode/init.ts
2024-10-24 10:35:13 +02:00

56 lines
1.9 KiB
TypeScript

import { Bot } from "https://deno.land/x/grammy/mod.ts";
import { Kysely } from 'npm:kysely';
import { Ctx } from "../ctx.ts";
import { CompiledConfig } from "../../cfg/config.ts";
import { Database } from "../../repo/exports.ts";
import { checkUser, checkUserOnNewChatMember } from "./user_managment.ts";
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
import { checkUserOnStart } from "./user_managment.ts";
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
console.log(`initializing normal mode`)
const { botCfg } = cfg
bot.on('message', ctx => {
if (ctx.from.is_bot) return
checkUser(ctx, db, botCfg)
})
bot.on('chat_member', async ctx => {
if (ctx.chatMember.from.is_bot || ctx.chat.id !== botCfg.chat_id) return
await checkUserOnNewChatMember(ctx, db, botCfg)
// TODO:
// const userInfo = await checkUserOnNewChatMember(ctx, db, botCfg)
//if (userInfo.isNewUser) { /* Some hello message? */ }
})
bot.on('message:text', async ctx => {
console.log(`From: ${ctx.from.id} Message: ${ctx.msg.text}`)
if (ctx.from.is_bot || ctx.hasCommand('start')) return
if (ctx.chat.id === botCfg.chat_id) {
await checkUser(ctx, db, botCfg, true)
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
await checkCaptchaSolution(ctx, db, botCfg)
}
})
bot.command('start', async ctx => {
if (!ctx.from || ctx.from.is_bot) return
if (ctx.chat.type !== 'private') return
console.log(`Start called by ${ctx.from.id}`)
const userInfo = await checkUserOnStart(ctx, db)
if (userInfo.isBlocked || userInfo.isTimeout) return
// TODO: if (userInfo.isNewUser) { /* Some hello message? */ }
if (!ctx.session.captcha_data) {
await initUserCaptcha(ctx, db, userInfo, botCfg)
}
})
}