def security_mute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] getcur, cur_value, cust_text = sql.welcome_security(chat.id) if len(args) >= 1: var = args[0] if var[:1] == "0": mutetime = "0" sql.set_welcome_security(chat.id, getcur, "0", cust_text) text = tld(chat.id, 'welcome_mute_time_none') else: mutetime = extract_time(message, var) if mutetime == "": return sql.set_welcome_security(chat.id, getcur, str(var), cust_text) text = tld(chat.id, 'welcome_mute_time').format(var) update.effective_message.reply_text(text) else: if str(cur_value) == "0": update.effective_message.reply_text( tld(chat.id, 'welcome_mute_time_settings_none')) else: update.effective_message.reply_text( tld(chat.id, 'welcome_mute_time_settings').format(cur_value))
def temp_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 is_user_ban_protected(chat, user_id, member): message.reply_text(tld(chat.id, "bans_err_usr_is_admin")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "bans_err_usr_is_bot")) return "" if not reason: message.reply_text(tld(chat.id, "bans_err_tban_no_arg")) return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" bantime = extract_time(message, time_val) if not bantime: return "" log = tld(chat.id, "bans_tban_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, time_val) if reason: log += tld(chat.id, "bans_logger_reason").format(reason) try: chat.kick_member(user_id, until_date=bantime) reply = tld(chat.id, "bans_tbanned_success").format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title), time_val) reply += tld(chat.id, "bans_logger_reason").format(reason) 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(tld(chat.id, "bans_tbanned_success").format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title), time_val), quote=False) 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("tbanning")) return ""
def temp_mute(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, reason) return "" if not user_id: send_message(update.effective_message, "You don't seem to be referring to a user.") return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": update.effective_send_message( update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": send_message(update.effective_message, "I can't find this user") return "" else: raise if is_user_admin(chat, user_id, member): send_message(update.effective_message, "I really wish I could mute admins...") return "" if user_id == context.bot.id: send_message(update.effective_message, "I'm not gonna MUTE myself, are you crazy?") return "" check = context.bot.getChatMember(chat.id, user.id) if check['can_restrict_members'] == False: send_message(update.effective_message, "You have no right to restrict someone.") return "" if not reason: send_message(update.effective_message, "You haven't specified a time to mute this user for!") 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#TMUTE" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\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), time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if member.can_send_messages is None or member.can_send_messages: context.bot.restrict_chat_member( chat.id, user_id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False)) if conn: text = "Muted for *{}* in *{}*!".format(time_val, chat_name) else: text = "Admin {} Temporary Muted user {} for {}\n<b>Reason:</b> {}".format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, reason or "No reason given") send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return log else: send_message(update.effective_message, "This user is already muted.") except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply send_message( update.effective_message, "Admin {} Temporary Muted user {} for {}\n<b>Reason:</b> {}". format(mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, reason or "No reason given"), parse_mode=ParseMode.HTML) 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) send_message(update.effective_message, "Well damn, I can't mute that user.") return ""
def new_member(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] should_welc, cust_welcome, cust_content, welc_type = sql.get_welc_pref( chat.id) cust_welcome = markdown_to_html(cust_welcome) if should_welc: sent = None new_members = update.effective_message.new_chat_members for new_mem in new_members: # Give start information when add bot to group if sw != None: sw_ban = sw.get_ban(new_mem.id) if sw_ban: return if new_mem.id == bot.id: bot.send_message( MESSAGE_DUMP, "I have been added to {} with ID: <pre>{}</pre>".format( chat.title, chat.id), parse_mode=ParseMode.HTML) bot.send_message(chat.id, tld(chat.id, 'welcome_added_to_grp')) else: # If welcome message is media, send with appropriate function if welc_type != sql.Types.TEXT and welc_type != sql.Types.BUTTON_TEXT: reply = update.message.message_id cleanserv = sql.clean_service(chat.id) # Clean service welcome if cleanserv: try: dispatcher.bot.delete_message( chat.id, update.message.message_id) except BadRequest: pass reply = False # Formatting text first_name = new_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if new_mem.last_name: fullname = "{} {}".format(first_name, new_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(new_mem.id, first_name) if new_mem.username: username = "******" + escape(new_mem.username) else: username = mention formatted_text = cust_welcome.format( first=escape(first_name), last=escape(new_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=new_mem.id) # Build keyboard buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) getsec, mutetime, custom_text = sql.welcome_security( chat.id) member = chat.get_member(new_mem.id) # If user ban protected don't apply security on him if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)): pass elif getsec: # If mute time is turned on if mutetime: if mutetime[:1] == "0": if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False else: mutetime = extract_time( update.effective_message, mutetime) if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, until_date=mutetime, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False # If security welcome is turned on if canrest: sql.add_to_userlist(chat.id, new_mem.id) keyb.append([ InlineKeyboardButton( text=str(custom_text), callback_data="check_bot_({})".format( new_mem.id)) ]) keyboard = InlineKeyboardMarkup(keyb) # Send message ENUM_FUNC_MAP[welc_type](chat.id, cust_content, caption=formatted_text, reply_markup=keyboard, parse_mode="html", reply_to_message_id=reply) return # else, move on first_name = new_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if cust_welcome: if new_mem.last_name: fullname = "{} {}".format(first_name, new_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(new_mem.id, first_name) if new_mem.username: username = "******" + escape(new_mem.username) else: username = mention valid_format = escape_invalid_curly_brackets( cust_welcome, VALID_WELCOME_FORMATTERS) res = valid_format.format(first=escape(first_name), last=escape(new_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=new_mem.id) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) else: res = sql.DEFAULT_WELCOME.format(first=first_name) keyb = [] getsec, mutetime, custom_text = sql.welcome_security(chat.id) member = chat.get_member(new_mem.id) # If user ban protected don't apply security on him if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)): pass elif getsec: if mutetime: if mutetime[:1] == "0": if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False else: mutetime = extract_time(update.effective_message, mutetime) if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, until_date=mutetime, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False if canrest: sql.add_to_userlist(chat.id, new_mem.id) keyb.append([ InlineKeyboardButton( text=str(custom_text), callback_data="check_bot_({})".format( new_mem.id)) ]) keyboard = InlineKeyboardMarkup(keyb) sent = send(update, res, keyboard, sql.DEFAULT_WELCOME.format( first=first_name)) # type: Optional[Message] prev_welc = sql.get_clean_pref(chat.id) if prev_welc: try: bot.delete_message(chat.id, prev_welc) except BadRequest as excp: pass if sent: sql.set_clean_welcome(chat.id, sent.message_id)
def del_blacklist(update, context): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] user = update.effective_user to_match = extract_text(message) if not to_match: return getmode, value = sql.get_blacklist_setting(chat.id) chat_filters = sql.get_chat_blacklist(chat.id) for trigger in chat_filters: pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])" if re.search(pattern, to_match, flags=re.IGNORECASE): try: if getmode == 0: return elif getmode == 1: message.delete() elif getmode == 2: message.delete() warn(update.effective_user, chat, "Say '{}' which in blacklist words".format(trigger), message, update.effective_user, conn=False) return elif getmode == 3: message.delete() bot.restrict_chat_member(chat.id, update.effective_user.id, can_send_messages=False) bot.sendMessage( chat.id, "{} muted because say '{}' which in blacklist words". format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown") return elif getmode == 4: message.delete() res = chat.unban_member(update.effective_user.id) if res: bot.sendMessage( chat.id, "{} kicked because say '{}' which in blacklist words" .format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown") return elif getmode == 5: message.delete() chat.kick_member(user.id) bot.sendMessage( chat.id, "{} banned because say '{}' which in blacklist words". format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown") return elif getmode == 6: message.delete() bantime = extract_time(message, value) chat.kick_member(user.id, until_date=bantime) bot.sendMessage( chat.id, "{} banned for {} because say '{}' which in blacklist words" .format(mention_markdown(user.id, user.first_name), value, trigger), parse_mode="markdown") return elif getmode == 7: message.delete() mutetime = extract_time(message, value) bot.restrict_chat_member(chat.id, user.id, until_date=mutetime, can_send_messages=False) bot.sendMessage( chat.id, "{} muted for {} because say '{}' which in blacklist words" .format(mention_markdown(user.id, user.first_name), value, trigger), parse_mode="markdown") return except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("Error while deleting blacklist message.") break
def check_flood(bot: Bot, update: Update) -> str: user = update.effective_user chat = update.effective_chat msg = update.effective_message if not user: # ignore channels return "" # ignore admins if is_user_admin(chat, user.id): sql.update_flood(chat.id, None) return "" should_ban = sql.update_flood(chat.id, user.id) if not should_ban: return "" try: getmode, getvalue = sql.get_flood_setting(chat.id) if getmode == 1: chat.kick_member(user.id) execstrings = "Banned" tag = "BANNED" elif getmode == 2: chat.kick_member(user.id) chat.unban_member(user.id) execstrings = "Kicked" tag = "KICKED" elif getmode == 3: bot.restrict_chat_member( chat.id, user.id, can_send_messages=False ) execstrings = "Muted" tag = "MUTED" elif getmode == 4: bantime = extract_time(msg, getvalue) chat.kick_member(user.id, until_date=bantime) execstrings = "Banned for {}".format(getvalue) tag = "TBAN" elif getmode == 5: mutetime = extract_time(msg, getvalue) bot.restrict_chat_member( chat.id, user.id, until_date=mutetime, can_send_messages=False ) execstrings = "Muted for {}".format(getvalue) tag = "TMUTE" send_message( update.effective_message, tld( chat.id, "flood_mute").format(execstrings) ) return tld(chat.id, "flood_logger_success").format( tag, html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), ) except BadRequest: msg.reply_text(tld(chat.id, "flood_err_no_perm")) sql.set_flood(chat.id, 0) return tld(chat.id, "flood_logger_fail").format(chat.title)
def blacklist_mode(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if args[0].lower() == 'off' or args[0].lower( ) == 'nothing' or args[0].lower() == 'no': settypeblacklist = 'do nothing' sql.set_blacklist_strength(chat_id, 0, "0") elif args[0].lower() == 'del' or args[0].lower() == 'delete': settypeblacklist = 'delete that msg' sql.set_blacklist_strength(chat_id, 1, "0") elif args[0].lower() == 'warn': settypeblacklist = 'warn sender' sql.set_blacklist_strength(chat_id, 2, "0") elif args[0].lower() == 'mute': settypeblacklist = 'mute sender' sql.set_blacklist_strength(chat_id, 3, "0") elif args[0].lower() == 'kick': settypeblacklist = 'kick sender' sql.set_blacklist_strength(chat_id, 4, "0") elif args[0].lower() == 'ban': settypeblacklist = 'banned sender' sql.set_blacklist_strength(chat_id, 5, "0") elif args[0].lower() == 'tban': if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blacklistmode tban <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = 'temporary banned sender for {}'.format(args[1]) sql.set_blacklist_strength(chat_id, 6, str(args[1])) elif args[0].lower() == 'tmute': if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blacklistmode tmute <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = 'temporary muted sender for {}'.format(args[1]) sql.set_blacklist_strength(chat_id, 7, str(args[1])) else: send_message( update.effective_message, "I only understand off/del/warn/ban/kick/mute/tban/tmute!") return "" if conn: text = "The blacklist mode has been changed, the User will be `{}` on *{}*!".format( settypeblacklist, chat_name) else: text = "Blacklist mode changed, will `{}`".format(settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") return "<b>{}:</b>\n" \ "<b>Admin:</b> {}\n" \ "Changed the blacklist mode. will {}.".format(html.escape(chat.title), mention_html(user.id, user.first_name), settypeblacklist) else: getmode, getvalue = sql.get_blacklist_setting(chat.id) if getmode == 0: settypeblacklist = "disabled" elif getmode == 1: settypeblacklist = "delete" elif getmode == 2: settypeblacklist = "warn" elif getmode == 3: settypeblacklist = "mute" elif getmode == 4: settypeblacklist = "kick" elif getmode == 5: settypeblacklist = "ban" elif getmode == 6: settypeblacklist = "temp ban for {}".format(getvalue) elif getmode == 7: settypeblacklist = "temp mute for {}".format(getvalue) if conn: text = "Current blacklist mode is set to *{}* in *{}*.".format( settypeblacklist, chat_name) else: text = "Current blacklist mode is set to *{}*.".format( settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) return ""
def check_flood(update, context) -> str: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] if not user: # ignore channels return "" # ignore admins if is_user_admin(chat, user.id): sql.update_flood(chat.id, None) return "" should_ban = sql.update_flood(chat.id, user.id) if not should_ban: return "" try: getmode, getvalue = sql.get_flood_setting(chat.id) if getmode == 1: chat.kick_member(user.id) execstrings = "Get out!" tag = "BANNED" elif getmode == 2: chat.kick_member(user.id) chat.unban_member(user.id) execstrings = "Get out!" tag = "KICKED" elif getmode == 3: context.bot.restrict_chat_member( chat.id, user.id, permissions=ChatPermissions(can_send_messages=False)) execstrings = "Now you shutup!" tag = "MUTED" elif getmode == 4: bantime = extract_time(msg, getvalue) chat.kick_member(user.id, until_date=bantime) execstrings = "Get out for {}!".format(getvalue) tag = "TBAN" elif getmode == 5: mutetime = extract_time(msg, getvalue) context.bot.restrict_chat_member( chat.id, user.id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False)) execstrings = "Now you shutup for {}!".format(getvalue) tag = "TMUTE" send_message( update.effective_message, "I like to leave the flooding to natural disasters. But you, you were just a disappointment. {}" .format(execstrings)) return "<b>{}:</b>" \ "\n#{}" \ "\n<b>User:</b> {}" \ "\nFlooded the group.".format(tag, html.escape(chat.title), mention_html(user.id, user.first_name)) except BadRequest: send_message( update.effective_message, "I can't kick people here, give me permissions first! Until then, I'll disable antiflood." ) sql.set_flood(chat.id, 0) return "<b>{}:</b>" \ "\n#INFO" \ "\n{}".format(chat.title, "Don't have kick permissions, so automatically disabled antiflood.")
def del_blackliststicker(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] user = update.effective_user to_match = message.sticker if not to_match or not to_match.set_name: return getmode, value = sql.get_blacklist_setting(chat.id) chat_filters = sql.get_chat_stickers(chat.id) for trigger in chat_filters: if to_match.set_name.lower() == trigger.lower(): try: if getmode == 0: return elif getmode == 1: message.delete() elif getmode == 2: message.delete() warn( update.effective_user, chat, "Using sticker '{}' which in blacklist stickers".format( trigger ), message, update.effective_user, # conn=False, ) return elif getmode == 3: message.delete() bot.restrict_chat_member( chat.id, update.effective_user.id, ) bot.sendMessage( chat.id, "{} muted because using '{}' which in blacklist stickers".format( mention_markdown(user.id, user.first_name), trigger ), parse_mode="markdown", ) return elif getmode == 4: message.delete() res = chat.unban_member(update.effective_user.id) if res: bot.sendMessage( chat.id, "{} kicked because using '{}' which in blacklist stickers".format( mention_markdown(user.id, user.first_name), trigger ), parse_mode="markdown", ) return elif getmode == 5: message.delete() chat.kick_member(user.id) bot.sendMessage( chat.id, "{} banned because using '{}' which in blacklist stickers".format( mention_markdown(user.id, user.first_name), trigger ), parse_mode="markdown", ) return elif getmode == 6: message.delete() bantime = extract_time(message, value) chat.kick_member(user.id, until_date=bantime) bot.sendMessage( chat.id, "{} banned for {} because using '{}' which in blacklist stickers".format( mention_markdown(user.id, user.first_name), value, trigger ), parse_mode="markdown", ) return elif getmode == 7: message.delete() mutetime = extract_time(message, value) bot.restrict_chat_member( chat.id, user.id, until_date=mutetime, ) bot.sendMessage( chat.id, "{} muted for {} because using '{}' which in blacklist stickers".format( mention_markdown(user.id, user.first_name), value, trigger ), parse_mode="markdown", ) return except BadRequest as excp: if excp.message != "Message to delete not found": LOGGER.exception("Error while deleting blacklist message.") break
def temp_ban(update, context): currentchat = update.effective_chat # type: Optional[Chat] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, reason) return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title check = context.bot.getChatMember(chat_id, context.bot.id) if check.status == 'member': if conn: text = "I can't restrict people in {}! Make sure I'm admin and can appoint new admins.".format(chat_name) else: text = "I can't restrict people here! Make sure I'm admin and can appoint new admins." send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return "" else: if check['can_restrict_members'] == False: if conn: text = "I can't restrict people in {}! Make sure I'm admin and can appoint new admins.".format(chat_name) else: text = "I can't restrict people here! Make sure I'm admin and can appoint new admins." send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return "" if not user_id: send_message(update.effective_message, "You don't seem to be referring to a user.") return "" try: if conn: member = context.bot.getChatMember(chat_id, user_id) else: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": send_message(update.effective_message, "I can't find this user") return "" else: raise if user_id == context.bot.id: send_message(update.effective_message, "I'm not gonna BAN myself, are you crazy?") return "" if is_user_ban_protected(chat, user_id, member): send_message(update.effective_message, "I really wish I could ban admins...") return "" if member['can_restrict_members'] == False: send_message(update.effective_message, "You have no right to restrict someone.") return "" if not reason: send_message(update.effective_message, "You haven't specified a time to ban this user for!") return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" bantime = extract_time(message, time_val) if not bantime: return "" log = "<b>{}:</b>" \ "\n#TEMPBAN" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</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), member.user.id, time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if conn: context.bot.kickChatMember(chat_id, user_id, until_date=bantime) context.bot.send_sticker(currentchat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, "Admin {} Temporary Banned user {} for {} at {}\n<b>Reason:</b> {}".format(mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, chat_name, reason or "No reason given"), parse_mode=ParseMode.HTML) else: chat.kick_member(user_id, until_date=bantime) context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, "Admin {} Temporary Banned user {} for {}\n<b>Reason:</b> {}".format(mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, reason or "No reason given"), parse_mode=ParseMode.HTML) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply send_message(update.effective_message,"Admin {} Temporary Banned user {} for {}\n<b>Reason:</b> {}".format(mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, reason), 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) send_message(update.effective_message, "Well damn, I can't kick that user") 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 ""