User leaving/joining proper handling
This commit is contained in:
parent
c7e82c8212
commit
43bf90ebcc
@ -3,48 +3,53 @@ import { Kysely } from 'npm:kysely';
|
|||||||
import { Ctx } from "../ctx.ts";
|
import { Ctx } from "../ctx.ts";
|
||||||
import { CompiledConfig } from "../../cfg/config.ts";
|
import { CompiledConfig } from "../../cfg/config.ts";
|
||||||
import { Database } from "../../repo/exports.ts";
|
import { Database } from "../../repo/exports.ts";
|
||||||
import { checkUser, checkUserOnNewChatMember } from "./user_managment.ts";
|
import { checkUser, checkUserOnNewChatMember, onMemberLeftChat } from "./user_managment.ts";
|
||||||
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
|
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
|
||||||
import { checkUserOnStart } from "./user_managment.ts";
|
import { checkUserOnStart } from "./user_managment.ts";
|
||||||
|
import { ChatMember } from "https://deno.land/x/grammy_types@v3.14.0/manage.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" &&
|
||||||
|
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")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
|
export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) => {
|
||||||
console.log(`initializing normal mode`)
|
console.log(`initializing normal mode`)
|
||||||
const { botCfg } = cfg
|
const { botCfg } = cfg
|
||||||
|
|
||||||
bot.on('message', ctx => {
|
bot.on('message', async (ctx, next) => {
|
||||||
if (ctx.from.is_bot) return
|
if (ctx.from?.is_bot) return
|
||||||
checkUser(ctx, db, botCfg)
|
console.log(`Chat ID: ${ctx.msg.chat.id} From: ${ctx.from.id} Message: ${ctx.msg.text}`)
|
||||||
|
await next()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
bot.on('message:text', async (ctx, next) => {
|
||||||
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) {
|
if (ctx.chat.id === botCfg.chat_id) {
|
||||||
await checkUser(ctx, db, botCfg, true)
|
await checkUser(ctx, db, botCfg, true)
|
||||||
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
|
} else if (ctx.message.chat.type == "private" && ctx.session.captcha_data) {
|
||||||
await checkCaptchaSolution(ctx, db, botCfg)
|
await checkCaptchaSolution(ctx, db, botCfg)
|
||||||
|
} else {
|
||||||
|
await next()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
bot.command('start', async ctx => {
|
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)
|
const userInfo = await checkUserOnStart(ctx, db)
|
||||||
if (userInfo.isBlocked || userInfo.isTimeout) return
|
if (userInfo.isBlocked || userInfo.isTimeout) return
|
||||||
|
|
||||||
@ -53,4 +58,27 @@ export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) =
|
|||||||
await initUserCaptcha(ctx, db, userInfo, botCfg)
|
await initUserCaptcha(ctx, db, userInfo, botCfg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
bot.on('chat_member', async (ctx, next) => {
|
||||||
|
if (ctx.from.is_bot) return
|
||||||
|
if (ctx.chat.id !== botCfg.chat_id) return
|
||||||
|
const chatMemberUpdateStatus = getChatMemberUpdateStatus(
|
||||||
|
ctx.chatMember.old_chat_member, ctx.chatMember.new_chat_member
|
||||||
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
}
|
}
|
@ -182,3 +182,14 @@ export const checkUserOnStart = async (
|
|||||||
isCaptchaSolved
|
isCaptchaSolved
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const onMemberLeftChat = async (
|
||||||
|
ctx: Filter<Ctx, "chat_member">,
|
||||||
|
db: Kysely<Database>,
|
||||||
|
) => {
|
||||||
|
await db.updateTable('users')
|
||||||
|
.where('tg_id', '=', ctx.chatMember.from.id).set({
|
||||||
|
is_chat_participant: false
|
||||||
|
}).execute()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user