KickerBot/kicker/tasks.go

67 lines
2.0 KiB
Go
Raw Normal View History

2022-02-04 12:35:47 +03:00
package kicker
import (
"context"
2023-02-14 00:50:21 +03:00
"fmt"
2022-02-04 12:35:47 +03:00
"kickerbot/db"
"log"
"time"
2023-02-14 00:50:21 +03:00
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
2022-02-04 12:35:47 +03:00
)
2022-11-09 00:45:08 +03:00
type TaskBot struct {
Token string
2023-02-14 00:50:21 +03:00
echotron.API
2022-11-09 00:45:08 +03:00
}
2023-02-14 00:50:21 +03:00
func TaskKickOldUsers(b *echotron.API) {
2022-02-04 12:35:47 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
2023-02-14 00:50:21 +03:00
now := time.Now()
old := now.Add(-10 * time.Minute)
store := tongo.NewStore[db.User](Client)
2023-09-10 02:48:59 +03:00
users, err := store.GetMany(ctx, tongo.E("date_joined", tongo.D(tongo.E("$lt", old))), tongo.E("is_joined", false))
2022-02-04 12:35:47 +03:00
if err != nil {
log.Printf("Error in deleting task: %v", err)
}
for _, user := range users {
2023-02-14 00:50:21 +03:00
_, err := b.BanChatMember(user.ChatId, user.UserId, &echotron.BanOptions{RevokeMessages: true})
2022-12-12 01:29:25 +03:00
if err != nil {
log.Println("User was not banned: ", err)
continue
}
log.Printf("User %s was banned", user.FirstName)
2023-09-10 02:48:59 +03:00
store.DeleteByID(ctx, user.Id)
2023-02-14 00:50:21 +03:00
}
}
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)
2023-09-10 02:48:59 +03:00
users, _ := store.GetMany(ctx, tongo.E("is_joined", false))
2023-02-14 00:50:21 +03:00
for _, user := range users {
if time.Since(user.LastNotification) > 2*time.Minute {
user.LastNotification = time.Now()
text := fmt.Sprintf("*%s*, напоминаю, что тебе необходимо пройти капчу\\!", UserMentionDB(user))
2023-02-14 00:50:21 +03:00
store.ReplaceItem(ctx, *user, false)
chat, err := chatStore.GetOne(ctx, tongo.E("chat_id", user.ChatId))
var topic int64 = 0
2023-02-14 00:50:21 +03:00
if err != nil {
log.Printf("Can't get chat from user: %s", err)
2023-02-14 01:20:21 +03:00
return
} else {
topic = chat.TopicId
2023-02-14 00:50:21 +03:00
}
2023-09-10 02:48:59 +03:00
res, err := b.SendMessage(text, user.ChatId, &echotron.MessageOptions{MessageThreadID: topic, ParseMode: echotron.MarkdownV2, ReplyToMessageID: user.CaptchaMessage})
2023-02-14 00:50:21 +03:00
if err != nil {
log.Printf("Can't send notification to user: %s", err)
}
go waitAndDelete(b, res.Result, 2*time.Minute)
}
2022-02-04 12:35:47 +03:00
}
}