Spaces:
Sleeping
Sleeping
Captain Ezio
commited on
Commit
·
9a4f289
1
Parent(s):
c0287da
Update flood_db.py
Browse files- Powers/database/flood_db.py +58 -54
Powers/database/flood_db.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
from threading import RLock
|
|
|
2 |
|
|
|
3 |
from Powers.database import MongoDB
|
4 |
from Powers.utils.msg_types import Types
|
5 |
|
@@ -7,60 +9,62 @@ INSERTION_LOCK = RLock()
|
|
7 |
|
8 |
class Floods(MongoDB):
|
9 |
"""Class to store flood limit and action of a chat"""
|
|
|
|
|
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 |
-
def is_chat(self, chat_id: int):
|
45 |
-
with INSERTION_LOCK:
|
46 |
-
curr = self.find_one({"chat_id": chat_id})
|
47 |
-
if curr:
|
48 |
-
action = [str(curr["limit"]), str(curr["within"]), str(curr["action"])]
|
49 |
-
return action
|
50 |
-
return False
|
51 |
-
|
52 |
-
def get_action(self, chat_id: int, limit: int, within: int, action: str):
|
53 |
-
with INSERTION_LOCK:
|
54 |
-
curr = self.find_one({"chat_id": chat_id, "limit": limit, "within": within, "action": action})
|
55 |
-
if curr:
|
56 |
-
return curr
|
57 |
-
return "Flood haven't set"
|
58 |
-
|
59 |
-
def rm_flood(self, chat_id: int, limit: int, within: int, action: str):
|
60 |
-
with INSERTION_LOCK:
|
61 |
-
curr = self.find_one({"chat_id": chat_id, "limit": limit, "within": within, "action": action})
|
62 |
-
if curr:
|
63 |
-
self.delete_one(curr)
|
64 |
-
return True
|
65 |
-
return False
|
66 |
-
|
|
|
1 |
from threading import RLock
|
2 |
+
from traceback import format_exc
|
3 |
|
4 |
+
from Powers import LOGGER
|
5 |
from Powers.database import MongoDB
|
6 |
from Powers.utils.msg_types import Types
|
7 |
|
|
|
9 |
|
10 |
class Floods(MongoDB):
|
11 |
"""Class to store flood limit and action of a chat"""
|
12 |
+
try:
|
13 |
+
db_name = "flood"
|
14 |
|
15 |
+
def __init__(self):
|
16 |
+
super().__init__(self.db_name)
|
17 |
|
18 |
+
def save_flood(
|
19 |
+
self,
|
20 |
+
chat_id: int,
|
21 |
+
limit: int,
|
22 |
+
within: int,
|
23 |
+
action: str,
|
24 |
+
):
|
25 |
+
with INSERTION_LOCK:
|
26 |
+
curr = self.find_one({"chat_id": chat_id})
|
27 |
+
if curr:
|
28 |
+
if not(limit == int(curr['limit']) or within == int(curr['within']) or action == str(curr['action'])):
|
29 |
+
return self.update(
|
30 |
+
{
|
31 |
+
"chat_id": chat_id,
|
32 |
+
"limit": limit,
|
33 |
+
"within": within,
|
34 |
+
"action": action,
|
35 |
+
}
|
36 |
+
)
|
37 |
+
return self.insert_one(
|
38 |
+
{
|
39 |
+
"chat_id" : chat_id,
|
40 |
+
"limit": limit,
|
41 |
+
"within": within,
|
42 |
+
"action" : action
|
43 |
+
},
|
44 |
+
)
|
45 |
|
46 |
+
def is_chat(self, chat_id: int):
|
47 |
+
with INSERTION_LOCK:
|
48 |
+
curr = self.find_one({"chat_id": chat_id})
|
49 |
+
if curr:
|
50 |
+
action = [str(curr["limit"]), str(curr["within"]), str(curr["action"])]
|
51 |
+
return action
|
52 |
+
return False
|
53 |
+
|
54 |
+
def get_action(self, chat_id: int, limit: int, within: int, action: str):
|
55 |
+
with INSERTION_LOCK:
|
56 |
+
curr = self.find_one({"chat_id": chat_id, "limit": limit, "within": within, "action": action})
|
57 |
+
if curr:
|
58 |
+
return curr
|
59 |
+
return "Flood haven't set"
|
60 |
+
|
61 |
+
def rm_flood(self, chat_id: int, limit: int, within: int, action: str):
|
62 |
+
with INSERTION_LOCK:
|
63 |
+
curr = self.find_one({"chat_id": chat_id, "limit": limit, "within": within, "action": action})
|
64 |
+
if curr:
|
65 |
+
self.delete_one(curr)
|
66 |
+
return True
|
67 |
+
return False
|
68 |
+
except Exception as e:
|
69 |
+
LOGGER.error(ef)
|
70 |
+
LOGGER.error(format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|