-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: warn and mute command #142
base: main
Are you sure you want to change the base?
Changes from 27 commits
f91bdc3
3d8ded5
e83c83a
1bea2f0
7c6d00f
5ad7ca1
003656e
95ea13b
ed9b959
5f5a31a
f3c2c9a
f09d904
c7732d1
e7f417b
f0d3625
76022db
4bc2e84
1871ffc
6fa7dae
7d1c699
40c342d
ca26e18
6620ffb
94a0844
7380b9b
af0f98d
f724895
f6b48e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Defines custom filters to use for commands""" | ||
from telegram import Chat, Message | ||
from telegram.ext.filters import MessageFilter | ||
|
||
|
||
class IsAdminFilter(MessageFilter): | ||
"""Check if the message from the update was sent by | ||
one of the administrators of the group | ||
Args: | ||
MessageFilter: the superclass for the filter | ||
""" | ||
|
||
def filter(self, message: Message): | ||
chat = message.chat | ||
sender_id = message.from_user.id | ||
if chat.type in [Chat.SUPERGROUP, Chat.GROUP]: | ||
return sender_id in [admin.id for admin in chat.get_administrators(chat.id)] | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting Filter. It opens quite a few possibilities There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice 🚀 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,27 @@ 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(): | ||
"""Job called each day at 05:00 utc. | ||
Removed users who have been warned for longer than setting duration | ||
Args: | ||
alepiaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context: context passed by the jobqueue | ||
""" | ||
warn_expiration = datetime.now() + timedelta(days=Config.post_get("warn_after_days")) | ||
DbManager.delete_from( | ||
table_name="warned_users", | ||
where="warn_time > %s", | ||
where_args=(warn_expiration,), | ||
) | ||
Comment on lines
+74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends on how you implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure 🤔 |
||
|
||
|
||
async def unmute_user(context: CallbackContext): | ||
"""A callback function that unmute the user | ||
|
||
Args: | ||
context: context passed by the job queue | ||
""" | ||
user = context.job.context | ||
user.unmute(context.bot) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""/mute command""" | ||
import re | ||
from datetime import timedelta | ||
|
||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
|
||
from spotted.data import User | ||
from spotted.utils import EventInfo | ||
|
||
from .job_handlers import unmute_user | ||
|
||
|
||
async def mute_cmd(update: Update, context: CallbackContext): | ||
"""Handles the /mute command. | ||
Mute a user by replying to one of his message in the comment group with /mute <n_days> | ||
|
||
Args: | ||
update: update event | ||
context: context passed by the handler | ||
""" | ||
info = EventInfo.from_message(update, context) | ||
g_message = update.message.reply_to_message | ||
user = User(g_message.from_user.id) | ||
match = re.search(r"^/mute (?P<days>\\d*)$", info.text) | ||
|
||
if g_message is None or match is None: | ||
await info.bot.send_message( | ||
chat_id=info.chat_id, | ||
text="Per mutare qualcuno, rispondi al suo commento con /mute <giorni>", | ||
) | ||
return | ||
|
||
days = 1 if match == "" else int(match) # if no argv is provided, default is 1 day | ||
|
||
user.mute(info.bot) | ||
text = f"L'utente è stato mutato per {days} giorn{'o' if days == 1 else 'i'}." | ||
await info.bot.send_message(chat_id=info.chat_id, text=text) | ||
context.job_queue.run_once(unmute_user, timedelta(days=days), context=user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This job probably gets lost and the user remains muted if, for any reason, the bot gets restarted on the VM. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I was actually not sure about putting the timedelta in the db table or not, in that case I might as well starting the job_queue on each bot restart rather than checking every day |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""/warn command""" | ||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
|
||
from spotted.data import Config, User | ||
from spotted.utils import EventInfo | ||
|
||
|
||
async def warn_cmd(update: Update, context: CallbackContext): | ||
"""Handles the /warn command. | ||
Warn a user by replying to one of his message in the comment group with /warn | ||
|
||
Args: | ||
update: update event | ||
context: context passed by the handler | ||
""" | ||
info = EventInfo.from_message(update, context) | ||
g_message = update.message.reply_to_message | ||
if g_message is None: | ||
await info.bot.send_message( | ||
chat_id=info.chat_id, | ||
text="Per warnare qualcuno, rispondi al suo commento con /warn", | ||
) | ||
return | ||
user = User(g_message.from_user.id) | ||
n_warns = user.get_n_warns() | ||
text = "L'utente " | ||
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: | ||
text += "è stato bannato." | ||
await info.bot.ban_chat_member(Config.post_get("community_group_id"), user.user_id) | ||
user.ban() | ||
await info.bot.send_message(chat_id=info.chat_id, text=text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no timestamp here?