forked from MrTamilKiD/AutoFilterBot-Beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_chats_db.py
180 lines (137 loc) · 5.21 KB
/
users_chats_db.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import motor.motor_asyncio
from info import DATABASE_NAME, DATABASE_URL, IMDB_TEMPLATE, WELCOME_TEXT, AUTH_CHANNEL, LINK_MODE, TUTORIAL, SHORTLINK_URL, SHORTLINK_API, SHORTLINK, FILE_CAPTION, IMDB, WELCOME, SPELL_CHECK, PROTECT_CONTENT, AUTO_FILTER, AUTO_DELETE
class Database:
default_setgs = {
'auto_filter': AUTO_FILTER,
'file_secure': PROTECT_CONTENT,
'imdb': IMDB,
'spell_check': SPELL_CHECK,
'auto_delete': AUTO_DELETE,
'welcome': WELCOME,
'welcome_text': WELCOME_TEXT,
'template': IMDB_TEMPLATE,
'caption': FILE_CAPTION,
'url': SHORTLINK_URL,
'api': SHORTLINK_API,
'shortlink': SHORTLINK,
'tutorial': TUTORIAL,
'links': LINK_MODE,
'fsub': AUTH_CHANNEL
}
default_verify = {
'is_verified': False,
'verified_time': 0,
'verify_token': "",
'link': ""
}
def __init__(self):
self._client = motor.motor_asyncio.AsyncIOMotorClient(DATABASE_URL)
self.db = self._client[DATABASE_NAME]
self.col = self.db.Users
self.grp = self.db.Groups
def new_user(self, id, name):
return dict(
id = id,
name = name,
ban_status=dict(
is_banned=False,
ban_reason="",
),
verify_status=dict(
is_verified=False,
verified_time="",
verify_token="",
link=""
))
def new_group(self, id, title):
return dict(
id = id,
title = title,
chat_status=dict(
is_disabled=False,
reason="",
),
settings=self.default_setgs
)
async def add_user(self, id, name):
user = self.new_user(id, name)
await self.col.insert_one(user)
async def is_user_exist(self, id):
user = await self.col.find_one({'id':int(id)})
return bool(user)
async def total_users_count(self):
count = await self.col.count_documents({})
return count
async def remove_ban(self, id):
ban_status = dict(
is_banned=False,
ban_reason=''
)
await self.col.update_one({'id': id}, {'$set': {'ban_status': ban_status}})
async def ban_user(self, user_id, ban_reason="No Reason"):
ban_status = dict(
is_banned=True,
ban_reason=ban_reason
)
await self.col.update_one({'id': user_id}, {'$set': {'ban_status': ban_status}})
async def get_ban_status(self, id):
default = dict(
is_banned=False,
ban_reason=''
)
user = await self.col.find_one({'id':int(id)})
if not user:
return default
return user.get('ban_status', default)
async def get_all_users(self):
return self.col.find({})
async def delete_user(self, user_id):
await self.col.delete_many({'id': int(user_id)})
async def delete_chat(self, grp_id):
await self.grp.delete_many({'id': int(grp_id)})
async def get_banned(self):
users = self.col.find({'ban_status.is_banned': True})
chats = self.grp.find({'chat_status.is_disabled': True})
b_chats = [chat['id'] async for chat in chats]
b_users = [user['id'] async for user in users]
return b_users, b_chats
async def add_chat(self, chat, title):
chat = self.new_group(chat, title)
await self.grp.insert_one(chat)
async def get_chat(self, chat):
chat = await self.grp.find_one({'id':int(chat)})
return False if not chat else chat.get('chat_status')
async def re_enable_chat(self, id):
chat_status=dict(
is_disabled=False,
reason="",
)
await self.grp.update_one({'id': int(id)}, {'$set': {'chat_status': chat_status}})
async def update_settings(self, id, settings):
await self.grp.update_one({'id': int(id)}, {'$set': {'settings': settings}})
async def get_settings(self, id):
chat = await self.grp.find_one({'id':int(id)})
if chat:
return chat.get('settings', self.default_setgs)
return self.default_setgs
async def disable_chat(self, chat, reason="No Reason"):
chat_status=dict(
is_disabled=True,
reason=reason,
)
await self.grp.update_one({'id': int(chat)}, {'$set': {'chat_status': chat_status}})
async def get_verify_status(self, user_id):
user = await self.col.find_one({'id':int(user_id)})
if user:
return user.get('verify_status', self.default_verify)
return self.default_verify
async def update_verify_status(self, user_id, verify):
await self.col.update_one({'id': int(user_id)}, {'$set': {'verify_status': verify}})
async def total_chat_count(self):
count = await self.grp.count_documents({})
return count
async def get_all_chats(self):
return self.grp.find({})
async def get_db_size(self):
return (await self.db.command("dbstats"))['dataSize']
db = Database()