diff --git a/db/user.go b/db/user.go index 6fa832a..9dfa259 100644 --- a/db/user.go +++ b/db/user.go @@ -43,6 +43,18 @@ func (d *DB) GetUser(ctx context.Context, user User) (User, error) { return result, nil } +func (d *DB) GetUsers(ctx context.Context, filter bson.D) ([]User, error) { + cursor, err := d.Database.Collection("users").Find(ctx, filter) + if err != nil { + return []User{}, err + } + var results []User + if err = cursor.All(ctx, &results); err != nil { + return []User{}, err + } + return results, nil +} + func (d *DB) RemoveUser(ctx context.Context, user User) error { filter := makeIDFilter(user) _, err := d.Database.Collection("users").DeleteOne(ctx, filter) diff --git a/kicker/tasks.go b/kicker/tasks.go new file mode 100644 index 0000000..f6e4937 --- /dev/null +++ b/kicker/tasks.go @@ -0,0 +1,34 @@ +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" +) + +func KickOldUsers(b tb.Bot) { + d := db.GetDatabase() + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + now := time.Now().Unix() + old := now - 60 + filter := bson.D{ + primitive.E{Key: "date_joined", Value: bson.D{{"$lt", old}}}, + } + 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} + b.Ban(&chat, &member) + time.Sleep(250 * time.Millisecond) + } +}