2023-06-28 11:15:45 +03:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
from bson import ObjectId
|
|
|
|
from libbot import config_get
|
|
|
|
from libbot.pyrogram.classes import PyroClient
|
|
|
|
|
|
|
|
from modules.database import col_users
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PyroUser:
|
|
|
|
"""Dataclass of DB entry of a user"""
|
|
|
|
|
|
|
|
_id: ObjectId
|
|
|
|
id: int
|
|
|
|
locale: Union[str, None]
|
|
|
|
banned: bool
|
|
|
|
cooldown: datetime
|
|
|
|
subscription: dict
|
|
|
|
|
2023-08-14 15:52:02 +03:00
|
|
|
async def update_locale(self, locale: str) -> None:
|
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
|
2023-06-28 11:15:45 +03:00
|
|
|
|
2023-08-14 15:52:02 +03:00
|
|
|
async def update_cooldown(self, time: datetime = datetime.now()) -> None:
|
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"cooldown": time}})
|
2023-06-28 11:15:45 +03:00
|
|
|
|
|
|
|
async def block(self) -> None:
|
|
|
|
"""Ban user from using command and submitting content."""
|
2023-08-14 15:52:02 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"banned": True}})
|
2023-06-28 11:15:45 +03:00
|
|
|
|
|
|
|
async def unblock(self) -> None:
|
|
|
|
"""Allow user to use command and submit posts again."""
|
2023-08-14 15:52:02 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"banned": False}})
|
2023-06-28 11:15:45 +03:00
|
|
|
|
|
|
|
async def is_limited(self, app: Union[PyroClient, None] = None) -> bool:
|
|
|
|
"""Check if user is on a cooldown after submitting something.
|
|
|
|
|
|
|
|
### Returns:
|
|
|
|
`bool`: Must be `True` if on the cooldown and `False` if not
|
|
|
|
"""
|
|
|
|
admins = (
|
2023-08-14 15:52:02 +03:00
|
|
|
await config_get("admins", "bot") + [await config_get("owner", "bot")]
|
|
|
|
if app is None
|
|
|
|
else app.admins
|
2023-06-28 11:15:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return (datetime.now() - self.cooldown).total_seconds() < (
|
|
|
|
app.config["submission"]["timeout"]
|
|
|
|
if app is not None
|
|
|
|
else await config_get("timeout", "submission")
|
|
|
|
)
|