def kick(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] user_id, reason = extract_user_and_text(message, args) if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_kick_perm")) return "" if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found.": message.reply_text(tld(chat.id, "bans_err_usr_not_found")) return "" else: raise if user_id == bot.id: message.reply_text(tld(chat.id, "bans_kick_is_bot")) return "" if is_user_ban_protected(chat, user_id): message.reply_text(tld(chat.id, "bans_kick_is_admin")) return "" res = chat.unban_member(user_id) # unban on current user = kick if res: reply = tld(chat.id, "bans_kick_success").format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title)) if reason: reply += tld(chat.id, "bans_logger_reason").format(reason) message.reply_text(reply, parse_mode=ParseMode.HTML) log = tld(chat.id, "bans_kick_logger").format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) return log else: message.reply_text(tld(chat.id, "bans_err_unknown").format("kicking")) return ""
def sban(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_ban_perm")) return "" update.effective_message.delete() user_id, reason = extract_user_and_text(message, args) if not user_id: return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": return "" else: raise if is_user_ban_protected(chat, user_id, member): return "" if user_id == bot.id: return "" log = tld(chat.id, "bans_sban_logger").format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) try: chat.kick_member(user_id) return log except BadRequest as excp: if excp.message == "Reply message not found": return log 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) return ""
def unban(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] user_id, reason = extract_user_and_text(message, args) if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_ban_perm")) return "" if not user_id: return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text(tld(chat.id, "common_err_no_user")) return "" else: raise if is_user_in_chat(chat, user_id): message.reply_text(tld(chat.id, "bans_unban_user_in_chat")) return "" chat.unban_member(user_id) message.reply_text(tld(chat.id, "bans_unban_success")) log = tld(chat.id, "bans_unban_logger").format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) return log
def ban(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] user_id, reason = extract_user_and_text(message, args) if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_ban_perm")) return "" if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found.": message.reply_text(tld(chat.id, "bans_err_usr_not_found")) return "" else: raise if user_id == bot.id: message.reply_text(tld(chat.id, "bans_err_usr_is_bot")) return "" if is_user_ban_protected(chat, user_id, member): message.reply_text(tld(chat.id, "bans_err_usr_is_admin")) return "" log = tld(chat.id, "bans_logger").format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) reply = tld(chat.id, "bans_banned_success").format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title)) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) reply += tld(chat.id, "bans_logger_reason").format(reason) try: chat.kick_member(user_id) message.reply_text(reply, parse_mode=ParseMode.HTML) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text(reply, quote=False, parse_mode=ParseMode.HTML) return log 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( tld(chat.id, "bans_err_unknown").format("banning")) return ""
def media(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return "" else: chatD = chat if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_mute_perm")) return "" user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "unrestrict_invalid")) return "" member = chatD.get_member(int(user_id)) if member.status != 'restricted': message.reply_text( tld(chat.id, "unrestrict_not_restricted").format(chatD.title)) return "" if member.status != 'kicked' and member.status != 'left': if member.can_send_messages and member.can_send_media_messages \ and member.can_send_other_messages and member.can_add_web_page_previews: message.reply_text( tld(chat.id, "unrestrict_not_restricted").format(chatD.title)) else: bot.restrict_chat_member(chatD.id, int(user_id), can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True) keyboard = [] reply = tld(chat.id, "unrestrict_success").format( mention_html(member.user.id, member.user.first_name), chatD.title) message.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#UNRESTRICTED" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chatD.title), mention_html( user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) else: message.reply_text(tld(chat.id, "unrestrict_not_in_chat")) return ""
@bot_admin @user_admin @loggable def mute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message if conn := connected(bot, update, chat, user.id): chatD = dispatcher.bot.getChat(conn) elif chat.type == "private": return "" else: chatD = chat if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_mute_perm")) return "" user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "mute_invalid")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "mute_not_myself")) return "" if member := chatD.get_member(int(user_id)): if user_id in SUDO_USERS: message.reply_text(tld(chat.id, "mute_not_sudo"))
def temp_nomedia(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return "" else: chatD = chat if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_mute_perm")) return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text(tld(chat.id, "mute_not_refer")) return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text(tld(chat.id, "mute_not_existed")) return "" else: raise if is_user_admin(chat, user_id, member): message.reply_text(tld(chat.id, "restrict_is_admin")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "restrict_is_bot")) return "" if not reason: message.reply_text(tld(chat.id, "nomedia_need_time")) return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" mutetime = extract_time(message, time_val) if not mutetime: return "" log = "<b>{}:</b>" \ "\n#TEMP RESTRICTED" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>" \ "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id, time_val) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) try: if member.can_send_messages is None or member.can_send_messages: bot.restrict_chat_member(chat.id, user_id, until_date=mutetime, can_send_messages=True, can_send_media_messages=False, can_send_other_messages=False, can_add_web_page_previews=False) message.reply_text( tld(chat.id, "nomedia_success").format(time_val, chatD.title)) return log else: message.reply_text( tld(chat.id, "restrict_already_restricted").format(chatD.title)) except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text(tld(chat.id, "nomedia_success").format( time_val, chatD.title), quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR muting user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) message.reply_text(tld(chat.id, "restrict_cant_restricted")) return ""
def mute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return "" else: chatD = chat if user_can_ban(chat, user, bot.id) is False: message.reply_text(tld(chat.id, "admin_no_mute_perm")) return "" user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "mute_invalid")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "mute_not_myself")) return "" member = chatD.get_member(int(user_id)) if member: if user_id in SUDO_USERS: message.reply_text(tld(chat.id, "mute_not_sudo")) elif is_user_admin(chatD, user_id, member=member): message.reply_text(tld(chat.id, "mute_not_m_admin")) elif member.can_send_messages is None or member.can_send_messages: bot.restrict_chat_member(chatD.id, user_id, can_send_messages=False) keyboard = [] reply = tld(chat.id, "mute_success").format( mention_html(member.user.id, member.user.first_name), chatD.title) message.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#MUTE" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}".format(html.escape(chatD.title), mention_html( user.id, user.first_name), mention_html(member.user.id, member.user.first_name)) else: message.reply_text( tld(chat.id, "mute_already_mute").format(chatD.title)) else: message.reply_text( tld(chat.id, "mute_not_in_chat").format(chatD.title)) return ""