KickerBot/kicker/tasks.go

83 lines
2.5 KiB
Go

package kicker
import (
"context"
"fmt"
"kickerbot/db"
"log"
"time"
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
)
type TaskBot struct {
Token string
echotron.API
}
func TaskKickOldUsers(b *echotron.API) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
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))), tongo.E("is_joined", false))
if err != nil {
log.Printf("Error in deleting task: %v", err)
}
for _, user := range users {
_, err := b.BanChatMember(user.ChatId, user.UserId, &echotron.BanOptions{RevokeMessages: true})
if err != nil {
log.Println("User was not banned: ", err)
continue
}
log.Printf("User %s was banned", user.FirstName)
_, err = b.DeleteMessage(user.ChatId, user.CaptchaMessage)
if err != nil {
log.Println("ERR: Captcha message not deleted: ", err)
}
store.DeleteByID(ctx, user.Id)
}
}
func TaskNotifyUsers(b *echotron.API) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
store := tongo.NewStore[db.User](Client)
chatStore := tongo.NewStore[db.Chat](Client)
users, _ := store.GetMany(ctx, tongo.E("is_joined", false))
for _, user := range users {
if time.Since(user.LastNotification) > 2*time.Minute {
user.LastNotification = time.Now()
store.ReplaceItem(ctx, *user, false)
chat, err := chatStore.GetOne(ctx, tongo.E("chat_id", user.ChatId))
var topic int64 = 0
if err != nil {
log.Printf("Can't get chat from user: %s", err)
return
} else {
topic = chat.TopicId
}
text := fmt.Sprintf("*%s*, напоминаю, что тебе необходимо пройти [капчу](https://t.me/c/%d/%d/%d)\\!", UserMentionDB(user), transformChatID(user.ChatId), chat.TopicId, user.CaptchaMessage)
res, err := b.SendMessage(text, user.ChatId, &echotron.MessageOptions{MessageThreadID: topic, ParseMode: echotron.MarkdownV2, ReplyToMessageID: user.CaptchaMessage})
if err != nil {
log.Printf("Can't send notification to user: %s", err)
}
go waitAndDelete(b, res.Result, 2*time.Minute)
}
}
}
func TaskRemoveOldMutes(b *echotron.API) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
store := tongo.NewStore[db.Mute](Client)
mutes, _ := store.GetMany(ctx)
for _, mute := range mutes {
if time.Now().After(mute.Until) {
store.DeleteByID(ctx, mute.Id)
}
}
}