-
Notifications
You must be signed in to change notification settings - Fork 25
/
database.py
188 lines (147 loc) · 5.79 KB
/
database.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
181
182
183
184
185
186
187
188
# Stubborn Developer
# Don't Remove Credit 🥺
# Telegram Channel @Stubborn1223
from os import environ
from config import Config
import motor.motor_asyncio
from pymongo import MongoClient
async def mongodb_version():
x = MongoClient(Config.DB_URL)
mongodb_version = x.server_info()['version']
return mongodb_version
class Database:
def __init__(self, uri, database_name):
self._client = motor.motor_asyncio.AsyncIOMotorClient(uri)
self.db = self._client[database_name]
self.bot = self.db.bots
self.col = self.db.users
self.nfy = self.db.notify
self.chl = self.db.channels
def new_user(self, id, name):
return dict(
id = id,
name = name,
ban_status=dict(
is_banned=False,
ban_reason="",
),
)
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_bots_count(self):
bcount = await self.bot.count_documents({})
count = await self.col.count_documents({})
return count, bcount
async def total_channels(self):
count = await self.chl.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 get_banned(self):
users = self.col.find({'ban_status.is_banned': True})
b_users = [user['id'] async for user in users]
return b_users
async def update_configs(self, id, configs):
await self.col.update_one({'id': int(id)}, {'$set': {'configs': configs}})
async def get_configs(self, id):
default = {
'caption': None,
'duplicate': True,
'forward_tag': False,
'file_size': 0,
'size_limit': None,
'extension': None,
'keywords': None,
'protect': None,
'button': None,
'db_uri': None,
'filters': {
'poll': True,
'text': True,
'audio': True,
'voice': True,
'video': True,
'photo': True,
'document': True,
'animation': True,
'sticker': True
}
}
user = await self.col.find_one({'id':int(id)})
if user:
return user.get('configs', default)
return default
async def add_bot(self, datas):
if not await self.is_bot_exist(datas['user_id']):
await self.bot.insert_one(datas)
async def remove_bot(self, user_id):
await self.bot.delete_many({'user_id': int(user_id)})
async def get_bot(self, user_id: int):
bot = await self.bot.find_one({'user_id': user_id})
return bot if bot else None
async def is_bot_exist(self, user_id):
bot = await self.bot.find_one({'user_id': user_id})
return bool(bot)
async def in_channel(self, user_id: int, chat_id: int) -> bool:
channel = await self.chl.find_one({"user_id": int(user_id), "chat_id": int(chat_id)})
return bool(channel)
async def add_channel(self, user_id: int, chat_id: int, title, username):
channel = await self.in_channel(user_id, chat_id)
if channel:
return False
return await self.chl.insert_one({"user_id": user_id, "chat_id": chat_id, "title": title, "username": username})
async def remove_channel(self, user_id: int, chat_id: int):
channel = await self.in_channel(user_id, chat_id )
if not channel:
return False
return await self.chl.delete_many({"user_id": int(user_id), "chat_id": int(chat_id)})
async def get_channel_details(self, user_id: int, chat_id: int):
return await self.chl.find_one({"user_id": int(user_id), "chat_id": int(chat_id)})
async def get_user_channels(self, user_id: int):
channels = self.chl.find({"user_id": int(user_id)})
return [channel async for channel in channels]
async def get_filters(self, user_id):
filters = []
filter = (await self.get_configs(user_id))['filters']
for k, v in filter.items():
if v == False:
filters.append(str(k))
return filters
async def add_frwd(self, user_id):
return await self.nfy.insert_one({'user_id': int(user_id)})
async def rmve_frwd(self, user_id=0, all=False):
data = {} if all else {'user_id': int(user_id)}
return await self.nfy.delete_many(data)
async def get_all_frwd(self):
return self.nfy.find({})
db = Database(Config.DB_URL, Config.DB_NAME)
# Stubborn Developer
# Don't Remove Credit 🥺
# Telegram Channel @Stubborn1223