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-09 01:45:47 +03:00
|
|
|
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))
|
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
|
|
|
|
}
|
2023-02-14 11:20:43 +03:00
|
|
|
log.Printf("User %s was banned", user.FirstName)
|
2023-09-09 01:45:47 +03:00
|
|
|
user.IsBanned = true
|
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-09 01:45:47 +03:00
|
|
|
users, _ := store.GetMany(ctx, tongo.E("is_joined", false), tongo.E("is_banned", 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()
|
2023-09-07 01:10:30 +03:00
|
|
|
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))
|
2023-09-07 01:10:30 +03:00
|
|
|
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 {
|
2023-09-07 01:10:30 +03:00
|
|
|
topic = chat.TopicId
|
2023-02-14 00:50:21 +03:00
|
|
|
}
|
2023-02-14 01:20:21 +03:00
|
|
|
res, err := b.SendMessage(text, user.ChatId, &echotron.MessageOptions{MessageThreadID: topic, ParseMode: echotron.MarkdownV2})
|
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
|
|
|
}
|
|
|
|
}
|