From 972827d6c278bba93b44711420e2c21b9eeb3134 Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 2 Apr 2023 16:44:46 +0200 Subject: [PATCH] Does some tasks from #34 --- config_example.json | 4 ++++ modules/commands/warn.py | 2 ++ modules/commands/warnings.py | 4 ++-- modules/scheduled.py | 46 +++++++++++++++++++++++++++++++++++- validation/warnings.json | 25 +++++++++++++++++--- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/config_example.json b/config_example.json index 1b4d791..ba435f8 100644 --- a/config_example.json +++ b/config_example.json @@ -82,6 +82,10 @@ "interval": 5, "enabled": true, "channels": [] + }, + "warnings_revocation": { + "interval": 6, + "enabled": true } }, "locations": { diff --git a/modules/commands/warn.py b/modules/commands/warn.py index a0b3761..578bc93 100644 --- a/modules/commands/warn.py +++ b/modules/commands/warn.py @@ -24,6 +24,8 @@ async def cmd_warn(app: Client, msg: Message): "admin": msg.from_user.id, "date": datetime.now(), "reason": message, + "active": True, + "revoke_date": None, } ) if message == "": diff --git a/modules/commands/warnings.py b/modules/commands/warnings.py index 0f12ba2..7d49250 100644 --- a/modules/commands/warnings.py +++ b/modules/commands/warnings.py @@ -23,7 +23,7 @@ async def cmd_warnings(app: Client, msg: Message): return try: - user_db = col_users.find_one({"user": int(msg.command[1])}) + user_db = col_users.find_one({"user": int(msg.command[1]), "active": True}) target_id = user_db["user"] target_name = user_db["tg_name"] except: @@ -47,7 +47,7 @@ async def cmd_warnings(app: Client, msg: Message): ) return - warns = col_warnings.count_documents({"user": target_id}) + warns = col_warnings.count_documents({"user": target_id, "active": True}) if warns == 0: await msg.reply_text( diff --git a/modules/scheduled.py b/modules/scheduled.py index 29a34a2..2cc2a81 100644 --- a/modules/scheduled.py +++ b/modules/scheduled.py @@ -18,7 +18,12 @@ from pyrogram.enums.chat_members_filter import ChatMembersFilter from classes.holo_user import HoloUser from modules.utils import configGet, jsonLoad, jsonSave, locale, logWrite from dateutil.relativedelta import relativedelta -from modules.database import col_applications, col_sponsorships, col_youtube +from modules.database import ( + col_applications, + col_sponsorships, + col_youtube, + col_warnings, +) from xmltodict import parse from requests import get @@ -182,6 +187,45 @@ if configGet("enabled", "features", "sponsorships") is True: logWrite("Sponsorships check performed") +# Revoke old warnings +if configGet("enabled", "features", "warnings") is True: + if configGet("enabled", "scheduler", "warnings_revocation") is True: + + @scheduler.scheduled_job( + trigger="date", run_date=datetime.now() + timedelta(seconds=10) + ) + @scheduler.scheduled_job( + trigger="interval", + hours=configGet("interval", "scheduler", "warnings_revocation"), + ) + async def revoke_warnings(): + for warning in list( + col_warnings.find( + { + "active": True, + "date": {"$lt": datetime.now() - timedelta(days=90)}, + } + ) + ): + if ( + col_warnings.count_documents( + { + "user": warning["user"], + "active": True, + "date": {"$gt": datetime.now() - timedelta(days=90)}, + } + ) + == 0 + ): + col_warnings.update_one( + {"_id": warning["_id"]}, + {"$set": {"active": False, "revoke_date": datetime.now()}}, + ) + logWrite( + f'Revoked warning {str(warning["_id"])} of user {warning["user"]} because no active warnings for the last 90 days found.' + ) + + # Register all bot commands @scheduler.scheduled_job( trigger="date", run_date=datetime.now() + timedelta(seconds=10) diff --git a/validation/warnings.json b/validation/warnings.json index 6712a4a..51694a8 100644 --- a/validation/warnings.json +++ b/validation/warnings.json @@ -4,15 +4,23 @@ "user", "admin", "date", - "reason" + "reason", + "active", + "revoke_date" ], "properties": { "user": { - "bsonType": ["int", "long"], + "bsonType": [ + "int", + "long" + ], "description": "Telegram ID of user" }, "admin": { - "bsonType": ["int", "long"], + "bsonType": [ + "int", + "long" + ], "description": "Telegram ID of admin" }, "date": { @@ -22,6 +30,17 @@ "reason": { "bsonType": "string", "description": "Broken rule or admin's comment" + }, + "active": { + "bsonType": "bool", + "description": "Whether warning is still present" + }, + "revoke_date": { + "bsonType": [ + "date", + "null" + ], + "description": "Date when warning got inactive" } } }