-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwarnsdb
More file actions
90 lines (73 loc) · 2.11 KB
/
Copy pathwarnsdb
File metadata and controls
90 lines (73 loc) · 2.11 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from PyrogramBot import db
from typing import Dict, List, Union
warnsdb = db.warns
async def int_to_alpha(user_id: int) -> str:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
text = ""
user_id = str(user_id)
for i in user_id:
text += alphabet[int(i)]
return text
async def alpha_to_int(user_id_alphabet: str) -> int:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
user_id = ""
for i in user_id_alphabet:
index = alphabet.index(i)
user_id += str(index)
user_id = int(user_id)
return user_id
async def get_warns_count() -> dict:
chats = warnsdb.find({"chat_id": {"$lt": 0}})
if not chats:
return {}
chats_count = 0
warns_count = 0
for chat in await chats.to_list(length=100000000):
for user in chat['warns']:
warns_count += chat['warns'][user]['warns']
chats_count += 1
return {
"chats_count": chats_count,
"warns_count": warns_count
}
async def get_warns(chat_id: int) -> Dict[str, int]:
warns = await warnsdb.find_one({"chat_id": chat_id})
if warns:
warns = warns['warns']
else:
warns = {}
return warns
async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
name = name.lower().strip()
warns = await get_warns(chat_id)
if name in warns:
return warns[name]
async def add_warn(chat_id: int, name: str, warn: dict):
name = name.lower().strip()
warns = await get_warns(chat_id)
warns[name] = warn
await warnsdb.update_one(
{"chat_id": chat_id},
{
"$set": {
"warns": warns
}
},
upsert=True
)
async def remove_warns(chat_id: int, name: str) -> bool:
warnsd = await get_warns(chat_id)
name = name.lower().strip()
if name in warnsd:
del warnsd[name]
await warnsdb.update_one(
{"chat_id": chat_id},
{
"$set": {
"warns": warnsd
}
},
upsert=True
)
return True
return False