Skip to content

Commit

Permalink
feat: added daily job to clean warned users if their warn time has ex…
Browse files Browse the repository at this point in the history
…pired
  • Loading branch information
alepiaz committed Nov 27, 2023
1 parent 003656e commit 95ea13b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/spotted/config/yaml/settings.yaml.default
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions src/spotted/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
18 changes: 17 additions & 1 deletion src/spotted/handlers/job_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
)
3 changes: 2 additions & 1 deletion src/spotted/handlers/warn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 95ea13b

Please sign in to comment.