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 )
users , err := store . GetMany ( ctx , tongo . E ( "date_joined" , tongo . D ( tongo . E ( "$lt" , old ) ) ) )
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 )
2022-11-09 00:45:08 +03:00
b . DeleteMessage ( user . ChatId , user . CaptchaMessage )
b . DeleteMessage ( user . ChatId , user . JoinedMessage )
2023-02-14 00:50:21 +03:00
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 ( )
2023-02-15 00:04:35 +03:00
text := fmt . Sprintf ( "*[%s](tg://user?id=%d)*, напоминаю, что тебе необходимо пройти капчу\\!" , EscapeText ( echotron . MarkdownV2 , user . FirstName ) , user . UserId )
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-02-14 01:20:21 +03:00
topic := 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 = int ( 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
}
}