Not deleting users

This commit is contained in:
Nefrace 2023-09-09 01:45:47 +03:00
parent e1319a951d
commit 635f27f5ed
4 changed files with 15 additions and 14 deletions

View File

@ -6,9 +6,9 @@ import (
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"os"
"strings" "strings"
"github.com/fogleman/gg" "github.com/fogleman/gg"
@ -104,7 +104,7 @@ func (captcha *Captcha) ToBytes() (*[]byte, error) {
// Логотипы читаются из папки /assets рядом с исполняемым файлом. // Логотипы читаются из папки /assets рядом с исполняемым файлом.
// Принимается формат .png, логотип, представляющий правильный ответ называется godot.png // Принимается формат .png, логотип, представляющий правильный ответ называется godot.png
func Init() error { func Init() error {
files, err := ioutil.ReadDir("./assets") files, err := os.ReadDir("./assets")
if err != nil { if err != nil {
return err return err
} }

View File

@ -30,6 +30,7 @@ type User struct {
DateJoined time.Time `bson:"date_joined"` DateJoined time.Time `bson:"date_joined"`
JoinedMessage int `bson:"joined_message"` JoinedMessage int `bson:"joined_message"`
LastNotification time.Time `bson:"last_notification"` LastNotification time.Time `bson:"last_notification"`
Warnings int `bson:"warnings"`
} }
func (User) Coll() string { return "users" } func (User) Coll() string { return "users" }

View File

@ -15,10 +15,10 @@ import (
func userJoined(b *bot, update *echotron.Update) error { func userJoined(b *bot, update *echotron.Update) error {
captcha := captchagen.GenCaptcha() captcha := captchagen.GenCaptcha()
_, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID) // _, err := b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
if err != nil { // if err != nil {
log.Printf("Can't delete message: %v", err) // log.Printf("Can't delete message: %v", err)
} // }
bytes, err := captcha.ToBytes() bytes, err := captcha.ToBytes()
if err != nil { if err != nil {
log.Printf("Error creating captcha bytes: %v", bytes) 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) store := tongo.NewStore[db.User](Client)
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 := 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) store.DeleteByID(ctx, user.Id)
b.DeleteMessage(message.Chat.ID, message.ID) b.DeleteMessage(message.Chat.ID, message.ID)
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) b.DeleteMessage(message.Chat.ID, user.CaptchaMessage)
@ -102,7 +102,8 @@ func checkCaptcha(b *bot, update *echotron.Update) error {
solved := false solved := false
if num, err := strconv.Atoi(guess); err == nil { if num, err := strconv.Atoi(guess); err == nil {
if num == int(user.CorrectAnswer) { if num == int(user.CorrectAnswer) {
_ = store.DeleteByID(ctx, user.Id) user.IsJoined = true
store.ReplaceItem(ctx, *user, true)
solved = true solved = true
b.DeleteMessage(message.Chat.ID, message.ID) b.DeleteMessage(message.Chat.ID, message.ID)
b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) 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.CaptchaMessage)
b.DeleteMessage(message.Chat.ID, user.JoinedMessage) b.DeleteMessage(message.Chat.ID, user.JoinedMessage)
b.BanChatMember(message.Chat.ID, sender.ID, nil) b.BanChatMember(message.Chat.ID, sender.ID, nil)
_ = store.DeleteByID(ctx, user.Id) user.IsBanned = true
store.ReplaceItem(ctx, *user, true)
} }
} }
return nil return nil

View File

@ -22,7 +22,7 @@ func TaskKickOldUsers(b *echotron.API) {
now := time.Now() now := time.Now()
old := now.Add(-10 * time.Minute) old := now.Add(-10 * time.Minute)
store := tongo.NewStore[db.User](Client) 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 { if err != nil {
log.Printf("Error in deleting task: %v", err) log.Printf("Error in deleting task: %v", err)
} }
@ -33,9 +33,7 @@ func TaskKickOldUsers(b *echotron.API) {
continue continue
} }
log.Printf("User %s was banned", user.FirstName) log.Printf("User %s was banned", user.FirstName)
b.DeleteMessage(user.ChatId, user.CaptchaMessage) user.IsBanned = true
b.DeleteMessage(user.ChatId, user.JoinedMessage)
store.DeleteByID(ctx, user.Id)
} }
} }
@ -44,7 +42,7 @@ func TaskNotifyUsers(b *echotron.API) {
defer cancel() defer cancel()
store := tongo.NewStore[db.User](Client) store := tongo.NewStore[db.User](Client)
chatStore := tongo.NewStore[db.Chat](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 { for _, user := range users {
if time.Since(user.LastNotification) > 2*time.Minute { if time.Since(user.LastNotification) > 2*time.Minute {
user.LastNotification = time.Now() user.LastNotification = time.Now()