def clear(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = tld(chat.id, "note_is_local") else: chat_name = chat.title if len(args) >= 1: notename = args[0].lower() if sql.rm_note(chat_id, notename): update.effective_message.reply_text( tld(chat.id, "clear_success").format(chat_name), parse_mode=ParseMode.MARKDOWN, ) else: update.effective_message.reply_text( tld(chat.id, "note_not_existed"))
def remove_all_notes(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user message = update.effective_message if chat.type == "private": chat.title = tld(chat.id, "note_is_local") else: owner = chat.get_member(user.id) chat.title = chat.title if owner.status != "creator": message.reply_text(tld(chat.id, "notes_must_be_creator")) return note_list = sql.get_all_chat_notes(chat.id) if not note_list: message.reply_text( tld(chat.id, "note_none_in_chat").format(chat.title), parse_mode=ParseMode.MARKDOWN, ) return x = 0 a_note = [] for notename in note_list: x += 1 note = notename.name.lower() a_note.append(note) for note in a_note: sql.rm_note(chat.id, note) message.reply_text(tld(chat.id, "notes_cleanup_success").format(x))
def extract_time(message, time_val): if any(time_val.endswith(unit) for unit in ("m", "h", "d")): chat = message.chat unit = time_val[-1] time_num = time_val[:-1] # type: str if not time_num.isdigit(): message.reply_text( tld(chat.id, "helpers_string_handling_invalid_time_amount")) return "" if unit == "m": bantime = int(time.time() + int(time_num) * 60) elif unit == "h": bantime = int(time.time() + int(time_num) * 60 * 60) elif unit == "d": bantime = int(time.time() + int(time_num) * 24 * 60 * 60) else: # how even...? return "" return bantime else: message.reply_text( tld(chat.id, "helpers_string_handling_invalid_time_type").format( time_val[-1])) return ""
def warns(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat user_id = extract_user(message, args) or update.effective_user.id result = sql.get_warns(user_id, chat.id) num = 1 if result and result[0] != 0: num_warns, reasons = result limit, soft_warn = sql.get_warn_setting(chat.id) if reasons: text = tld(chat.id, "warns_list_warns").format(num_warns, limit) for reason in reasons: text += "\n {}. {}".format(num, reason) num += 1 msgs = split_message(text) for msg in msgs: update.effective_message.reply_text(msg) else: update.effective_message.reply_text( tld(chat.id, "warns_list_warns_no_reason").format(num_warns, limit)) else: update.effective_message.reply_text( tld(chat.id, "warns_list_warns_none"))
def promote(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message user = update.effective_user chat = update.effective_chat conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: chatD = update.effective_chat if chat.type == "private": return if not chatD.get_member(bot.id).can_promote_members: update.effective_message.reply_text(tld(chat.id, "admin_err_no_perm")) return user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return user_member = chatD.get_member(user_id) if user_member.status == "administrator" or user_member.status == "creator": message.reply_text(tld(chat.id, "admin_err_user_admin")) return if user_id == bot.id: message.reply_text(tld(chat.id, "admin_err_self_promote")) return # set same perms as bot - bot can't assign higher perms than itself! bot_member = chatD.get_member(bot.id) bot.promoteChatMember( chatD.id, user_id, can_change_info=bot_member.can_change_info, can_post_messages=bot_member.can_post_messages, can_edit_messages=bot_member.can_edit_messages, can_delete_messages=bot_member.can_delete_messages, can_invite_users=bot_member.can_invite_users, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, can_promote_members=bot_member.can_promote_members, ) message.reply_text( tld(chat.id, "admin_promote_success").format( mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), html.escape(chatD.title), ), parse_mode=ParseMode.HTML, ) return ( f"<b>{html.escape(chatD.title)}:</b>" "\n#PROMOTED" f"\n<b>Admin:</b> {mention_html(user.id, user.first_name)}" f"\n<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" )
def connected(bot, update, chat, user_id, need_admin=True): chat = update.effective_chat # type: Optional[Chat] if chat.type == chat.PRIVATE and sql.get_connected_chat(user_id): conn_id = sql.get_connected_chat(user_id).chat_id if (bot.get_chat_member( conn_id, user_id).status in ("administrator", "creator") or (sql.allow_connect_to_chat(connect_chat) == True) and bot.get_chat_member( user_id, update.effective_message.from_user.id).status in ("member")) or (user_id in SUDO_USERS): if need_admin: if (bot.get_chat_member( conn_id, update.effective_message.from_user.id).status in ("administrator", "creator") or user_id in SUDO_USERS): return conn_id else: update.effective_message.reply_text( tld(chat.id, "connection_err_no_admin")) return else: return conn_id else: update.effective_message.reply_text( tld(chat.id, "connection_err_unknown")) disconnect_chat(bot, update) return else: return False
def disconnect_chat(bot, update): chat = update.effective_chat # type: Optional[Chat] if update.effective_chat.type == "private": disconnection_status = sql.disconnect( update.effective_message.from_user.id) if disconnection_status: sql.disconnected_chat = update.effective_message.reply_text( tld(chat.id, "connection_dis_success")) # Rebuild user's keyboard keyboard(bot, update) else: update.effective_message.reply_text( tld(chat.id, "connection_dis_fail")) elif update.effective_chat.type == "supergroup": disconnection_status = sql.disconnect( update.effective_message.from_user.id) if disconnection_status: sql.disconnected_chat = update.effective_message.reply_text( tld(chat.id, "connection_dis_success")) # Rebuild user's keyboard keyboard(bot, update) else: update.effective_message.reply_text( tld(chat.id, "connection_dis_fail")) else: update.effective_message.reply_text(tld(chat.id, "common_cmd_pm_only"))
def security(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] getcur, cur_value, cust_text = sql.welcome_security(chat.id) if len(args) >= 1: var = args[0].lower() if var == "yes" or var == "y" or var == "on": check = bot.getChatMember(chat.id, bot.id) if check.status == "member" or check[ "can_restrict_members"] == False: text = tld(chat.id, "welcome_mute_bot_cant_mute") update.effective_message.reply_text(text, parse_mode="markdown") return "" sql.set_welcome_security(chat.id, True, str(cur_value), cust_text) update.effective_message.reply_text( tld(chat.id, "welcome_mute_enabled")) elif var == "no" or var == "n" or var == "off": sql.set_welcome_security(chat.id, False, str(cur_value), cust_text) update.effective_message.reply_text( tld(chat.id, "welcome_mute_disabled")) else: update.effective_message.reply_text(tld(chat.id, "common_invalid_arg"), parse_mode=ParseMode.MARKDOWN) else: getcur, cur_value, cust_text = sql.welcome_security(chat.id) if getcur: getcur = "True" else: getcur = "False" if cur_value[:1] == "0": cur_value = "None" text = tld(chat.id, "welcome_mute_curr_settings").format( getcur, cur_value, cust_text) update.effective_message.reply_text(text, parse_mode="markdown")
def check_and_ban(update, user_id, should_message=True): chat = update.effective_chat message = update.effective_message try: if sw != None: sw_ban = sw.get_ban(user_id) if sw_ban: spamwatch_reason = sw_ban.reason chat.kick_member(user_id) if should_message: message.reply_text( tld(chat.id, "antispam_spamwatch_banned").format( spamwatch_reason), parse_mode=ParseMode.HTML, ) return else: return except Exception: pass if sql.is_user_gbanned(user_id): chat.kick_member(user_id) if should_message: userr = sql.get_gbanned_user(user_id) usrreason = userr.reason if not usrreason: usrreason = tld(chat.id, "antispam_no_reason") message.reply_text( tld(chat.id, "antispam_checkban_user_removed").format(usrreason), parse_mode=ParseMode.MARKDOWN, ) return
def set_warn_limit(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user msg = update.effective_message if args: if args[0].isdigit(): if int(args[0]) < 1: msg.reply_text(tld(chat.id, "warns_warnlimit_min_1")) else: sql.set_warn_limit(chat.id, int(args[0])) msg.reply_text( tld(chat.id, "warns_warnlimit_updated_success").format(args[0])) return tld(chat.id, "warns_set_warn_limit_log_channel").format( html.escape(chat.title), mention_html(user.id, user.first_name), args[0], ) else: msg.reply_text(tld(chat.id, "warns_warnlimit_invalid_arg")) else: limit, soft_warn = sql.get_warn_setting(chat.id) msg.reply_text(tld(chat.id, "warns_warnlimit_current").format(limit)) return ""
def stop_all_filters(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user message = update.effective_message if chat.type == "private": chat.title = tld(chat.id, "cust_filters_local") else: owner = chat.get_member(user.id) chat.title = chat.title if owner.status != "creator": message.reply_text(tld(chat.id, "notes_must_be_creator")) return x = 0 flist = sql.get_chat_triggers(chat.id) if not flist: message.reply_text( tld(chat.id, "cust_filters_list_empty").format(chat.title)) return f_flist = [] for f in flist: x += 1 f_flist.append(f) for fx in f_flist: sql.remove_filter(chat.id, fx) message.reply_text(tld(chat.id, "cust_filters_cleanup_success").format(x))
def unlock(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message if is_user_admin(chat, message.from_user.id): if len(args) >= 1: if args[0] in LOCK_TYPES: sql.update_lock(chat.id, args[0], locked=False) message.reply_text( tld(chat.id, "locks_unlock_success").format(args[0]), parse_mode=ParseMode.MARKDOWN, ) return ("<b>{}:</b>" "\n#UNLOCK" "\n<b>Admin:</b> {}" "\nUnlocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), args[0], )) elif args[0] in RESTRICTION_TYPES: sql.update_restriction(chat.id, args[0], locked=False) # members = users_sql.get_chat_members(chat.id) # if args[0] == "messages": # unrestr_members(bot, chat.id, members, media=False, other=False, previews=False) # elif args[0] == "media": # unrestr_members(bot, chat.id, members, other=False, previews=False) # elif args[0] == "other": # unrestr_members(bot, chat.id, members, previews=False) # elif args[0] == "previews": # unrestr_members(bot, chat.id, members) # elif args[0] == "all": # unrestr_members(bot, chat.id, members, True, True, True, True) message.reply_text( tld(chat.id, "locks_unlock_success").format(args[0]), parse_mode=ParseMode.MARKDOWN, ) return ("<b>{}:</b>" "\n#UNLOCK" "\n<b>Admin:</b> {}" "\nUnlocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), args[0], )) else: message.reply_text(tld(chat.id, "locks_type_invalid")) else: bot.sendMessage(chat.id, tld(chat.id, "locks_unlock_no_type")) return ""
def set_welcome(bot: Bot, update: Update) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] # If user is not set text and not reply a message if not msg.reply_to_message: if len(msg.text.split()) == 1: msg.reply_text(tld(chat.id, "welcome_set_welcome_no_text"), parse_mode="markdown") return "" text, data_type, content, buttons = get_welcome_type(msg) if data_type is None: msg.reply_text(tld(chat.id, "welcome_set_welcome_no_datatype")) return "" sql.set_custom_welcome(chat.id, content, text, data_type, buttons) msg.reply_text(tld(chat.id, "welcome_set_welcome_success")) return ("<b>{}:</b>" "\n#SET_WELCOME" "\n<b>Admin:</b> {}" "\nSet the welcome message.".format( escape(chat.title), mention_html(user.id, user.first_name)))
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: bot.restrict_chat_member(chat.id, user.id, can_send_messages=False) msg.reply_text(tld(chat.id, "flood_mute")) return tld(chat.id, "flood_logger_success").format( html.escape(chat.title), mention_html(user.id, 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 del_lockables(bot: Bot, update: Update): chat = update.effective_chat message = update.effective_message for lockable, filter in LOCK_TYPES.items(): if (filter(message) and sql.is_locked(chat.id, lockable) and can_delete(chat, bot.id)): if lockable == "bots": new_members = update.effective_message.new_chat_members for new_mem in new_members: if new_mem.is_bot: if not is_bot_admin(chat, bot.id): message.reply_text( tld(chat.id, "locks_lock_bots_no_admin")) return chat.kick_member(new_mem.id) message.reply_text(tld(chat.id, "locks_lock_bots_kick")) else: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break
def remove_warn_filter(bot: Bot, update: Update): chat = update.effective_chat msg = update.effective_message args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text if len(args) < 2: return extracted = split_quotes(args[1]) if len(extracted) < 1: return to_remove = extracted[0] chat_filters = sql.get_chat_warn_triggers(chat.id) if not chat_filters: msg.reply_text(tld(chat.id, "warns_filters_list_empty")) return for filt in chat_filters: if filt == to_remove: sql.remove_warn_filter(chat.id, to_remove) msg.reply_text(tld(chat.id, "warns_filters_remove_success")) raise DispatcherHandlerStop msg.reply_text(tld(chat.id, "warns_filter_doesnt_exist"))
def paste(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] BURL = "https://del.dog" message = update.effective_message if message.reply_to_message: data = message.reply_to_message.text elif len(args) >= 1: data = message.text.split(None, 1)[1] else: message.reply_text(tld(chat.id, "misc_paste_invalid")) return r = requests.post(f"{BURL}/documents", data=data.encode("utf-8")) if r.status_code == 404: update.effective_message.reply_text(tld(chat.id, "misc_paste_404")) r.raise_for_status() res = r.json() if r.status_code != 200: update.effective_message.reply_text(res["message"]) r.raise_for_status() key = res["key"] if res["isUrl"]: reply = tld(chat.id, "misc_paste_success").format(BURL, key, BURL, key) else: reply = f"{BURL}/{key}" update.effective_message.reply_text(reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
def invite(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chatP = dispatcher.bot.getChat(conn) else: chatP = update.effective_chat if chat.type == "private": return if chatP.username: update.effective_message.reply_text(chatP.username) elif chatP.type == chatP.SUPERGROUP or chatP.type == chatP.CHANNEL: bot_member = chatP.get_member(bot.id) if bot_member.can_invite_users: invitelink = chatP.invite_link # print(invitelink) if not invitelink: invitelink = bot.exportChatInviteLink(chatP.id) update.effective_message.reply_text(invitelink) else: update.effective_message.reply_text( tld(chat.id, "admin_err_no_perm_invitelink")) else: update.effective_message.reply_text( tld(chat.id, "admin_chat_no_invitelink"))
def rmwarn_handler(bot: Bot, update: Update) -> str: chat = update.effective_chat query = update.callback_query user = update.effective_user match = re.match(r"rm_warn\((.+?)\)", query.data) if match: user_id = match.group(1) if not is_user_admin(chat, int(user.id)): query.answer(text=tld(chat.id, "warns_remove_admin_only"), show_alert=True) return "" res = sql.remove_warn(user_id, chat.id) if res: update.effective_message.edit_text( tld(chat.id, "warns_remove_success").format( mention_html(user.id, user.first_name)), parse_mode=ParseMode.HTML, ) user_member = chat.get_member(user_id) return tld(chat.id, "warns_remove_log_channel").format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), user_member.user.id, ) else: update.effective_message.edit_text( tld(chat.id, "warns_user_has_no_warns").format( mention_html(user.id, user.first_name)), parse_mode=ParseMode.HTML, ) return ""
async def purge(event): if event.from_id == None: return chat = event.chat_id if not await user_is_admin(user_id=event.from_id, message=event): await event.reply(tld(chat, "helpers_user_not_admin")) return if not await can_delete_messages(message=event): await event.reply(tld(chat, "helpers_bot_cant_delete")) return msg = await event.get_reply_message() if not msg: await event.reply(tld(chat, "purge_invalid")) return msgs = [] msg_id = msg.id delete_to = event.message.id - 1 await event.client.delete_messages(chat, event.message.id) msgs.append(event.reply_to_msg_id) for m_id in range(delete_to, msg_id - 1, -1): msgs.append(m_id) if len(msgs) == 100: await event.client.delete_messages(chat, msgs) msgs = [] await event.client.delete_messages(chat, msgs) text = tld(chat, "purge_msg_success") await event.respond(text, parse_mode="md")
def get_id(bot: Bot, update: Update, args: List[str]): user_id = extract_user(update.effective_message, args) chat = update.effective_chat # type: Optional[Chat] if user_id: if (update.effective_message.reply_to_message and update.effective_message.reply_to_message.forward_from): user1 = update.effective_message.reply_to_message.from_user user2 = update.effective_message.reply_to_message.forward_from update.effective_message.reply_markdown( tld(chat.id, "misc_get_id_1").format( escape_markdown(user2.first_name), user2.id, escape_markdown(user1.first_name), user1.id, )) else: user = bot.get_chat(user_id) update.effective_message.reply_markdown( tld(chat.id, "misc_get_id_2").format(escape_markdown(user.first_name), user.id)) else: chat = update.effective_chat # type: Optional[Chat] if chat.type == "private": update.effective_message.reply_markdown( tld(chat.id, "misc_id_1").format(chat.id)) else: update.effective_message.reply_markdown( tld(chat.id, "misc_id_2").format(chat.id))
def flood(bot: Bot, update: Update): chat = update.effective_chat limit = sql.get_flood_limit(chat.id) if limit == 0: update.effective_message.reply_text(tld(chat.id, "flood_status_off")) else: update.effective_message.reply_text( tld(chat.id, "flood_status_on").format(limit))
def gdpr(bot: Bot, update: Update): update.effective_message.reply_text( tld(update.effective_chat.id, "misc_gdpr")) for mod in GDPR: mod.__gdpr__(update.effective_user.id) update.effective_message.reply_text(tld(update.effective_chat.id, "send_gdpr"), parse_mode=ParseMode.MARKDOWN)
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 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 != "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 ""
def build_curr_disabled(chat_id: Union[str, int]) -> str: disabled = sql.get_all_disabled(chat_id) if not disabled: return tld(chat_id, "disable_chatsettings_none_disabled") result = "" for cmd in disabled: result += " - `{}`\n".format(escape_markdown(cmd)) return tld(chat_id, "disable_chatsettings_list_disabled").format(result)
def security_text_reset(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] getcur, cur_value, cust_text = sql.welcome_security(chat.id) sql.set_welcome_security(chat.id, getcur, cur_value, tld(chat.id, "welcome_mute_btn_default_text")) update.effective_message.reply_text( tld(chat.id, "welcome_mute_btn_text_reset").format( tld(chat.id, "welcome_mute_btn_default_text")), parse_mode="markdown", )
def keyboard(bot, update): chat = update.effective_chat user = update.effective_user conn_id = con_sql.get_connected_chat(user.id) if conn_id and not conn_id == False: btn1 = "/disconnect - {}".format(tld(chat.id, "keyboard_disconnect")) btn2 = "" btn3 = "" else: if con_sql.get_history(user.id): history = con_sql.get_history(user.id) try: chat_name1 = dispatcher.bot.getChat(history.chat_id1).title except Exception: chat_name1 = "" try: chat_name2 = dispatcher.bot.getChat(history.chat_id2).title except Exception: chat_name2 = "" try: chat_name3 = dispatcher.bot.getChat(history.chat_id3).title except Exception: chat_name3 = "" if chat_name1: btn1 = "/connect {} - {}".format(history.chat_id1, chat_name1) else: btn1 = "/connect - {}".format(tld(chat.id, "keyboard_connect")) if chat_name2: btn2 = "/connect {} - {}".format(history.chat_id2, chat_name2) else: btn2 = "" if chat_name3: btn3 = "/connect {} - {}".format(history.chat_id3, chat_name3) else: btn3 = "" # TODO: Remove except garbage update.effective_message.reply_text( tld(chat.id, "keyboard_updated"), reply_markup=ReplyKeyboardMarkup([ [ KeyboardButton("/help"), KeyboardButton("/notes - {}".format( tld(chat.id, "keyboard_notes"))), ], [KeyboardButton(btn1)], [KeyboardButton(btn2)], [KeyboardButton(btn3)], ]), )
def stickerid(bot: Bot, update: Update): chat = update.effective_chat msg = update.effective_message if msg.reply_to_message and msg.reply_to_message.sticker: update.effective_message.reply_text( tld(chat.id, "stickers_stickerid").format( escape_markdown(msg.reply_to_message.sticker.file_id)), parse_mode=ParseMode.MARKDOWN, ) else: update.effective_message.reply_text( tld(chat.id, "stickers_stickerid_no_reply"))
def send_start(bot, update): chat = update.effective_chat # chat = update.effective_chat and unused variable text = tld(chat.id, "main_start_pm") keyboard = [[ InlineKeyboardButton( text=tld(chat.id, "main_start_btn_support"), url="https://t.me/exusiaisupport", ) ]] keyboard += [[ InlineKeyboardButton(text=tld(chat.id, "main_start_btn_lang"), callback_data="set_lang_"), InlineKeyboardButton(text=tld(chat.id, "btn_help"), callback_data="help_back"), ]] try: query = update.callback_query # query.message.delete() bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard), disable_web_page_preview=True, ) except Exception: pass if query: try: bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard), disable_web_page_preview=True, ) except Exception: return else: update.effective_message.reply_text( text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, )
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 ""