Does some tasks from #34

This commit is contained in:
Profitroll 2023-04-02 16:44:46 +02:00
parent 43e71c95c4
commit 972827d6c2
5 changed files with 75 additions and 6 deletions

View File

@ -82,6 +82,10 @@
"interval": 5,
"enabled": true,
"channels": []
},
"warnings_revocation": {
"interval": 6,
"enabled": true
}
},
"locations": {

View File

@ -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 == "":

View File

@ -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(

View File

@ -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)

View File

@ -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"
}
}
}