From 635f27f5ed5d981018b4ea027af6bca8435fe9bf Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sat, 9 Sep 2023 01:45:47 +0300 Subject: [PATCH] Not deleting users --- captchagen/captchagen.go | 4 ++-- db/structs.go | 1 + kicker/handlers.go | 16 +++++++++------- kicker/tasks.go | 8 +++----- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/captchagen/captchagen.go b/captchagen/captchagen.go index e5293d3..f313755 100644 --- a/captchagen/captchagen.go +++ b/captchagen/captchagen.go @@ -6,9 +6,9 @@ import ( "image" "image/color" "image/png" - "io/ioutil" "log" "math/rand" + "os" "strings" "github.com/fogleman/gg" @@ -104,7 +104,7 @@ func (captcha *Captcha) ToBytes() (*[]byte, error) { // Логотипы читаются из папки /assets рядом с исполняемым файлом. // Принимается формат .png, логотип, представляющий правильный ответ называется godot.png func Init() error { - files, err := ioutil.ReadDir("./assets") + files, err := os.ReadDir("./assets") if err != nil { return err } diff --git a/db/structs.go b/db/structs.go index b10b2a9..67e8b5c 100644 --- a/db/structs.go +++ b/db/structs.go @@ -30,6 +30,7 @@ type User struct { DateJoined time.Time `bson:"date_joined"` JoinedMessage int `bson:"joined_message"` LastNotification time.Time `bson:"last_notification"` + Warnings int `bson:"warnings"` } func (User) Coll() string { return "users" } diff --git a/kicker/handlers.go b/kicker/handlers.go index ef16289..982c893 100644 --- a/kicker/handlers.go +++ b/kicker/handlers.go @@ -15,10 +15,10 @@ import ( func userJoined(b *bot, update *echotron.Update) error { captcha := captchagen.GenCaptcha() - _, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID) - if err != nil { - log.Printf("Can't delete message: %v", err) - } + // _, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID) + // if err != nil { + // log.Printf("Can't delete message: %v", err) + // } bytes, err := captcha.ToBytes() if err != nil { log.Printf("Error creating captcha bytes: %v", bytes) @@ -67,7 +67,7 @@ func userLeft(b *bot, update *echotron.Update) error { store := tongo.NewStore[db.User](Client) ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() - if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil { + if user, err := store.GetOne(ctx, tongo.E("user_id", sender.ID), tongo.E("chat_id", message.Chat.ID), tongo.E("is_joined", false), tongo.E("is_banned", false)); err == nil { //d.GetUser(ctx, db.User{UserId: sender.ID, ChatId: message.Chat.ID}); err == nil { store.DeleteByID(ctx, user.Id) b.DeleteMessage(message.Chat.ID, message.ID) b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) @@ -102,7 +102,8 @@ func checkCaptcha(b *bot, update *echotron.Update) error { solved := false if num, err := strconv.Atoi(guess); err == nil { if num == int(user.CorrectAnswer) { - _ = store.DeleteByID(ctx, user.Id) + user.IsJoined = true + store.ReplaceItem(ctx, *user, true) solved = true b.DeleteMessage(message.Chat.ID, message.ID) b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) @@ -130,7 +131,8 @@ func checkCaptcha(b *bot, update *echotron.Update) error { b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) b.DeleteMessage(message.Chat.ID, user.JoinedMessage) b.BanChatMember(message.Chat.ID, sender.ID, nil) - _ = store.DeleteByID(ctx, user.Id) + user.IsBanned = true + store.ReplaceItem(ctx, *user, true) } } return nil diff --git a/kicker/tasks.go b/kicker/tasks.go index 2e14f39..b0d7408 100644 --- a/kicker/tasks.go +++ b/kicker/tasks.go @@ -22,7 +22,7 @@ func TaskKickOldUsers(b *echotron.API) { now := time.Now() old := now.Add(-10 * time.Minute) store := tongo.NewStore[db.User](Client) - users, err := store.GetMany(ctx, tongo.E("date_joined", tongo.D(tongo.E("$lt", old)))) + users, err := store.GetMany(ctx, tongo.E("date_joined", tongo.D(tongo.E("$lt", old))), tongo.E("is_joined", false), tongo.E("is_banned", false)) if err != nil { log.Printf("Error in deleting task: %v", err) } @@ -33,9 +33,7 @@ func TaskKickOldUsers(b *echotron.API) { continue } log.Printf("User %s was banned", user.FirstName) - b.DeleteMessage(user.ChatId, user.CaptchaMessage) - b.DeleteMessage(user.ChatId, user.JoinedMessage) - store.DeleteByID(ctx, user.Id) + user.IsBanned = true } } @@ -44,7 +42,7 @@ func TaskNotifyUsers(b *echotron.API) { defer cancel() store := tongo.NewStore[db.User](Client) chatStore := tongo.NewStore[db.Chat](Client) - users, _ := store.GetMany(ctx) + users, _ := store.GetMany(ctx, tongo.E("is_joined", false), tongo.E("is_banned", false)) for _, user := range users { if time.Since(user.LastNotification) > 2*time.Minute { user.LastNotification = time.Now()