mic-bot/bot/normal_mode/init.ts

56 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-10-19 10:47:33 +00:00
import { Bot } from "https://deno.land/x/grammy/mod.ts";
2024-10-24 08:35:13 +00:00
import { Kysely } from 'npm:kysely';
2024-10-19 10:00:35 +00:00
import { Ctx } from "../ctx.ts";
2024-10-19 10:47:33 +00:00
import { CompiledConfig } from "../../cfg/config.ts";
2024-10-19 10:00:35 +00:00
import { Database } from "../../repo/exports.ts";
2024-10-23 16:09:10 +00:00
import { checkUser, checkUserOnNewChatMember } from "./user_managment.ts";
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
import { checkUserOnStart } from "./user_managment.ts";
2024-10-19 10:00:35 +00:00
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
2024-10-24 08:35:13 +00:00
console.log(`initializing normal mode`)
2024-10-23 16:09:10 +00:00
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? */ }
})
2024-10-24 08:35:13 +00:00
bot.on('message:text', async ctx => {
console.log(`From: ${ctx.from.id} Message: ${ctx.msg.text}`)
2024-10-23 16:09:10 +00:00
if (ctx.from.is_bot || ctx.hasCommand('start')) return
if (ctx.chat.id === botCfg.chat_id) {
2024-10-24 08:35:13 +00:00
await checkUser(ctx, db, botCfg, true)
2024-10-23 16:09:10 +00:00
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
2024-10-24 08:35:13 +00:00
await checkCaptchaSolution(ctx, db, botCfg)
2024-10-23 16:09:10 +00:00
}
})
bot.command('start', async ctx => {
if (!ctx.from || ctx.from.is_bot) return
if (ctx.chat.type !== 'private') return
2024-10-24 08:35:13 +00:00
console.log(`Start called by ${ctx.from.id}`)
2024-10-23 16:09:10 +00:00
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)
}
})
2024-10-19 10:00:35 +00:00
}