KickerBot/kicker/tasks.go

40 lines
991 B
Go
Raw Normal View History

2022-02-04 12:35:47 +03:00
package kicker
import (
"context"
"kickerbot/db"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
tb "gopkg.in/tucnak/telebot.v3"
)
2022-02-04 12:39:57 +03:00
func TaskKickOldUsers(b tb.Bot) {
2022-02-04 12:35:47 +03:00
d := db.GetDatabase()
2022-02-04 13:20:29 +03:00
log.Print("STARTING KICKING TASK")
2022-02-04 12:35:47 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
now := time.Now().Unix()
old := now - 120
2022-02-04 12:35:47 +03:00
filter := bson.D{
2022-02-04 13:20:29 +03:00
primitive.E{Key: "date_joined", Value: bson.D{bson.E{Key: "$lt", Value: old}}},
2022-02-04 12:35:47 +03:00
}
users, err := d.GetUsers(ctx, filter)
if err != nil {
log.Printf("Error in deleting task: %v", err)
}
for _, user := range users {
chat := tb.Chat{ID: user.ChatId}
tbUser := tb.User{ID: user.Id}
member := tb.ChatMember{User: &tbUser}
2022-02-04 13:20:29 +03:00
message := tb.Message{Chat: &chat, ID: user.CaptchaMessage}
joinMessage := tb.Message{Chat: &chat, ID: user.JoinedMessage}
2022-02-04 12:35:47 +03:00
b.Ban(&chat, &member)
2022-02-04 13:20:29 +03:00
b.Delete(&message)
b.Delete(&joinMessage)
2022-02-04 12:56:31 +03:00
d.RemoveUser(ctx, user)
2022-02-04 12:35:47 +03:00
}
}