File size: 2,160 Bytes
87abec5
11ae35a
af1662b
89a7a7a
11ae35a
87abec5
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dcea66
89a7a7a
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
6dcea66
ca4eb6d
 
 
 
 
89ad488
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from datetime import datetime
from threading import RLock

from Powers import TIME_ZONE as TZ
from Powers.database import MongoDB

INSERTION_LOCK = RLock()
ANTISPAM_BANNED = set()


class GBan(MongoDB):
    """Class for managing Gbans in bot."""

    db_name = "gbans"

    def __init__(self) -> None:
        super().__init__(self.db_name)

    def check_gban(self, user_id: int):
        with INSERTION_LOCK:
            return bool(self.find_one({"_id": user_id}))

    def add_gban(self, user_id: int, reason: str, by_user: int):
        global ANTISPAM_BANNED
        with INSERTION_LOCK:
            # Check if  user is already gbanned or not
            if self.find_one({"_id": user_id}):
                return self.update_gban_reason(user_id, reason)

            # If not already gbanned, then add to gban
            ANTISPAM_BANNED.add(user_id)
            time_rn = datetime.now(TZ)
            return self.insert_one(
                {
                    "_id": user_id,
                    "reason": reason,
                    "by": by_user,
                    "time": time_rn,
                },
            )

    def remove_gban(self, user_id: int):
        global ANTISPAM_BANNED
        with INSERTION_LOCK:
            # Check if  user is already gbanned or not
            if self.find_one({"_id": user_id}):
                ANTISPAM_BANNED.remove(user_id)
                return self.delete_one({"_id": user_id})
            return "User not gbanned!"

    def get_gban(self, user_id: int):
        if self.check_gban(user_id):
            if curr := self.find_one({"_id": user_id}):
                return True, curr["reason"]
        return False, ""

    def update_gban_reason(self, user_id: int, reason: str):
        with INSERTION_LOCK:
            return self.update(
                {"_id": user_id},
                {"reason": reason},
            )

    def count_gbans(self):
        with INSERTION_LOCK:
            return self.count()

    def load_from_db(self):
        with INSERTION_LOCK:
            return self.find_all()

    def list_gbans(self):
        with INSERTION_LOCK:
            return self.find_all()