This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/classes/user.py

45 lines
1.7 KiB
Python
Raw Normal View History

2023-02-17 23:48:37 +02:00
from modules.app import app
2023-02-17 22:54:52 +02:00
from datetime import datetime
2023-02-17 23:48:37 +02:00
from modules.database import col_banned, col_users
from modules.utils import configGet
2023-02-17 22:54:52 +02:00
class PosterUser():
def __init__(self, id: int):
self.id = id
def is_blocked(self) -> bool:
"""Check if user is banned from submitting content.
### Returns:
`bool`: Must be `True` if banned and `False` if not
"""
2023-02-17 22:54:52 +02:00
return False if col_banned.find_one({"user": self.id}) is None else True
def block(self) -> None:
"""Ban user from using command and submitting content."""
2023-02-17 22:54:52 +02:00
if col_banned.find_one({"user": self.id}) is None:
col_banned.insert_one({"user": self.id, "date": datetime.now()})
def unblock(self) -> None:
"""Allow user to use command and submit posts again."""
2023-02-17 23:48:37 +02:00
col_banned.find_one_and_delete({"user": self.id})
def is_limited(self) -> bool:
"""Check if user is on a cooldown after submitting something.
### Returns:
`bool`: Must be `True` if on the cooldown and `False` if not
"""
2023-02-17 23:48:37 +02:00
if self.id in app.admins:
return False
else:
db_record = col_users.find_one({"user": self.id})
if db_record is None:
return False
return True if (datetime.now() - db_record["cooldown"]).total_seconds() < configGet("timeout", "submission") else False
def limit(self) -> None:
"""Restart user's cooldown. Used after post has been submitted."""
2023-02-17 23:48:37 +02:00
if col_users.find_one_and_update({"user": self.id}, {"$set": {"cooldown": datetime.now()}}) is None:
col_users.insert_one({"user": self.id, "cooldown": datetime.now()})