-
Notifications
You must be signed in to change notification settings - Fork 917
Add /rban command allowing remote bans #13
base: master
Are you sure you want to change the base?
Changes from 2 commits
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
is_user_admin, is_user_in_chat | ||
from tg_bot.modules.helper_funcs.extraction import extract_user_and_text | ||
from tg_bot.modules.log_channel import loggable | ||
|
||
from tg_bot.modules.helper_funcs.filters import CustomFilters | ||
|
||
@run_async | ||
@bot_admin | ||
|
@@ -191,6 +191,82 @@ def unban(bot: Bot, update: Update, args: List[str]) -> str: | |
return log | ||
|
||
|
||
@run_async | ||
@bot_admin | ||
def rban(bot: Bot, update: Update, args: List[str]): | ||
message = update.effective_message | ||
|
||
if not args: | ||
message.reply_text("You don't seem to be referring to a chat/user.") | ||
return | ||
|
||
user_id, chat_id = extract_user_and_text(message, args) | ||
|
||
if not user_id: | ||
message.reply_text("You don't seem to be referring to a user.") | ||
return | ||
elif not chat_id: | ||
message.reply_text("You don't seem to be referring to a chat.") | ||
return | ||
|
||
try: | ||
chat = bot.get_chat(chat_id) | ||
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. Small nitpick - if people write after the chat_id, that text will be included. how about .split() and take [0]? avoid as many errors as possible, as early as possible |
||
except BadRequest as excp: | ||
if excp.message == "Chat not found": | ||
message.reply_text("Chat not found! Make sure you entered a valid chat ID and I'm part of that chat.") | ||
return | ||
else: | ||
raise | ||
|
||
if chat.type == 'private': | ||
message.reply_text("I'm sorry, but that's a private chat!") | ||
return | ||
|
||
if not is_bot_admin(chat, bot.id) and not chat.get_member(bot.id).can_restrict_members: | ||
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. you mean 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. Oh crap. 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. De Morgan law bugs - this is why code review is good |
||
message.reply_text("I can't restrict people there! Make sure I'm admin and can ban users.") | ||
return | ||
|
||
try: | ||
member = chat.get_member(user_id) | ||
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. Check if chat is a group chat first. If you've loaded a private chat, how are you gonna ban? |
||
except BadRequest as excp: | ||
if excp.message == "User not found": | ||
message.reply_text("I can't seem to find this user") | ||
return | ||
else: | ||
raise | ||
|
||
if is_user_ban_protected(chat, user_id, member): | ||
message.reply_text("I really wish I could ban admins...") | ||
return | ||
|
||
if user_id == bot.id: | ||
message.reply_text("I'm not gonna BAN myself, are you crazy?") | ||
return | ||
|
||
try: | ||
chat.kick_member(user_id) | ||
message.reply_text("Banned!") | ||
except BadRequest as excp: | ||
if excp.message == "Reply message not found": | ||
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. You're going to need more exception catching here, see the possible gban exceptions. |
||
# Do not reply | ||
message.reply_text('Banned!', quote=False) | ||
elif excp.message == "User_not_participant": | ||
message.reply_text("This user is not a participant of the chat!") | ||
elif excp.message == "Group chat was deactivated": | ||
message.reply_text("This group chat was deactivated!") | ||
elif excp.message == "Need to be inviter of a user to kick it from a basic group": | ||
message.reply_text(excp.message) | ||
elif excp.message == "Only the creator of a basic group can kick group administrators": | ||
message.reply_text(excp.message) | ||
elif excp.message == "Peer_id_invalid": | ||
message.reply_text("Could not ban user. Perhaps the group has been suspended by Telegram.") | ||
else: | ||
LOGGER.warning(update) | ||
LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, | ||
excp.message) | ||
message.reply_text("Well damn, I can't ban that user.") | ||
|
||
|
||
__help__ = """ | ||
- /kickme: kicks the user who issued the command | ||
|
||
|
@@ -206,8 +282,10 @@ def unban(bot: Bot, update: Update, args: List[str]) -> str: | |
KICK_HANDLER = CommandHandler("kick", kick, pass_args=True, filters=Filters.group) | ||
UNBAN_HANDLER = CommandHandler("unban", unban, pass_args=True, filters=Filters.group) | ||
KICKME_HANDLER = DisableAbleCommandHandler("kickme", kickme, filters=Filters.group) | ||
RBAN_HANDLER = CommandHandler("rban", rban, pass_args=True, filters=CustomFilters.sudo_filter) | ||
|
||
dispatcher.add_handler(BAN_HANDLER) | ||
dispatcher.add_handler(KICK_HANDLER) | ||
dispatcher.add_handler(UNBAN_HANDLER) | ||
dispatcher.add_handler(KICKME_HANDLER) | ||
dispatcher.add_handler(RBAN_HANDLER) |
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.
All the
return ""
are useless, given this hasn't been set as @loggable. Use normalreturn
instead.