diff --git a/bot/normal_mode/captcha.ts b/bot/normal_mode/captcha.ts index bc122b9..a446af9 100644 --- a/bot/normal_mode/captcha.ts +++ b/bot/normal_mode/captcha.ts @@ -54,8 +54,9 @@ const captchaPassed = async (ctx: Ctx, db: Kysely, cfg: BotConfig) => }) await db.transaction().execute(async trx => { - await trx.updateTable('users').where('tg_id', '=', ctx.from!.id) - .set({ is_captcha_passed: true }).execute() + await trx.updateTable('users').set({ + is_captcha_passed: true + }).where('tg_id', '=', ctx.from!.id).execute() await trx.insertInto('invite_links').values({ link: link.invite_link, @@ -70,7 +71,27 @@ const captchaPassed = async (ctx: Ctx, db: Kysely, cfg: BotConfig) => } -const initUserCaptcha = async (ctx: Ctx, db: Kysely, user: CheckUserOnStartOut, cfg: BotConfig) => { +const captchaFailed = async (ctx: Filter, db: Kysely) => { + console.log("Captcha solution failed") + ctx.session.captcha_data!.tries_failed++ + if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) { + const timeoutUntil = new Date() + timeoutUntil.setHours(timeoutUntil.getHours() + 12) + + await db.updateTable('users').set({ + timeout_until: timeoutUntil, + is_timeout: true, + }).where('tg_id', '=', ctx.from.id).execute() + + await ctx.reply(ctx.t("captcha-already-in-chat", + {timeout_mins: TIMEOUT_AFTER_FAIL_MINS})) + await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id) + } + await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id) +} + + +export const initUserCaptcha = async (ctx: Ctx, db: Kysely, user: CheckUserOnStartOut, cfg: BotConfig) => { if (user.isChatParticipant) { await ctx.reply(ctx.t("captcha-already-in-chat")) } else if (user.activeInviteLink) { @@ -91,7 +112,7 @@ const initUserCaptcha = async (ctx: Ctx, db: Kysely, user: CheckUserOn // returns true if captcha is response to a captcha; else -- returns false -const checkCaptchaSolution = async (ctx: Filter, db: Kysely, cfg: BotConfig) => { +export const checkCaptchaSolution = async (ctx: Filter, db: Kysely, cfg: BotConfig) => { if (!ctx.msg) return 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 @@ -103,17 +124,7 @@ const checkCaptchaSolution = async (ctx: Filter, db: Kysely if (isSolutionCorrect(ctx.session.captcha_data!.solution, ctx.message.text)) { await captchaPassed(ctx, db, cfg) } else { - ctx.session.captcha_data!.tries_failed++ - await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id) - if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) { - await ctx.reply(ctx.t("captcha-already-in-chat", - {timeout_mins: TIMEOUT_AFTER_FAIL_MINS})) - await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id) - // TODO: Add timeout - } + await captchaFailed(ctx, db) } - return } - -export { checkCaptchaSolution, initUserCaptcha } diff --git a/bot/normal_mode/user_managment.ts b/bot/normal_mode/user_managment.ts index 327e117..d56060b 100644 --- a/bot/normal_mode/user_managment.ts +++ b/bot/normal_mode/user_managment.ts @@ -57,7 +57,7 @@ export const checkUser = async ( await trx.updateTable('users').set({ is_chat_participant: true, is_captcha_passed: true, - }).execute() + }).where('tg_id', '=', user.tg_id).execute() } out.isBlocked = userRestrictions.isBlocked @@ -207,8 +207,7 @@ export const onMemberLeftChat = async ( ctx: Filter, db: Kysely, ) => { - await db.updateTable('users') - .where('tg_id', '=', ctx.chatMember.from.id).set({ + await db.updateTable('users').set({ is_chat_participant: false - }).execute() + }).where('tg_id', '=', ctx.chatMember.from.id).execute() }