KickerBot/kicker/tasks.go

69 lines
2.0 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))))
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)
b.DeleteMessage(user.ChatId, user.CaptchaMessage)
b.DeleteMessage(user.ChatId, user.JoinedMessage)
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)
for _, user := range users {
if time.Since(user.LastNotification) > 2*time.Minute {
user.LastNotification = time.Now()
text := fmt.Sprintf("*%s*, напоминаю, что тебе необходимо пройти капчу\\!", UserMentionDB(user))
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
}
res, err := b.SendMessage(text, user.ChatId, &echotron.MessageOptions{MessageThreadID: topic, ParseMode: echotron.MarkdownV2})
if err != nil {
log.Printf("Can't send notification to user: %s", err)
}
go waitAndDelete(b, res.Result, 2*time.Minute)
}
}
}