Added ban

This commit is contained in:
Vlad Rud 2022-02-02 00:53:12 +03:00
parent 6eeaddfd37
commit 0fed3c1832
1 changed files with 11 additions and 7 deletions

View File

@ -52,24 +52,28 @@ var HandlersV1 = []Handler{
Handler: func(c tb.Context) error { Handler: func(c tb.Context) error {
sender := c.Sender() sender := c.Sender()
message := c.Message() message := c.Message()
bot := c.Bot()
d := db.GetDatabase() d := db.GetDatabase()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel() defer cancel()
if user, err := d.GetUser(ctx, db.User{Id: sender.ID, ChatId: message.Chat.ID, IsBanned: false}); err == nil { if user, err := d.GetUser(ctx, db.User{Id: sender.ID, ChatId: message.Chat.ID}); err == nil {
text := message.Text text := message.Text
solved := false
if num, err := strconv.Atoi(text); err == nil { if num, err := strconv.Atoi(text); err == nil {
if num == int(user.CorrectAnswer) { if num == int(user.CorrectAnswer) {
_ = d.RemoveUser(ctx, user) _ = d.RemoveUser(ctx, user)
c.Reply("Капча пройдена!") solved = true
c.Bot().Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage}) bot.Delete(message)
} else { bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
c.Reply("Ещё разочек")
} }
} else { } else {
log.Print(err) log.Print(err)
} }
} else { if !solved {
c.Bot().Ban(message.Chat, &tb.ChatMember{User: sender}) bot.Delete(message)
bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage})
bot.Ban(message.Chat, &tb.ChatMember{User: sender})
}
} }
return nil return nil
}, },