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

56 lines
1.8 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
2023-03-09 12:33:02 +02:00
class PosterUser:
2023-02-17 22:54:52 +02:00
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-03-09 12:33:02 +02:00
"""
2023-02-17 22:54:52 +02:00
return False if col_banned.find_one({"user": self.id}) is None else True
2023-03-09 12:33:02 +02:00
2023-02-17 22:54:52 +02:00
def block(self) -> None:
2023-03-09 12:33:02 +02:00
"""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:
2023-03-09 12:33:02 +02:00
"""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-03-09 12:33:02 +02:00
"""
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
2023-03-09 12:33:02 +02:00
return (
True
if (datetime.now() - db_record["cooldown"]).total_seconds()
< configGet("timeout", "submission")
else False
)
2023-02-17 23:48:37 +02:00
def limit(self) -> None:
2023-03-09 12:33:02 +02:00
"""Restart user's cooldown. Used after post has been submitted."""
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()})