mic-bot/bot/normal_mode/init.ts

96 lines
3.8 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/scheme.ts";
2024-10-25 17:35:12 +00:00
import { CompiledConfig } from "../../cfg/config.ts";
2024-10-28 22:12:12 +00:00
import { checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart, checkUserTrx } 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 {
2024-10-28 22:12:12 +00:00
joined: (oldMember.status === "left" || oldMember.status === "kicked")
&& (newMember.status !== "left" && newMember.status !== "kicked"),
left: (oldMember.status !== "left" && oldMember.status !== "kicked")
&& (newMember.status === "left" || newMember.status === "kicked"),
kicked: newMember.status === "kicked",
restricted: oldMember.status !== "restricted" && newMember.status === "restricted",
roleChanged: oldMember.status !== newMember.status
&& (newMember.status !== "left" && newMember.status !== "kicked")
2024-10-25 17:35:12 +00:00
};
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) => {
2024-10-28 18:00:03 +00:00
if (!ctx.from || ctx.from.is_bot) return
console.log('=======================================')
console.log(`New message: CID: ${ctx.msg.chat.id} `+
`From: ${ctx.from.first_name} ${ctx.from.last_name} `+
`UID: ${ctx.from.id} Locale: ${ctx.from.language_code}`)
console.log(ctx.msg.text)
2024-10-25 17:35:12 +00:00
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) {
2024-10-28 18:00:03 +00:00
console.log("Message was sent into the mic chat")
2024-10-28 22:12:12 +00:00
const userInfo = await checkUserTrx(ctx, db, cfg.botCfg, true)
2024-10-28 18:00:03 +00:00
console.log("User Info: ", userInfo)
2024-10-25 17:35:12 +00:00
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
2024-10-28 18:00:03 +00:00
console.log("Handling message as captcha solution")
2024-10-25 17:35:12 +00:00
await checkCaptchaSolution(ctx, db, botCfg)
}
2024-10-28 22:12:12 +00:00
await next()
2024-10-25 17:35:12 +00:00
})
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)
2024-10-28 18:00:03 +00:00
console.log("User clicked start. User Info: ", userInfo)
2024-10-25 17:35:12 +00:00
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) => {
2024-10-28 22:12:12 +00:00
if (ctx.chatMember.from.is_bot) return
2024-10-25 17:35:12 +00:00
if (ctx.chat.id !== botCfg.chat_id) return
2024-10-27 18:29:26 +00:00
2024-10-25 17:35:12 +00:00
const { old_chat_member, new_chat_member } = ctx.chatMember
const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member)
2024-10-28 22:12:12 +00:00
console.log(chatMemberUpdateStatus)
2024-10-24 13:19:25 +00:00
2024-10-25 17:35:12 +00:00
if (chatMemberUpdateStatus.joined) {
2024-10-28 22:12:12 +00:00
const userInfo = await checkUserOnNewChatMember(ctx, db)
console.log(
`New chat member ${ctx.chatMember.from.id}: `+
`${ctx.from.first_name} ${ctx.from.last_name}`
)
console.log("User info: ", userInfo)
ctx.reply("Welcome!") // TODO: Change message
} else if (chatMemberUpdateStatus.left) {
console.log(
`Chat member left ${ctx.chatMember.from.id}: `+
`${ctx.from.first_name} ${ctx.from.last_name}`
)
2024-10-25 17:35:12 +00:00
await onMemberLeftChat(ctx, db)
2024-10-28 22:12:12 +00:00
ctx.reply("pobeg s durki :(") // TODO: Change message
2024-10-25 17:35:12 +00:00
}
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
}