forked from szbots/Fake-Mail-BOT
-
Notifications
You must be signed in to change notification settings - Fork 7
/
database.py
71 lines (59 loc) · 2.25 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
#=================================================================================================
# Copyright (C) 2022 by szsupunma@Github, < https://github.com/szsupunma >.
# Released under the "GNU v3.0 License Agreement".
# All rights reserved.
#=================================================================================================
import os
from motor.motor_asyncio import AsyncIOMotorClient
DATABASE = os.environ["DATABASE"]
mongo_client = AsyncIOMotorClient(DATABASE)
db = mongo_client.users
userdb = db.users
#===================== User database ================================
async def is_served_user(user_id: int) -> bool:
user = await userdb.find_one({"bot_users": user_id})
if not user:
return False
return True
async def get_served_users() -> list:
users = userdb.find({"bot_users": {"$gt": 0}})
if not users:
return []
users_list = []
for user in await users.to_list(length=1000000000):
users_list.append(user)
return users_list
async def add_served_user(user_id: int):
is_served = await is_served_user(user_id)
if is_served:
return
return await userdb.insert_one({"bot_users": user_id})
async def remove_served_user(user_id: int):
is_served = await is_served_user(user_id)
if is_served:
return
return await userdb.delete_one({"bot_users": user_id})
#===================== groups database ================================
async def get_served_chats() -> list:
chats = userdb.find({"chat_id": {"$lt": 0}})
if not chats:
return []
chats_list = []
async for chat in userdb.find({"chat_id": {"$lt": 0}}):
chats_list.append(chat)
return chats_list
async def is_served_chat(chat_id: int) -> bool:
chat = await userdb.find_one({"chat_id": chat_id})
if not chat:
return False
return True
async def add_served_chat(chat_id: int):
is_served = await is_served_chat(chat_id)
if is_served:
return
return await userdb.insert_one({"chat_id": chat_id})
async def remove_served_chat(chat_id: int):
is_served = await is_served_chat(chat_id)
if not is_served:
return
return await userdb.delete_one({"chat_id": chat_id})