Fixes I guess?
This commit is contained in:
parent
d545318aed
commit
ce6802faff
24
bot/bot.ts
24
bot/bot.ts
@ -1,13 +1,8 @@
|
||||
import { Bot, Context, session } from "https://deno.land/x/grammy/mod.ts";
|
||||
import { BotError } from "https://deno.land/x/grammy@v1.30.0/bot.ts";
|
||||
import { Bot, Context, session, BotError, GrammyError, HttpError } from "https://deno.land/x/grammy/mod.ts";
|
||||
import { Ctx, defaultSessionData } from "./ctx.ts";
|
||||
import * as config from "../cfg/config.ts"
|
||||
import { ERR_CODES, Err } from "../utils/errors.ts";
|
||||
|
||||
class BotUnknownRuntimeErr extends Err {
|
||||
code: ERR_CODES = ERR_CODES.UnknownErr;
|
||||
override cause?: BotError<Context>;
|
||||
}
|
||||
class BotUnknownOnStartErr extends Err {
|
||||
code: ERR_CODES = ERR_CODES.UnknownErr;
|
||||
}
|
||||
@ -16,16 +11,23 @@ export const runBot = async (cfg: config.BotConfig, initMode: (bot: Bot<Ctx>) =>
|
||||
const bot = new Bot<Ctx>(cfg.bot_token)
|
||||
bot.use(session({ initial: defaultSessionData }))
|
||||
bot.catch((err: BotError<Context>) => {
|
||||
throw new BotUnknownRuntimeErr("Unknown error while running the bot",
|
||||
const ctx = err.ctx;
|
||||
console.error(`Error while handling update ${ctx.update.update_id}:`);
|
||||
const e = err.error;
|
||||
if (e instanceof GrammyError) {
|
||||
if (e.error_code === 400 && e.method === "deleteMessage") return
|
||||
console.error("Error in request:", e.description);
|
||||
} else if (e instanceof HttpError) {
|
||||
console.error("Could not contact Telegram:", e);
|
||||
} else {
|
||||
throw new BotUnknownOnStartErr("Unknown error while starting the bot",
|
||||
{ cause: err })
|
||||
}
|
||||
})
|
||||
|
||||
initMode(bot)
|
||||
await bot.init()
|
||||
|
||||
console.log(`starting bot ${bot.botInfo.username}`)
|
||||
await bot.start().catch(err => {
|
||||
throw new BotUnknownOnStartErr("Unknown error while starting the bot",
|
||||
{ cause: err })
|
||||
})
|
||||
await bot.start().catch(err => console.log(err))
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { Filter } from "https://deno.land/x/grammy@v1.30.0/mod.ts";
|
||||
import { Filter } from "https://deno.land/x/grammy/mod.ts";
|
||||
import { BotConfig } from "../../cfg/config.ts";
|
||||
import { Ctx } from "../ctx.ts";
|
||||
import { LangManager } from "../lang/export.ts";
|
||||
import { Kysely } from 'npm:kysely';
|
||||
import { Database } from "../../repo/exports.ts";
|
||||
import { checkUserRestrictions, getActiveInviteLink } from "../../core/users.ts";
|
||||
import { CheckUserOut } from "./user_managment.ts";
|
||||
import { CheckUserOut, type CheckUserOnStartOut } from "./user_managment.ts";
|
||||
|
||||
const CAPTCHA_FAILS_LIMIT = 3
|
||||
const TIMEOUT_AFTER_FAIL_MINS = 5
|
||||
@ -46,11 +46,10 @@ const isSolutionCorrect = (expectSolution: string, solution: string): boolean =>
|
||||
|
||||
|
||||
const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) => {
|
||||
if (!ctx.from) return
|
||||
if (ctx.chatId && ctx.session.captcha_data) {
|
||||
ctx.api.deleteMessage(
|
||||
await ctx.api.deleteMessage(
|
||||
ctx.chatId, ctx.session.captcha_data.message_id
|
||||
).catch()
|
||||
)
|
||||
}
|
||||
ctx.session.captcha_data = undefined
|
||||
|
||||
@ -73,32 +72,21 @@ const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) =>
|
||||
}).execute()
|
||||
})
|
||||
|
||||
ctx.reply(LangManager.getLang(ctx.from.language_code)
|
||||
ctx.reply(LangManager.getLang(ctx.from!.language_code)
|
||||
.replies.captcha.passed(link.invite_link))
|
||||
ctx.session.captcha_data = undefined
|
||||
}
|
||||
|
||||
|
||||
const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOut, cfg: BotConfig) => {
|
||||
if (!ctx.msg || !ctx.msg.from) return
|
||||
const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => {
|
||||
if (user.isChatParticipant) {
|
||||
ctx.reply(LangManager.getLang(ctx.msg.from.language_code)
|
||||
ctx.reply(LangManager.getLang(ctx.from!.language_code)
|
||||
.replies.captcha.already_in_chat)
|
||||
return
|
||||
}
|
||||
if (user.isCaptchaSolved) {
|
||||
let activeLink: string | undefined;
|
||||
await db.transaction().execute(async trx => {
|
||||
activeLink = await getActiveInviteLink(trx, ctx.msg!.from!.id)
|
||||
})
|
||||
if (activeLink) {
|
||||
ctx.reply(activeLink)
|
||||
return
|
||||
}
|
||||
} else if (user.activeInviteLink) {
|
||||
await ctx.reply(user.activeInviteLink)
|
||||
} else if (user.isCaptchaSolved) {
|
||||
await captchaPassed(ctx, db, cfg)
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
const captcha = new Captcha()
|
||||
const msg = await ctx.reply(captcha._text)
|
||||
ctx.session.captcha_data = {
|
||||
@ -107,6 +95,7 @@ const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOu
|
||||
generated_at: captcha._generated_at,
|
||||
solution: captcha._solution.toString(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -116,18 +105,19 @@ const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely
|
||||
const users = await db.selectFrom('users').selectAll().where('tg_id', '=', ctx.msg.from.id).execute()
|
||||
if (users.length < 1) return // TODO: Maybe create new user here, idk
|
||||
|
||||
const userRestrictions = await checkUserRestrictions(db, users[0])
|
||||
const user = users[0]
|
||||
const userRestrictions = await checkUserRestrictions(db, user)
|
||||
if (userRestrictions.isBlocked || userRestrictions.isTimeout) return
|
||||
|
||||
if (isSolutionCorrect(ctx.session.captcha_data!.solution, ctx.message.text)) {
|
||||
await captchaPassed(ctx, db, cfg)
|
||||
} else {
|
||||
ctx.session.captcha_data!.tries_failed++
|
||||
ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id).catch()
|
||||
await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id)
|
||||
if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) {
|
||||
ctx.reply(LangManager.getLang(ctx.msg.from.language_code)
|
||||
.replies.captcha.failed(TIMEOUT_AFTER_FAIL_MINS))
|
||||
ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id).catch()
|
||||
await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id)
|
||||
// TODO: Add timeout
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
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 { CompiledConfig } from "../../cfg/config.ts";
|
||||
import { Database } from "../../repo/exports.ts";
|
||||
import { checkUser, checkUserOnNewChatMember, onMemberLeftChat } from "./user_managment.ts";
|
||||
import { CompiledConfig } from "../../cfg/config.ts";
|
||||
import { checkUser, checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart } from "./user_managment.ts";
|
||||
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.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,
|
||||
@ -52,19 +51,15 @@ export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) =
|
||||
bot.command('start', async ctx => {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
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
|
||||
)
|
||||
const { old_chat_member, new_chat_member } = ctx.chatMember
|
||||
const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member)
|
||||
|
||||
if (chatMemberUpdateStatus.joined) {
|
||||
console.log(`New Chat member ${ctx.chatMember.from.id}:'+
|
||||
|
@ -93,9 +93,20 @@ export const checkUserOnNewChatMember = async (
|
||||
expectedUserTgId: -1,
|
||||
}
|
||||
|
||||
const userInfo = await checkUser(ctx, db, cfg, true)
|
||||
let userInfo: CheckUserOut
|
||||
await db.transaction().execute(async trx => {
|
||||
userInfo = await checkUser(ctx, trx, cfg, true)
|
||||
if (userInfo.isBlocked || userInfo.isNewUser) {
|
||||
return
|
||||
}
|
||||
await trx.updateTable('users').where('tg_id', '=', ctx.from.id).set({
|
||||
is_chat_participant: true
|
||||
}).execute()
|
||||
})
|
||||
userInfo = userInfo!
|
||||
|
||||
if (userInfo.isBlocked) {
|
||||
ctx.banChatMember(ctx.chatMember.from.id).catch(
|
||||
await ctx.banChatMember(ctx.chatMember.from.id).catch(
|
||||
err => console.log(err)
|
||||
)
|
||||
out.isBlocked = true
|
||||
@ -135,43 +146,50 @@ export const checkUserOnNewChatMember = async (
|
||||
|
||||
|
||||
|
||||
export interface CheckUserOnStartOut extends CheckUserOut {
|
||||
activeInviteLink: string | null
|
||||
}
|
||||
export const checkUserOnStart = async (
|
||||
ctx: CommandContext<Ctx>,
|
||||
db: Kysely<Database>,
|
||||
): Promise<CheckUserOut> => {
|
||||
): Promise<CheckUserOnStartOut> => {
|
||||
let isBlocked: boolean = false
|
||||
let isTimeout: boolean = false
|
||||
let isNewUser: boolean = false
|
||||
let isChatParticipant: boolean = false
|
||||
let isCaptchaSolved: boolean = false
|
||||
let activeInviteLink: string | null = null
|
||||
|
||||
await db.transaction().execute(async trx => {
|
||||
const users = await trx.selectFrom('users')
|
||||
.select([
|
||||
'id',
|
||||
'tg_id',
|
||||
'is_captcha_passed',
|
||||
'is_banned',
|
||||
'is_timeout',
|
||||
'is_chat_participant',
|
||||
'timeout_until'
|
||||
])
|
||||
.where('tg_id', '=', ctx.msg.from!.id).execute()
|
||||
.where('tg_id', '=', ctx.from!.id)
|
||||
.leftJoin('invite_links as il', join => join.onRef(
|
||||
'il.expect_user_tg_id', '=', 'users.tg_id'
|
||||
)).selectAll().execute()
|
||||
if (users.length < 1) {
|
||||
// TODO -- referal links support
|
||||
// const referal = ctx.match
|
||||
|
||||
await trx.insertInto('users').values({
|
||||
tg_id: ctx.msg.from?.id
|
||||
tg_id: ctx.from!.id
|
||||
}).execute()
|
||||
isNewUser = true
|
||||
} else {
|
||||
const now = new Date()
|
||||
const user = users[0]
|
||||
const userRestrictions = await checkUserRestrictions(db, user)
|
||||
isBlocked = userRestrictions.isBlocked
|
||||
isTimeout = userRestrictions.isTimeout
|
||||
isChatParticipant = user.is_chat_participant
|
||||
isCaptchaSolved = user.is_captcha_passed
|
||||
// TODO: move invite link related values
|
||||
// (eg. valid_until and link) into it's own namespace
|
||||
// if possible
|
||||
if (user.valid_until && user.valid_until < now) {
|
||||
await trx.deleteFrom('invite_links').where('link', '=', user.link).execute()
|
||||
} else {
|
||||
activeInviteLink = user.link
|
||||
}
|
||||
}
|
||||
})
|
||||
return {
|
||||
@ -179,7 +197,8 @@ export const checkUserOnStart = async (
|
||||
isTimeout,
|
||||
isNewUser,
|
||||
isChatParticipant,
|
||||
isCaptchaSolved
|
||||
isCaptchaSolved,
|
||||
activeInviteLink
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,11 @@ import { CompiledConfig } from "../../cfg/config.ts";
|
||||
import { Ctx } from "../ctx.ts";
|
||||
|
||||
export const init = (bot: Bot<Ctx>, _: CompiledConfig) => {
|
||||
bot.command("getchat", ctx => {
|
||||
bot.command("getchat", async ctx => {
|
||||
if (!ctx.from || !ctx.message) return
|
||||
if (ctx.message.chat.type != "group"
|
||||
&& ctx.message.chat.type != "supergroup") return
|
||||
ctx.deleteMessage()
|
||||
await ctx.deleteMessage()
|
||||
console.log(`Chat ${ctx.message.chat.title} ID: ${ctx.message.chat.id}`)
|
||||
})
|
||||
|
||||
|
@ -150,7 +150,8 @@
|
||||
}
|
||||
},
|
||||
"redirects": {
|
||||
"https://deno.land/x/grammy/mod.ts": "https://deno.land/x/grammy@v1.30.0/mod.ts"
|
||||
"https://deno.land/x/grammy/mod.ts": "https://deno.land/x/grammy@v1.30.0/mod.ts",
|
||||
"https://deno.land/x/grammy_types/manage.ts": "https://deno.land/x/grammy_types@v3.14.0/manage.ts"
|
||||
},
|
||||
"remote": {
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/dialect/database-introspector.js": "dce8b6ada28af6b75864ca3fcc2c61bbacb8976def6f715b2f96c5aa7a9331ce",
|
||||
@ -193,7 +194,6 @@
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/expression/expression-builder.js": "da9c44985b130c4d9ebd2dabff36b66e748e5afa8a785313e0d9318967ef24d7",
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/expression/expression-wrapper.js": "35742f42558a1cbb369ecf7b3cb863f4486fa0c2384fb23e278244440f3c12c9",
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/expression/expression.js": "7c024b0c9f292bbd0aefe486c81328d7b18ece24cb569afc3171e06035f4f970",
|
||||
"npm:kysely": "ba2b09d8e5e4ae121ef77cfd971477858381650c26bcf28dfd3a8e5ed0535e8a",
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/kysely.js": "d6b10df0da4bb8d853d1ff946b8a1a5e4c254c7a2e26d3a5ecacce82c9e3c7eb",
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/migration/file-migration-provider.js": "04be6f4d0bb587f254b270875b529bbf30bbdf19af2d95b06272cd5e6b01e56e",
|
||||
"https://cdn.jsdelivr.net/npm/kysely/dist/esm/migration/migrator.js": "35c171e46ae9d18b9b9459148f626be5bc88f22ee5f7844b9b6818dd53ecb77a",
|
||||
@ -530,7 +530,8 @@
|
||||
"https://deno.land/x/postgres@v0.17.0/query/transaction.ts": "8e75c3ce0aca97da7fe126e68f8e6c08d640e5c8d2016e62cee5c254bebe7fe8",
|
||||
"https://deno.land/x/postgres@v0.17.0/query/types.ts": "a6dc8024867fe7ccb0ba4b4fa403ee5d474c7742174128c8e689c3b5e5eaa933",
|
||||
"https://deno.land/x/postgres@v0.17.0/utils/deferred.ts": "dd94f2a57355355c47812b061a51b55263f72d24e9cb3fdb474c7519f4d61083",
|
||||
"https://deno.land/x/postgres@v0.17.0/utils/utils.ts": "19c3527ddd5c6c4c49ae36397120274c7f41f9d3cbf479cb36065d23329e9f90"
|
||||
"https://deno.land/x/postgres@v0.17.0/utils/utils.ts": "19c3527ddd5c6c4c49ae36397120274c7f41f9d3cbf479cb36065d23329e9f90",
|
||||
"npm:kysely": "ba2b09d8e5e4ae121ef77cfd971477858381650c26bcf28dfd3a8e5ed0535e8a"
|
||||
},
|
||||
"workspace": {
|
||||
"dependencies": [
|
||||
|
Loading…
Reference in New Issue
Block a user