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 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] 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 warn_user(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message chat = update.effective_chat warner = update.effective_user user_id, reason = extract_user_and_text(message, args) if user_id: if (message.reply_to_message and message.reply_to_message.from_user.id == user_id): return warn( message.reply_to_message.from_user, chat, reason, message.reply_to_message, warner, ) else: return warn( chat.get_member(user_id).user, chat, reason, message, warner) else: message.reply_text(tld(chat.id, "common_err_no_user")) 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 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 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 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 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 gban(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat banner = update.effective_user user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return if int(user_id) in SUDO_USERS: message.reply_text(tld(chat.id, "antispam_err_usr_sudo")) return if int(user_id) in SUPPORT_USERS: message.reply_text(tld(chat.id, "antispam_err_usr_support")) return if user_id == bot.id: message.reply_text(tld(chat.id, "antispam_err_usr_bot")) return try: user_chat = bot.get_chat(user_id) except BadRequest as excp: message.reply_text(excp.message) return if user_chat.type != "private": message.reply_text(tld(chat.id, "antispam_err_not_usr")) return if user_chat.first_name == "": message.reply_text(tld(chat.id, "antispam_err_usr_deleted")) return if not reason: message.reply_text("Global Ban must have a reason!") return full_reason = html.escape( f"{reason} // GBanned by {banner.first_name} id {banner.id}") if sql.is_user_gbanned(user_id): old_reason = (sql.update_gban_reason( user_id, user_chat.username or user_chat.first_name, full_reason) or "None") try: bot.send_message( GBAN_DUMP, tld(chat.id, "antispam_logger_update_gban").format( mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name or "Deleted Account"), user_chat.id, old_reason, full_reason, ), parse_mode=ParseMode.HTML, ) except Exception: pass message.reply_text( tld(chat.id, "antispam_reason_updated").format(html.escape(old_reason), html.escape(full_reason)), parse_mode=ParseMode.HTML, ) return starting = tld(chat.id, "antispam_new_gban").format( mention_html(user_chat.id, user_chat.first_name or "Deleted Account"), user_chat.id, reason, ) message.reply_text(starting, parse_mode=ParseMode.HTML) try: bot.send_message( GBAN_DUMP, tld(chat.id, "antispam_logger_new_gban").format( mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name), user_chat.id, full_reason or tld(chat.id, "antispam_no_reason"), ), parse_mode=ParseMode.HTML, ) except Exception: print("nut") try: bot.kick_chat_member(chat.id, user_chat.id) except BadRequest as excp: if excp.message in GBAN_ERRORS: pass sql.gban_user(user_id, user_chat.username or user_chat.first_name, full_reason)
def ungban(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat user_id, reason = extract_user_and_text(message, args) reason = html.escape(reason) if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return user_chat = bot.get_chat(user_id) if user_chat.type != "private": message.reply_text(tld(chat.id, "antispam_err_not_usr")) return if not sql.is_user_gbanned(user_id): message.reply_text(tld(chat.id, "antispam_user_not_gbanned")) return if not reason: message.reply_text( "Removal of Global Ban requires a reason to do so, why not send me one?" ) return banner = update.effective_user message.reply_text( "<b>Initializing Global Ban Removal</b>\n<b>Sudo Admin:</b> {}\n<b>User:</b> {}\n<b>ID:</b> <code>{}</code>\n<b>Reason:</b> {}" .format( mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name), user_chat.id, reason, ), parse_mode=ParseMode.HTML, ) try: bot.send_message( GBAN_DUMP, tld(chat.id, "antispam_logger_ungban").format( mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name), user_chat.id, reason, ), parse_mode=ParseMode.HTML, ) except Exception: pass # chats = get_all_chats() # for chat in chats: # chat_id = chat.chat_id # # Check if this group has disabled gbans # if not sql.does_chat_gban(chat_id): # continue # try: # member = bot.get_chat_member(chat_id, user_id) # if member.status == 'kicked': # bot.unban_chat_member(chat_id, user_id) # except BadRequest as excp: # if excp.message in UNGBAN_ERRORS: # pass # else: # message.reply_text( # tld(chat.id, "antispam_err_ungban").format(excp.message)) # bot.send_message( # OWNER_ID, # tld(chat.id, "antispam_err_ungban").format(excp.message)) # return # except TelegramError: # pass sql.ungban_user(user_id) message.reply_text( "This user have been ungbanned succesfully, they might have to ask 'admins' of chats they were banned to unban manually due to global ban." "\n\nPlease forward this message to them or let them know about this.")
def fed_ban(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user fed_id = sql.get_fed_id(chat.id) if not fed_id: update.effective_message.reply_text( "This group is not a part of any federation!") return info = sql.get_fed_info(fed_id) OW = bot.get_chat(info["owner"]) HAHA = OW.id FEDADMIN = sql.all_fed_users(fed_id) FEDADMIN.append(int(HAHA)) if is_user_fed_admin(fed_id, user.id) == False: update.effective_message.reply_text( "Only federation admins can do this!") return message = update.effective_message user_id, reason = extract_user_and_text(message, args) fban, fbanreason = sql.get_fban_user(fed_id, user_id) if not user_id: message.reply_text("You don't seem to be referring to a user") return if user_id == bot.id: message.reply_text( "What is funnier than fbanning the bot? Self sacrifice.") return if is_user_fed_owner(fed_id, user_id) == True: message.reply_text("Why did you try the federation fban?") return if is_user_fed_admin(fed_id, user_id) == True: message.reply_text("He is a federation admin, I can't fban him.") return if user_id == OWNER_ID: message.reply_text( "I don't want to fban my master, that's a very stupid idea!") return if int(user_id) in SUDO_USERS: message.reply_text("I will not fban sudos!") return if int(user_id) in WHITELIST_USERS: message.reply_text( "This person is whitelisted, so they can't be fbanned!") return try: user_chat = bot.get_chat(user_id) except BadRequest as excp: message.reply_text(excp.message) return if user_chat.type != "private": message.reply_text("That's not a user!") return if fban: user_target = mention_html(user_chat.id, user_chat.first_name) fed_name = info["fname"] starting = "The reason of federation ban has been replaced for {} in the Federation <b>{}</b>.".format( user_target, fed_name) update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML) if reason == "": reason = "No reason given." temp = sql.un_fban_user(fed_id, user_id) if not temp: message.reply_text("Failed to update the reason for fban!") return x = sql.fban_user( fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason, ) if not x: message.reply_text( "Failed to ban from the federation! If this problem continues, ask in @exusiaisupport for help!" ) return fed_chats = sql.all_fed_chats(fed_id) for chat in fed_chats: try: bot.kick_chat_member(chat, user_id) except BadRequest as excp: if excp.message in FBAN_ERRORS: pass else: LOGGER.warning("Could not fban in {} because: {}".format( chat, excp.message)) except TelegramError: pass send_to_list( bot, FEDADMIN, "<b>FedBan reason updated</b>" "\n<b>Federation:</b> {}" "\n<b>Federation Admin:</b> {}" "\n<b>User:</b> {}" "\n<b>User ID:</b> <code>{}</code>" "\n<b>Reason:</b> {}".format( fed_name, mention_html(user.id, user.first_name), mention_html(user_chat.id, user_chat.first_name), user_chat.id, reason, ), html=True, ) message.reply_text("FedBan reason has been updated.") return user_target = mention_html(user_chat.id, user_chat.first_name) fed_name = info["fname"] starting = "Starting a federation ban for {} in the Federation <b>{}</b>.".format( user_target, fed_name) update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML) if reason == "": reason = "No reason given." x = sql.fban_user( fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason, ) if not x: message.reply_text( "Failed to ban from the federation! If this problem continues, ask in @exusiaisupport for help." ) return fed_chats = sql.all_fed_chats(fed_id) for chat in fed_chats: try: bot.kick_chat_member(chat, user_id) except BadRequest as excp: if excp.message in FBAN_ERRORS: try: dispatcher.bot.getChat(chat) except Unauthorized: sql.chat_leave_fed(chat) LOGGER.info( "Chat {} has leave fed {} because bot is kicked". format(chat, info["fname"])) continue else: LOGGER.warning("Cannot fban on {} because: {}".format( chat, excp.message)) except TelegramError: pass send_to_list( bot, FEDADMIN, "<b>New FedBan</b>" "\n<b>Federation:</b> {}" "\n<b>Federation Admin:</b> {}" "\n<b>User:</b> {}" "\n<b>User ID:</b> <code>{}</code>" "\n<b>Reason:</b> {}".format( fed_name, mention_html(user.id, user.first_name), mention_html(user_chat.id, user_chat.first_name), user_chat.id, reason, ), html=True, ) message.reply_text("This person has been fbanned")