diff --git a/src/spotted/config/yaml/settings.yaml.default b/src/spotted/config/yaml/settings.yaml.default new file mode 100644 index 00000000..ae19d855 --- /dev/null +++ b/src/spotted/config/yaml/settings.yaml.default @@ -0,0 +1,24 @@ +debug: + local_log: false + reset_on_load: false + log_file: "logs/spotted.log" + log_error_file: "logs/spotted_error.log" + db_file: "spotted.sqlite3" +post: + community_group_id: -1 + channel_id: -2 + channel_tag: "@channel_tag" + comments: true + admin_group_id: -3 + n_votes: 2 + remove_after_h: 12 + report: true + report_wait_mins: 30 + replace_anonymous_comments: true + delete_anonymous_comments: true + reject_after_autoreply: true + autoreplies_per_page: 6 + warn_after_days: 10 + max_n_warns : 3 +token: "" +bot_tag: "@bot_tag" \ No newline at end of file diff --git a/src/spotted/handlers/__init__.py b/src/spotted/handlers/__init__.py index 587fda9d..29111944 100644 --- a/src/spotted/handlers/__init__.py +++ b/src/spotted/handlers/__init__.py @@ -1,5 +1,5 @@ """Modules that handle the events the bot recognizes and reacts to""" -from datetime import time +from datetime import time, timedelta from warnings import filterwarnings from pytz import utc @@ -27,7 +27,7 @@ from .follow_spot import follow_spot_callback from .forwarded_post import forwarded_post_msg from .help import help_cmd -from .job_handlers import clean_pending_job, db_backup_job +from .job_handlers import clean_pending_job, clean_warned_users, db_backup_job from .purge import purge_cmd from .reload import reload_cmd from .reply import reply_cmd @@ -147,3 +147,4 @@ def add_jobs(app: Application): """ app.job_queue.run_daily(clean_pending_job, time=time(hour=5, tzinfo=utc)) # run each day at 05:00 utc app.job_queue.run_daily(db_backup_job, time=time(hour=5, tzinfo=utc)) # run each day at 05:00 utc + app.job_queue.run_daily(clean_warned_users, time=time(hour=5, tzinfo=utc)) # run each day at 05:00 utc diff --git a/src/spotted/handlers/job_handlers.py b/src/spotted/handlers/job_handlers.py index 976be72c..3d404b1f 100644 --- a/src/spotted/handlers/job_handlers.py +++ b/src/spotted/handlers/job_handlers.py @@ -4,7 +4,7 @@ from telegram.error import BadRequest, Forbidden from telegram.ext import CallbackContext -from spotted.data import Config, PendingPost +from spotted.data import Config, DbManager, PendingPost from spotted.debug import logger from spotted.utils import EventInfo @@ -62,3 +62,19 @@ async def db_backup_job(context: CallbackContext): ) except Exception as ex: # pylint: disable=broad-except await context.bot.send_message(chat_id=admin_group_id, text=f"✖️ Impossibile effettuare il backup\n\n{ex}") + + +async def clean_warned_users(context: CallbackContext): + """Job called each day at 05:00 utc. + Removed users who have been warned for longer than setting duration + Args: + context (CallbackContext): _description_ + """ + warn_expiration = datetime.now() + timedelta(days=Config.post_get("warn_after_days")) + DbManager.select_from( + table_name="warned_users", + select=("user_id", "MAX(warn_time)"), + where="warn_time > %s", + where_args=(warn_expiration,), + group_by="user_id", + ) diff --git a/src/spotted/handlers/warn.py b/src/spotted/handlers/warn.py index 6949986d..4a1b214c 100644 --- a/src/spotted/handlers/warn.py +++ b/src/spotted/handlers/warn.py @@ -25,7 +25,8 @@ async def warn_cmd(update: Update, context: CallbackContext): user = User(g_message.from_user.id) n_warns = user.get_n_warns() text = f"L'utente {g_message.from_user.name} " - if n_warns < 2: + max_warns = Config.post_get("max_n_warns") + if n_warns < max_warns: user.warn() text += f"ha ricevuto {n_warns + 1} warn(s)" else: