create database

This commit is contained in:
levina 2022-01-23 19:41:37 +07:00 committed by GitHub
parent 55c952ab11
commit 5c68699e2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 0 deletions

38
driver/database/dbchat.py Normal file
View File

@ -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})

View File

@ -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

View File

@ -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})