mic-bot/bot/normal_mode/init.ts

83 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-10-25 17:35:12 +00:00
import { ChatMember } from "https://deno.land/x/grammy_types/manage.ts";
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";
import { Database } from "../../repo/exports.ts";
2024-10-25 17:35:12 +00:00
import { CompiledConfig } from "../../cfg/config.ts";
import { checkUser, checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart } from "./user_managment.ts";
2024-10-23 16:09:10 +00:00
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
2024-10-26 16:21:50 +00:00
import { handleSafebooruQuery } from "./safebooru.ts";
2024-10-24 13:19:25 +00:00
interface ChatMemberUpdateStatus {
joined: boolean,
left: boolean,
kicked: boolean,
restricted: boolean,
roleChanged: boolean
}
function getChatMemberUpdateStatus(oldMember: ChatMember, newMember: ChatMember): ChatMemberUpdateStatus {
2024-10-25 17:35:12 +00:00
return {
joined: oldMember.status === "left" &&
newMember.status !== "left" && newMember.status !== "kicked",
left: oldMember.status !== "left" && newMember.status === "left",
kicked: newMember.status === "kicked",
restricted: oldMember.status !== "restricted" && newMember.status === "restricted",
roleChanged: oldMember.status !== newMember.status &&
(newMember.status !== "left" && newMember.status !== "kicked")
};
2024-10-24 13:19:25 +00:00
}
2024-10-19 10:00:35 +00:00
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
2024-10-25 17:35:12 +00:00
console.log(`initializing normal mode`)
const { botCfg } = cfg
2024-10-23 16:09:10 +00:00
2024-10-25 17:35:12 +00:00
bot.on('message', async (ctx, next) => {
if (ctx.from?.is_bot) return
console.log(`Chat ID: ${ctx.msg.chat.id} From: ${ctx.from.id} Message: ${ctx.msg.text}`)
await next()
})
2024-10-23 16:09:10 +00:00
2024-10-25 17:35:12 +00:00
bot.on('message:text', async (ctx, next) => {
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)
} else {
await next()
}
})
2024-10-23 16:09:10 +00:00
2024-10-25 17:35:12 +00:00
bot.command('start', async ctx => {
const userInfo = await checkUserOnStart(ctx, db)
if (userInfo.isBlocked || userInfo.isTimeout) return
// TODO: if (userInfo.isNewUser) { /* Some hello message? */ }
await initUserCaptcha(ctx, db, userInfo, botCfg)
})
2024-10-23 16:09:10 +00:00
2024-10-25 17:35:12 +00:00
bot.on('chat_member', async (ctx, next) => {
if (ctx.from.is_bot) return
if (ctx.chat.id !== botCfg.chat_id) return
const { old_chat_member, new_chat_member } = ctx.chatMember
const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member)
2024-10-24 13:19:25 +00:00
2024-10-25 17:35:12 +00:00
if (chatMemberUpdateStatus.joined) {
console.log(`New Chat member ${ctx.chatMember.from.id}:'+
'${ctx.from.first_name} ${ctx.from.last_name}`)
await checkUserOnNewChatMember(ctx, db, botCfg)
// TODO:
// const userInfo = await checkUserOnNewChatMember(ctx, db, botCfg)
//if (userInfo.isNewUser) { /* Some hello message? */ }
} else if (chatMemberUpdateStatus.left) {
console.log(`Chat member left ${ctx.chatMember.from.id}:'+
'${ctx.from.first_name} ${ctx.from.last_name}`)
await onMemberLeftChat(ctx, db)
// TODO: /* POBEG S DURKI! */
}
await next()
})
2024-10-26 16:21:50 +00:00
2024-10-27 09:12:11 +00:00
bot.inlineQuery(/sfbr */, async ctx => { await handleSafebooruQuery(ctx) })
2024-10-25 17:35:12 +00:00
}