From 104fd180abf12de7fda24d7754a5c5423ecabf96 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Mon, 29 Jul 2024 23:07:04 +0300 Subject: [PATCH] Working captcha concept. --- src/handlers.go | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/handlers.go b/src/handlers.go index c0c41a5..d63c2cb 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -80,9 +80,33 @@ func handleNewJoined(ctx context.Context, b *bot.Bot, u *models.Update) { }, }, }) - _, err := db.Exec(`INSERT INTO captchas (user_id, chat_id, message_id, correct_answer, blocked_until) values ($1, $2, $3, 0, 0)`, user.ID, u.Message.Chat.ID, msg.ID) + _, err := db.Exec(`INSERT INTO captchas (user_id, chat_id, message_id) values ($1, $2, $3)`, user.ID, u.Message.Chat.ID, msg.ID) if err != nil { log.Println("newusers: can't add to db: ", err) + return + } + _, err = b.RestrictChatMember(ctx, &bot.RestrictChatMemberParams{ + ChatID: u.Message.Chat.ID, + UserID: user.ID, + Permissions: &models.ChatPermissions{ + CanSendMessages: false, + CanSendAudios: false, + CanSendDocuments: false, + CanSendPhotos: false, + CanSendVideos: false, + CanSendVideoNotes: false, + CanSendVoiceNotes: false, + CanSendPolls: false, + CanSendOtherMessages: false, + CanAddWebPagePreviews: false, + CanChangeInfo: false, + CanInviteUsers: false, + CanPinMessages: false, + CanManageTopics: false, + }, + }) + if err != nil { + log.Println("Can't restrict new user: ", err) } } } @@ -149,7 +173,7 @@ func handlePrivateStartCaptcha(ctx context.Context, b *bot.Bot, u *models.Update func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) { msg := u.Message captcha := Captcha{} - err := db.Get(&captcha, "select * from captcha where user_id = $1 and correct_answer != 0", msg.From.ID) + err := db.Get(&captcha, "select * from captchas where user_id = $1 and correct_answer != 0", msg.From.ID) if err != nil { log.Println("Can't solve user captcha: ", err) @@ -164,18 +188,17 @@ func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) { ban_minutes := 0 num, err := strconv.Atoi(msg.Text) + text := "That's not a number. Try again in 30 minutes." if err != nil { - b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text: "That's not a number. Try again in 30 minutes."}) ban_minutes = 30 - } - - if num != captcha.CorrectAnswer { - b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text: "That's the wrong answer. Try again in 5 hours."}) + } else if num != captcha.CorrectAnswer { + text = "That's the wrong answer. Try again in 5 hours." ban_minutes = 300 } if ban_minutes > 0 { - _, err := db.Exec("update captcha set banned_until = $2 where id = $1", captcha.Id, time.Now().Add(time.Minute * time.Duration(ban_minutes)).Unix()) + b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text:text}) + _, err := db.Exec("update captchas set blocked_until = $2 where id = $1", captcha.Id, time.Now().Add(time.Minute * time.Duration(ban_minutes)).Unix()) if err != nil { log.Println("Can't block user from captcha: ", err) }