MissKatyPyro/database/warn_db.py
yasirarism a7008c664e
Reformat using black (#120)
* ci: Update .deepsource.toml

* ci: Update .deepsource.toml

* style: format code with black and isort

Format code with black and isort

This commit fixes the style issues introduced in 0fb651a according to the output
from Black and isort.

Details: https://app.deepsource.com/gh/yasirarism/MissKatyPyro/transform/d8f2f66e-b496-4686-aca6-9830236eda12/

---------

Co-authored-by: deepsource-io[bot] <42547082+deepsource-io[bot]@users.noreply.github.com>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
2023-06-27 11:07:42 +07:00

51 lines
1.3 KiB
Python

from typing import Dict, Union
from database import dbname
warnsdb = dbname["warn"]
async def get_warns_count() -> dict:
chats_count = 0
warns_count = 0
async for chat in warnsdb.find({"chat_id": {"$lt": 0}}):
for user in chat["warns"]:
warns_count += chat["warns"][user]["warns"]
chats_count += 1
return {"chats_count": chats_count, "warns_count": warns_count}
async def get_warns(chat_id: int) -> Dict[str, int]:
warns = await warnsdb.find_one({"chat_id": chat_id})
return warns["warns"] if warns else {}
async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
name = name.lower().strip()
warns = await get_warns(chat_id)
if name in warns:
return warns[name]
async def add_warn(chat_id: int, name: str, warn: dict):
name = name.lower().strip()
warns = await get_warns(chat_id)
warns[name] = warn
await warnsdb.update_one(
{"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
)
async def remove_warns(chat_id: int, name: str) -> bool:
warnsd = await get_warns(chat_id)
name = name.lower().strip()
if name in warnsd:
del warnsd[name]
await warnsdb.update_one(
{"chat_id": chat_id},
{"$set": {"warns": warnsd}},
upsert=True,
)
return True
return False