Working captcha concept.

This commit is contained in:
Nefrace 2024-07-29 23:07:04 +03:00
parent 587b98eb7e
commit 104fd180ab
1 changed files with 31 additions and 8 deletions

View File

@ -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 { if err != nil {
log.Println("newusers: can't add to db: ", err) 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) { func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
msg := u.Message msg := u.Message
captcha := Captcha{} 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 { if err != nil {
log.Println("Can't solve user captcha: ", err) 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 ban_minutes := 0
num, err := strconv.Atoi(msg.Text) num, err := strconv.Atoi(msg.Text)
text := "That's not a number. Try again in 30 minutes."
if err != nil { 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 ban_minutes = 30
} } else if num != captcha.CorrectAnswer {
text = "That's the wrong answer. Try again in 5 hours."
if num != captcha.CorrectAnswer {
b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text: "That's the wrong answer. Try again in 5 hours."})
ban_minutes = 300 ban_minutes = 300
} }
if ban_minutes > 0 { 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 { if err != nil {
log.Println("Can't block user from captcha: ", err) log.Println("Can't block user from captcha: ", err)
} }