mic-bot/bot/normal_mode/init.ts

96 lines
3.8 KiB
TypeScript

import { ChatMember } from "https://deno.land/x/grammy_types/manage.ts";
import { Bot } from "https://deno.land/x/grammy/mod.ts";
import { Kysely } from 'npm:kysely';
import { Ctx } from "../ctx.ts";
import { Database } from "../../repo/scheme.ts";
import { CompiledConfig } from "../../cfg/config.ts";
import { checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart, checkUserTrx } from "./user_managment.ts";
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
import { handleSafebooruQuery } from "./safebooru.ts";
interface ChatMemberUpdateStatus {
joined: boolean,
left: boolean,
kicked: boolean,
restricted: boolean,
roleChanged: boolean
}
function getChatMemberUpdateStatus(oldMember: ChatMember, newMember: ChatMember): ChatMemberUpdateStatus {
return {
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")
};
}
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
console.log(`initializing normal mode`)
const { botCfg } = cfg
bot.on('message', async (ctx, next) => {
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)
await next()
})
bot.on('message:text', async (ctx, next) => {
if (ctx.chat.id === botCfg.chat_id) {
console.log("Message was sent into the mic chat")
const userInfo = await checkUserTrx(ctx, db, cfg.botCfg, true)
console.log("User Info: ", userInfo)
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
console.log("Handling message as captcha solution")
await checkCaptchaSolution(ctx, db, botCfg)
}
await next()
})
bot.command('start', async ctx => {
const userInfo = await checkUserOnStart(ctx, db)
console.log("User clicked start. User Info: ", userInfo)
if (userInfo.isBlocked || userInfo.isTimeout) return
// TODO: if (userInfo.isNewUser) { /* Some hello message? */ }
await initUserCaptcha(ctx, db, userInfo, botCfg)
})
bot.on('chat_member', async (ctx, next) => {
if (ctx.chatMember.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)
console.log(chatMemberUpdateStatus)
if (chatMemberUpdateStatus.joined) {
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}`
)
await onMemberLeftChat(ctx, db)
ctx.reply("pobeg s durki :(") // TODO: Change message
}
await next()
})
bot.inlineQuery(/sfbr */, async ctx => { await handleSafebooruQuery(ctx) })
}