KickerBot/kicker/tasks.go

92 lines
2.8 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-12-07 22:02:36 +03:00
logstore := tongo.NewStore[db.LogChannel](Client)
logchat, _ := logstore.GetOne(ctx)
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})
2023-12-07 22:02:36 +03:00
2022-12-12 01:29:25 +03:00
if err != nil {
log.Println("User was not banned: ", err)
continue
}
2023-12-07 22:02:36 +03:00
if logchat != nil {
b.SendMessage(
fmt.Sprintf("Пользователь %s не прошёл капчу \\#captcha", MentionWithDataDB(user)),
logchat.ChatId,
&echotron.MessageOptions{ParseMode: echotron.MarkdownV2})
}
log.Printf("User %s was banned", user.FirstName)
2023-09-10 20:33:40 +03:00
_, err = b.DeleteMessage(user.ChatId, user.CaptchaMessage)
if err != nil {
log.Println("ERR: Captcha message not deleted: ", err)
}
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()
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-13 22:33:53 +03:00
text := fmt.Sprintf("*%s*, напоминаю, что тебе необходимо пройти [капчу](https://t.me/c/%d/%d/%d)\\!", UserMentionDB(user), transformChatID(user.ChatId), chat.TopicId, user.CaptchaMessage)
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
}
}
2023-09-13 22:33:53 +03:00
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)
}
}
}