diff --git a/driver/database/dbchat.py b/driver/database/dbchat.py new file mode 100644 index 0000000..1b881c5 --- /dev/null +++ b/driver/database/dbchat.py @@ -0,0 +1,38 @@ +""" chat database """ + +from typing import Dict, List, Union + +from driver.database.dblocal import db + +chatsdb = db.chats + + +async def get_served_chats() -> list: + chats = chatsdb.find({"chat_id": {"$lt": 0}}) + if not chats: + return [] + chats_list = [] + for chat in await chats.to_list(length=1000000000): + chats_list.append(chat) + return chats_list + + +async def is_served_chat(chat_id: int) -> bool: + chat = await chatsdb.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 chatsdb.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 chatsdb.delete_one({"chat_id": chat_id}) diff --git a/driver/database/dblocal.py b/driver/database/dblocal.py new file mode 100644 index 0000000..c1e4d9f --- /dev/null +++ b/driver/database/dblocal.py @@ -0,0 +1,8 @@ +""" mongo database """ + +from motor.motor_asyncio import AsyncIOMotorClient as Bot +from config import MONGO_DB_URI as tmo + + +MONGODB_CLI = Bot(tmo) +db = MONGODB_CLI.program diff --git a/driver/database/dbpunish.py b/driver/database/dbpunish.py new file mode 100644 index 0000000..029fe1c --- /dev/null +++ b/driver/database/dbpunish.py @@ -0,0 +1,32 @@ +from typing import Dict, List, Union + +from driver.database.dblocal import db + +gbansdb = db.gban + + +async def get_gbans_count() -> int: + users = gbansdb.find({"user_id": {"$gt": 0}}) + users = await users.to_list(length=100000) + return len(users) + + +async def is_gbanned_user(user_id: int) -> bool: + user = await gbansdb.find_one({"user_id": user_id}) + if not user: + return False + return True + + +async def add_gban_user(user_id: int): + is_gbanned = await is_gbanned_user(user_id) + if is_gbanned: + return + return await gbansdb.insert_one({"user_id": user_id}) + + +async def remove_gban_user(user_id: int): + is_gbanned = await is_gbanned_user(user_id) + if not is_gbanned: + return + return await gbansdb.delete_one({"user_id": user_id})