Migration to Tongo, new logic for user storage #3
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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" }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue