forked from Learningbots79/movies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_chats_db.py
406 lines (349 loc) · 16.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import datetime
import pytz
from motor.motor_asyncio import AsyncIOMotorClient
from info import SETTINGS, IS_PM_SEARCH, PREMIUM_POINT,REF_PREMIUM,IS_VERIFY, SHORTENER_WEBSITE3, SHORTENER_API3, THREE_VERIFY_GAP, LINK_MODE, FILE_CAPTION, TUTORIAL, DATABASE_NAME, DATABASE_URI, IMDB, IMDB_TEMPLATE, PROTECT_CONTENT, AUTO_DELETE, SPELL_CHECK, AUTO_FILTER, LOG_VR_CHANNEL, SHORTENER_WEBSITE, SHORTENER_API, SHORTENER_WEBSITE2, SHORTENER_API2, TWO_VERIFY_GAP
# from utils import get_seconds
client = AsyncIOMotorClient(DATABASE_URI)
mydb = client[DATABASE_NAME]
fsubs = client['fsubs']
class Database:
default = SETTINGS.copy()
def __init__(self):
self.col = mydb.users
self.grp = mydb.groups
self.misc = mydb.misc
self.verify_id = mydb.verify_id
self.users = mydb.uersz
self.req = mydb.requests
self.mGrp = mydb.mGrp
self.pmMode = mydb.pmMode
self.jisshu_ads_link = mydb.jisshu_ads_link
self.grp_and_ids = fsubs.grp_and_ids
self.movies_update_channel = mydb.movies_update_channel
self.botcol = mydb.botcol
def new_user(self, id, name):
return dict(
id = id,
name = name,
point = 0,
ban_status=dict(
is_banned=False,
ban_reason=""
)
)
async def get_settings(self, id):
chat = await self.grp.find_one({'id':int(id)})
if chat:
return chat.get('settings', self.default)
else:
await self.grp.update_one({'id': int(id)}, {'$set': {'settings': self.default}} , upsert=True)
return self.default
async def find_join_req(self, id):
return bool(await self.req.find_one({'id': id}))
async def add_join_req(self, id):
await self.req.insert_one({'id': id})
async def del_join_req(self):
await self.req.drop()
def new_group(self, id, title):
return dict(
id = id,
title = title,
chat_status=dict(
is_disabled=False,
reason=""
)
)
async def add_user(self, id, name):
user = self.new_user(id, name)
await self.col.insert_one(user)
async def update_point(self ,id):
await self.col.update_one({'id' : id} , {'$inc':{'point' : 100}})
point = (await self.col.find_one({'id' : id}))['point']
if point >= PREMIUM_POINT :
seconds = (REF_PREMIUM * 24 * 60 * 60)
oldEx =(await self.users.find_one({'id' : id}))
if oldEx :
expiry_time = oldEx['expiry_time'] + datetime.timedelta(seconds=seconds)
else:
expiry_time = datetime.datetime.now() + datetime.timedelta(seconds=seconds)
user_data = {"id": id, "expiry_time": expiry_time}
await db.update_user(user_data)
await self.col.update_one({'id' : id} , {'$set':{'point' : 0}})
async def get_point(self , id):
newPoint = await self.col.find_one({'id' : id})
return newPoint['point'] if newPoint else None
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 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, id):
await self.grp.delete_many({'id': int(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 update_settings(self, id, settings):
await self.grp.update_one({'id': int(id)}, {'$set': {'settings': settings}})
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 mydb.command("dbstats"))['dataSize']
async def get_notcopy_user(self, user_id):
user_id = int(user_id)
user = await self.misc.find_one({"user_id": user_id})
ist_timezone = pytz.timezone('Asia/Kolkata')
if not user:
res = {
"user_id": user_id,
"last_verified": datetime.datetime(2020, 5, 17, 0, 0, 0, tzinfo=ist_timezone),
"second_time_verified": datetime.datetime(2019, 5, 17, 0, 0, 0, tzinfo=ist_timezone),
}
user = await self.misc.insert_one(res)
return user
async def update_notcopy_user(self, user_id, value:dict):
user_id = int(user_id)
myquery = {"user_id": user_id}
newvalues = {"$set": value}
return await self.misc.update_one(myquery, newvalues)
async def is_user_verified(self, user_id):
user = await self.get_notcopy_user(user_id)
try:
pastDate = user["last_verified"]
except Exception:
user = await self.get_notcopy_user(user_id)
pastDate = user["last_verified"]
ist_timezone = pytz.timezone('Asia/Kolkata')
pastDate = pastDate.astimezone(ist_timezone)
current_time = datetime.datetime.now(tz=ist_timezone)
seconds_since_midnight = (current_time - datetime.datetime(current_time.year, current_time.month, current_time.day, 0, 0, 0, tzinfo=ist_timezone)).total_seconds()
time_diff = current_time - pastDate
total_seconds = time_diff.total_seconds()
return total_seconds <= seconds_since_midnight
async def user_verified(self, user_id):
user = await self.get_notcopy_user(user_id)
try:
pastDate = user["second_time_verified"]
except Exception:
user = await self.get_notcopy_user(user_id)
pastDate = user["second_time_verified"]
ist_timezone = pytz.timezone('Asia/Kolkata')
pastDate = pastDate.astimezone(ist_timezone)
current_time = datetime.datetime.now(tz=ist_timezone)
seconds_since_midnight = (current_time - datetime.datetime(current_time.year, current_time.month, current_time.day, 0, 0, 0, tzinfo=ist_timezone)).total_seconds()
time_diff = current_time - pastDate
total_seconds = time_diff.total_seconds()
return total_seconds <= seconds_since_midnight
async def use_second_shortener(self, user_id, time):
user = await self.get_notcopy_user(user_id)
if not user.get("second_time_verified"):
ist_timezone = pytz.timezone('Asia/Kolkata')
await self.update_notcopy_user(user_id, {"second_time_verified":datetime.datetime(2019, 5, 17, 0, 0, 0, tzinfo=ist_timezone)})
user = await self.get_notcopy_user(user_id)
if await self.is_user_verified(user_id):
try:
pastDate = user["last_verified"]
except Exception:
user = await self.get_notcopy_user(user_id)
pastDate = user["last_verified"]
ist_timezone = pytz.timezone('Asia/Kolkata')
pastDate = pastDate.astimezone(ist_timezone)
current_time = datetime.datetime.now(tz=ist_timezone)
time_difference = current_time - pastDate
if time_difference > datetime.timedelta(seconds=time):
pastDate = user["last_verified"].astimezone(ist_timezone)
second_time = user["second_time_verified"].astimezone(ist_timezone)
return second_time < pastDate
return False
async def use_third_shortener(self, user_id, time):
user = await self.get_notcopy_user(user_id)
if not user.get("third_time_verified"):
ist_timezone = pytz.timezone('Asia/Kolkata')
await self.update_notcopy_user(user_id, {"third_time_verified":datetime.datetime(2018, 5, 17, 0, 0, 0, tzinfo=ist_timezone)})
user = await self.get_notcopy_user(user_id)
if await self.user_verified(user_id):
try:
pastDate = user["second_time_verified"]
except Exception:
user = await self.get_notcopy_user(user_id)
pastDate = user["second_time_verified"]
ist_timezone = pytz.timezone('Asia/Kolkata')
pastDate = pastDate.astimezone(ist_timezone)
current_time = datetime.datetime.now(tz=ist_timezone)
time_difference = current_time - pastDate
if time_difference > datetime.timedelta(seconds=time):
pastDate = user["second_time_verified"].astimezone(ist_timezone)
second_time = user["third_time_verified"].astimezone(ist_timezone)
return second_time < pastDate
return False
async def create_verify_id(self, user_id: int, hash):
res = {"user_id": user_id, "hash":hash, "verified":False}
return await self.verify_id.insert_one(res)
async def get_verify_id_info(self, user_id: int, hash):
return await self.verify_id.find_one({"user_id": user_id, "hash": hash})
async def update_verify_id_info(self, user_id, hash, value: dict):
myquery = {"user_id": user_id, "hash": hash}
newvalues = { "$set": value }
return await self.verify_id.update_one(myquery, newvalues)
async def get_user(self, user_id):
user_data = await self.users.find_one({"id": user_id})
return user_data
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 update_user(self, user_data):
await self.users.update_one({"id": user_data["id"]}, {"$set": user_data}, upsert=True)
async def get_expired(self, current_time):
expired_users = []
if data := self.users.find({"expiry_time": {"$lt": current_time}}):
async for user in data:
expired_users.append(user)
return expired_users
async def has_premium_access(self, user_id):
user_data = await self.get_user(user_id)
if user_data:
expiry_time = user_data.get("expiry_time")
if expiry_time is None:
# User previously used the free trial, but it has ended.
return False
elif isinstance(expiry_time, datetime.datetime) and datetime.datetime.now() <= expiry_time:
return True
else:
await self.users.update_one({"id": user_id}, {"$set": {"expiry_time": None}})
return False
async def check_remaining_uasge(self, user_id):
user_id = user_id
user_data = await self.get_user(user_id)
expiry_time = user_data.get("expiry_time")
# Calculate remaining time
remaining_time = expiry_time - datetime.datetime.now()
return remaining_time
async def all_premium_users(self):
count = await self.users.count_documents({
"expiry_time": {"$gt": datetime.datetime.now()}
})
return count
async def update_one(self, filter_query, update_data):
try:
# Assuming self.client and self.users are set up properly
result = await self.users.update_one(filter_query, update_data)
return result.matched_count == 1
except Exception as e:
print(f"Error updating document: {e}")
return False
async def remove_premium_access(self, user_id):
return await self.update_one(
{"id": user_id}, {"$set": {"expiry_time": None}}
)
async def check_trial_status(self, user_id):
user_data = await self.get_user(user_id)
if user_data:
return user_data.get("has_free_trial", False)
return False
# Free Trail Remove Logic
async def reset_free_trial(self, user_id=None):
if user_id is None:
# Reset for all users
update_data = {"$set": {"has_free_trial": False}}
result = await self.users.update_many({}, update_data) # Empty query to match all users
return result.modified_count
else:
# Reset for a specific user
update_data = {"$set": {"has_free_trial": False}}
result = await self.users.update_one({"id": user_id}, update_data)
return 1 if result.modified_count > 0 else 0 # Return 1 if updated, 0 if not
async def give_free_trial(self, user_id):
#await set_free_trial_status(user_id)
user_id = user_id
seconds = 5*60
expiry_time = datetime.datetime.now() + datetime.timedelta(seconds=seconds)
user_data = {"id": user_id, "expiry_time": expiry_time, "has_free_trial": True}
await self.users.update_one({"id": user_id}, {"$set": user_data}, upsert=True)
# JISSHU BOTS = @IM_JISSHU
async def jisshu_set_ads_link(self,link):
await self.jisshu_ads_link.update_one({} , {'$set': {'link': link}} , upsert=True)
async def jisshu_get_ads_link(self):
link = await self.jisshu_ads_link.find_one({})
if link is not None:
return link.get("link")
else:
return None
async def jisshu_del_ads_link(self):
try:
isDeleted = await self.jisshu_ads_link.delete_one({})
if isDeleted.deleted_count > 0:
return True
else:
return False
except Exception as e:
print(f"Got err in db set : {e}")
return False
async def setFsub(self , grpID , fsubID):
return await self.grp_and_ids.update_one({'grpID': grpID} , {'$set': {'grpID': grpID , "fsubID": fsubID}}, upsert=True)
async def getFsub(self , grpID):
link = await self.grp_and_ids.find_one({"grpID": grpID})
if link is not None:
return link.get("fsubID")
else:
return None
async def delFsub(self , grpID):
result = await self.grp_and_ids.delete_one({"grpID": grpID})
if result.deleted_count != 0:
return True
else:
return False
async def get_pm_search_status(self, bot_id):
bot = await self.botcol.find_one({'id': bot_id})
if bot and bot.get('bot_pm_search'):
return bot['bot_pm_search']
else:
return IS_PM_SEARCH
async def update_pm_search_status(self, bot_id, enable):
bot = await self.botcol.find_one({'id': int(bot_id)})
if bot:
await self.botcol.update_one({'id': int(bot_id)}, {'$set': {'bot_pm_search': enable}})
else:
await self.botcol.insert_one({'id': int(bot_id), 'bot_pm_search': enable})
async def movies_update_channel_id(self , id=None):
if id is None:
myLinks = await self.movies_update_channel.find_one({})
if myLinks is not None:
return myLinks.get("id")
else:
return None
return await self.movies_update_channel.update_one({} , {'$set': {'id': id}} , upsert=True)
db = Database()