def get_id(bot: Bot, update: Update, args: List[str]): user_id = extract_user(update.effective_message, args) 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_text( get_string("misc", "MSG_ID_WITH_FORWARD", lang.get_lang(update.effective_chat.id)).format( escape_markdown(user2.first_name), user2.id, escape_markdown(user1.first_name), user1.id), parse_mode=ParseMode.MARKDOWN) # MSG_ID_WITH_FORWARD else: user = bot.get_chat(user_id) update.effective_message.reply_text( get_string("misc", "MSG_ID_USER", lang.get_lang(update.effective_chat.id)).format( escape_markdown(user.first_name), user.id), parse_mode=ParseMode.MARKDOWN) # MSG_ID_USER else: chat = update.effective_chat # type: Optional[Chat] if chat.type == "private": update.effective_message.reply_text( get_string("misc", "MSG_YOUR_ID", lang.get_lang(update.effective_chat.id)).format( chat.id), parse_mode=ParseMode.MARKDOWN) # MSG_YOUR_ID else: update.effective_message.reply_text( get_string("misc", "MSG_GROUP_ID", lang.get_lang(update.effective_chat.id)).format( chat.id), parse_mode=ParseMode.MARKDOWN) # MSG_GROUP_ID
def blacklist(bot: Bot, update: Update, args: List[str]): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] all_blacklisted = sql.get_chat_blacklist(chat.id) filter_list = get_string("blacklist", "BASE_BLACKLIST_STRING", lang.get_lang(update.effective_chat.id)) if len(args) > 0 and args[0].lower() == 'copy': for trigger in all_blacklisted: filter_list += "<code>{}</code>\n".format(html.escape(trigger)) else: for trigger in all_blacklisted: filter_list += " - <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(filter_list) for text in split_text: if text == BASE_BLACKLIST_STRING: msg.reply_text( get_string("blacklist", "MSG_NO_BLACKLIST", lang.get_lang( update.effective_chat.id))) # MSG_NO_BLACKLIST return msg.reply_text(text, parse_mode=ParseMode.HTML)
def disable(update: Update, context: CallbackContext): args = context.args chat = update.effective_chat # type: Optional[Chat] if len(args) >= 1: disable_cmd = args[0] if disable_cmd.startswith(CMD_STARTERS): disable_cmd = disable_cmd[1:] if disable_cmd in set(DISABLE_CMDS + DISABLE_OTHER): sql.disable_command(chat.id, disable_cmd) update.effective_message.reply_text( get_string( "disable", "DISABLED_COMMAND", lang.get_lang( update.effective_chat.id)).format(disable_cmd), parse_mode=ParseMode.MARKDOWN) # DISABLED_COMMAND else: update.effective_message.reply_text( get_string( "disable", "ERR_INVALID_COMMAND", lang.get_lang( update.effective_chat.id))) # ERR_INVALID_COMMAND else: update.effective_message.reply_text( get_string("disable", "ERR_NO_COMMAND", lang.get_lang( update.effective_chat.id))) # ERR_NO_COMMAND
def add_blacklist(bot: Bot, update: Update): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] words = msg.text.split(None, 1) if len(words) > 1: text = words[1] to_blacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) for trigger in to_blacklist: sql.add_to_blacklist(chat.id, trigger.lower()) if len(to_blacklist) == 1: msg.reply_text( get_string("blacklist", "MSG_BLACKLIST_ADD_SUCCESS", lang.get_lang(update.effective_chat.id)).format( html.escape(to_blacklist[0])), parse_mode=ParseMode.HTML) # MSG_BLACKLIST_ADD_SUCCESS else: msg.reply_text(get_string( "blacklist", "MSG_BLACKLIST_ADD_SUCCESS_MULTIPLE", lang.get_lang(update.effective_chat.id)).format( len(to_blacklist)), parse_mode=ParseMode.HTML ) # MSG_BLACKLIST_ADD_SUCCESS_MULTIPLE else: msg.reply_text( get_string("blacklist", "ERR_BAD_REQUEST", lang.get_lang( update.effective_chat.id))) # ERR_BAD_REQUEST
def enable(update: Update, context: CallbackContext): args = context.args chat = update.effective_chat # type: Optional[Chat] if len(args) >= 1: enable_cmd = args[0] if enable_cmd.startswith(CMD_STARTERS): enable_cmd = enable_cmd[1:] if sql.enable_command(chat.id, enable_cmd): update.effective_message.reply_text( get_string( "disable", "ENABLED_COMMAND", lang.get_lang( update.effective_chat.id)).format(enable_cmd), parse_mode=ParseMode.MARKDOWN) # ENABLED_COMMAND else: update.effective_message.reply_text( get_string( "disable", "ERR_NOT_DISABLED", lang.get_lang( update.effective_chat.id))) # ERR_NOT_DISABLED else: update.effective_message.reply_text( get_string( "disable", "ERR_NO_COMMAND_TO_ENABLE", lang.get_lang( update.effective_chat.id))) # ERR_NO_COMMAND_TO_ENABLE
def nice(update: Update, context: CallbackContext): dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) dir = f"{dir}/misc" if not os.path.isdir(dir): os.mkdir(dir) dir = f"{dir}/nice" if not os.path.isdir(dir): os.mkdir(dir) user = update.effective_user if not os.path.isfile(f"{dir}/{user.id}.json"): update.effective_message.reply_text( get_random_string("nice", lang.get_lang(update.effective_chat.id))) else: # This is gonna check for a file for the sender, and if it exists, it is gonna decide randomly whether it should # be used or not. If it is used, it is gonna send with a line from that file instead of the normal ones. use = bool(random.choice([True, False])) print(use) if use: path = f"{dir}/{user.id}.json" with open(path) as f: data = json.load(f) i = len(data) r = str(random.randint(1, i)) update.effective_message.reply_text(data[r]) else: update.effective_message.reply_text( get_random_string("nice", lang.get_lang(update.effective_chat.id)))
def start(update: Update, context: CallbackContext): if update.effective_chat.type == "private": args = context.args if len(args) >= 1: if args[0].lower() == "help": HELP_STRINGS = get_string("main", "HELP_STRINGS", lang.get_lang(update.effective_chat.id)) send_help(update.effective_chat.id, HELP_STRINGS) elif args[0].lower().startswith("stngs_"): match = re.match("stngs_(.*)", args[0].lower()) chat = dispatcher.bot.getChat(match.group(1)) if is_user_admin(chat, update.effective_user.id): send_settings(match.group(1), update.effective_user.id, False) else: send_settings(match.group(1), update.effective_user.id, True) elif args[0][1:].isdigit() and "rules" in IMPORTED: IMPORTED["rules"].send_rules(update, args[0], from_pm=True) else: first_name = update.effective_user.first_name update.effective_message.reply_text( get_string("main", "PM_START_TEXT", lang.get_lang(update.effective_chat.id)).format(escape_markdown(first_name), escape_markdown(context.bot.first_name), OWNER_ID), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text(get_string("main", "START_IN_GROUP", lang.get_lang(update.effective_chat.id))) # START_IN_GROUP
def reply_afk(bot: Bot, update: Update): message = update.effective_message # type: Optional[Message] entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION]) if message.entities and entities: for ent in entities: if ent.type == MessageEntity.TEXT_MENTION: user_id = ent.user.id fst_name = ent.user.first_name elif ent.type == MessageEntity.MENTION: user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length]) if not user_id: # Should never happen, since for a user to become AFK they must have spoken. Maybe changed username? return chat = bot.get_chat(user_id) fst_name = chat.first_name else: return if sql.is_afk(user_id): valid, reason = sql.check_afk_status(user_id) if valid: if not reason: res = get_string("afk", "MSG_IS_AFK", lang.get_lang(update.effective_chat.id)).format(fst_name) # MSG_IS_AFK else: res = get_string("afk", "MSG_IS_AFK_REASON", lang.get_lang(update.effective_chat.id)).format(fst_name, reason) # MSG_IS_AFK_REASON message.reply_text(res)
def gbanlist(update: Update, context: CallbackContext): banned_users = sql.get_gban_list() if not banned_users: update.effective_message.reply_text( get_string("gbans", "NO_GBANS", lang.get_lang(update.effective_chat.id))) return banfile = get_string("gbans", "EXPORT_HEAD", lang.get_lang( update.effective_chat.id)) # EXPORT_HEAD for user in banned_users: banfile += "[x] {} - {}\n".format(user["name"], user["user_id"]) if user["reason"]: banfile += "Reason: {}\n".format(user["reason"]) # REASON with BytesIO(str.encode(banfile)) as output: output.name = "gbanlist.txt" update.effective_message.reply_document( document=output, filename="gbanlist.txt", caption=get_string( "gbans", "EXPORT_SUCCESS", lang.get_lang(update.effective_chat.id))) # EXPORT_SUCCESS
def settings_button(bot: Bot, update: Update): query = update.callback_query user = update.effective_user mod_match = re.match(r"stngs_module\((.+?),(.+?)\)", query.data) prev_match = re.match(r"stngs_prev\((.+?),(.+?)\)", query.data) next_match = re.match(r"stngs_next\((.+?),(.+?)\)", query.data) back_match = re.match(r"stngs_back\((.+?)\)", query.data) try: if mod_match: chat_id = mod_match.group(1) module = mod_match.group(2) chat = bot.get_chat(chat_id) text = get_string("main", "MODULE_SETTINGS", lang.get_lang(chat_id)).format(escape_markdown(chat.title), CHAT_SETTINGS[ module].__mod_name__) + \ CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id) # MODULE_SETTINGS query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="stngs_back({})".format(chat_id))]])) elif prev_match: chat_id = prev_match.group(1) curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(get_string("main", "LOT_OF_SETTINGS", lang.get_lang(chat_id)).format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", chat=chat_id))) # LOT_OF_SETTINGS elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(get_string("main", "LOT_OF_SETTINGS", lang.get_lang(chat_id)).format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", chat=chat_id))) # LOT_OF_SETTINGS elif back_match: chat_id = back_match.group(1) chat = bot.get_chat(chat_id) query.message.reply_text(text=get_string("main", "LOT_OF_SETTINGS", lang.get_lang(chat_id)).format(escape_markdown(chat.title)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id))) # LOT_OF_SETTINGS # ensure no spinny white circle bot.answer_callback_query(query.id) query.message.delete() except BadRequest as excp: if excp.message == "Message is not modified": # ERR_MSG_NOT_MODIFIED pass elif excp.message == "Query_id_invalid": # ERR_QUERY_ID_INVALID pass elif excp.message == "Message can't be deleted": # ERR_MSG_CANT_DELETE pass else: LOGGER.exception(get_string("main", "ERR_EXCP_SETTINGS_BUTTONS", DEFAULT_LANG), str(query.data)) # ERR_EXCP_SETTINGS_BUTTONS
def check_flood(bot: Bot, update: Update) -> 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: chat.kick_member(user.id) msg.reply_text(get_string("antiflood", "MSG_KICK", lang.get_lang(update.effective_chat.id))) # MSG_KICK return get_string("antiflood", "MSG_KICK_HTML", lang.get_lang(update.effective_chat.id)).format(html.escape(chat.title), mention_html(user.id, user.first_name)) # MSG_KICK_HTML except BadRequest: msg.reply_text(get_string("antiflood", "ERR_NO_PERMS", lang.get_lang(update.effective_chat.id))) # ERR_NO_PERMS sql.set_flood(chat.id, 0) return get_string("antiflood", "ERR_NO_PERMS_HTML", lang.get_lang(update.effective_chat.id)).format(chat.title) # ERR_NO_PERMS_HTML
def flood(update: Update, context: CallbackContext): chat = update.effective_chat # type: Optional[Chat] limit = sql.get_flood_limit(chat.id) if limit == 0: update.effective_message.reply_text(get_string("antiflood", "MSG_DISABLED", lang.get_lang(update.effective_chat.id))) # MSG_DISABLED else: update.effective_message.reply_text( get_string("antiflood", "MSG_INFO", lang.get_lang(update.effective_chat.id)).format(limit)) # MSG_INFO
def promote(update: Update, context: CallbackContext) -> str: args = context.args bot = context.bot chat_id = update.effective_chat.id message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] user_id = extract_user(message, args) if not user_id: message.reply_text( get_string("admin", "ERR_NO_USER", lang.get_lang(update.effective_chat.id))) # ERR_NO_USER return "" user_member = chat.get_member(user_id) if user_member.status in ("administrator", "creator"): message.reply_text( get_string( "admin", "ERR_CANT_PROMOTE_ADMIN", lang.get_lang( update.effective_chat.id))) # ERR_CANT_PROMOTE_ADMIN return "" if user_id == bot.id: message.reply_text( get_string( "admin", "ERR_CANT_PROMOTE_MYSELF", lang.get_lang( update.effective_chat.id))) # ERR_CANT_PROMOTE_MYSELF return "" # set same perms as bot - bot can't assign higher perms than itself! bot_member = chat.get_member(bot.id) bot.promoteChatMember( chat_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( get_string("admin", "PROMOTE_SUCCESS", lang.get_lang(update.effective_chat.id))) # PROMOTE_SUCCESS return get_string( "admin", "PROMOTE_SUCCESS_HTML", lang.get_lang(update.effective_chat.id)).format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name)) # PROMOTE_SUCCESS_HTML
def gbanstat(bot: Bot, update: Update, args: List[str]): if len(args) > 0: if args[0].lower() in ["on", "yes"]: sql.enable_gbans(update.effective_chat.id) update.effective_message.reply_text(get_string("gbans", "GBANS_ON", lang.get_lang(update.effective_chat.id))) # GBANS_ON elif args[0].lower() in ["off", "no"]: sql.disable_gbans(update.effective_chat.id) update.effective_message.reply_text(get_string("gbans", "GBANS_OFF", lang.get_lang(update.effective_chat.id))) # GBANS_OFF else: update.effective_message.reply_text(get_string("gbans", "GBAN_NO_ARGS", lang.get_lang(update.effective_chat.id)).format(sql.does_chat_gban(update.effective_chat.id))) # GBAN_NO_ARGS
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(get_string("bans", "ERR_NO_TARGET", lang.get_lang(update.effective_chat.id))) # ERR_NO_TARGET return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text(get_string("bans", "ERR_USER_NOT_FOUND", lang.get_lang(update.effective_chat.id))) # ERR_USER_NOT_FOUND return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text(get_string("bans", "ERR_TARGET_IS_ADMIN", lang.get_lang(update.effective_chat.id))) # ERR_TARGET_IS_ADMIN return "" if user_id == bot.id: message.reply_text(get_string("bans", "ERR_TARGET_ITSELF", lang.get_lang(update.effective_chat.id))) # ERR_TARGET_ITSELF return "" log = get_string("bans", "MSG_BAN_HTML", lang.get_lang(update.effective_chat.id)).format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id) # MSG_BAN_HTML if reason: log += get_string("bans", "MSG_BAN_HTML_REASON", lang.get_lang(update.effective_chat.id)).format(reason) # MSG_BAN_HTML_REASON try: chat.kick_member(user_id) # bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker message.reply_text(get_string("bans", "MSG_BAN_SUCCESS", lang.get_lang(update.effective_chat.id))) # MSG_BAN_SUCCESS return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text(get_string("bans", "MSG_BAN_SUCCESS", lang.get_lang(update.effective_chat.id)), quote=False) # MSG_BAN_SUCCESS return log else: LOGGER.warning(update) LOGGER.exception(get_string("bans", "ERR_CONSOLE_CANT_BAN", lang.get_lang(update.effective_chat.id)), user_id, chat.title, chat.id, excp.message) # ERR_CONSOLE_CANT_BAN message.reply_text(get_string("bans", "ERR_CANT_BAN", lang.get_lang(update.effective_chat.id))) # ERR_CANT_BAN return ""
def gdpr(bot: Bot, update: Update): update.effective_message.reply_text( get_string("misc", "MSG_DELETING_DATA", lang.get_lang( update.effective_chat.id))) # MSG_DELETING_DATA for mod in GDPR: mod.__gdpr__(update.effective_user.id) update.effective_message.reply_text( get_string("misc", "MSG_DELETING_SUCCESS", lang.get_lang(update.effective_chat.id)), parse_mode=ParseMode.MARKDOWN) # MSG_DELETING_SUCCESS
def markdown_help(bot: Bot, update: Update): update.effective_message.reply_text(get_string( "misc", "MARKDOWN_HELP", lang.get_lang(update.effective_chat.id)), parse_mode=ParseMode.HTML) update.effective_message.reply_text( get_string("misc", "MARKDOWN_HELP_FORWARD", lang.get_lang( update.effective_chat.id))) # MARKDOWN_HELP_FORWARD update.effective_message.reply_text( get_string("misc", "MARKDOWN_HELP_FORWARD_MSG", lang.get_lang( update.effective_chat.id))) # MARKDOWN_HELP_FORWARD_MSG
def get_time(update: Update, context: CallbackContext): # This does not work args = context.args bot = context.bot location = " ".join(args) if location.lower() == bot.first_name.lower(): update.effective_message.reply_text( get_string("misc", "MSG_BANHAMMER_TIME", lang.get_lang( update.effective_chat.id))) # MSG_BANHAMMER_TIME bot.send_sticker(update.effective_chat.id, BAN_STICKER) return res = requests.get(GMAPS_LOC, params=dict(address=location)) if res.status_code == 200: loc = json.loads(res.text) if loc.get('status') == 'OK': lat = loc['results'][0]['geometry']['location']['lat'] long = loc['results'][0]['geometry']['location']['lng'] country = None city = None address_parts = loc['results'][0]['address_components'] for part in address_parts: if 'country' in part['types']: country = part.get('long_name') if 'administrative_area_level_1' in part['types'] and not city: city = part.get('long_name') if 'locality' in part['types']: city = part.get('long_name') if city and country: location = "{}, {}".format(city, country) elif country: location = country timenow = int(datetime.utcnow().timestamp()) res = requests.get(GMAPS_TIME, params=dict(location="{},{}".format(lat, long), timestamp=timenow)) if res.status_code == 200: offset = json.loads(res.text)['dstOffset'] timestamp = json.loads(res.text)['rawOffset'] time_there = datetime.fromtimestamp(timenow + timestamp + offset).strftime( "%H:%M:%S on %A %d %B") update.message.reply_text( get_string("misc", "MSG_TIME", lang.get_lang(update.effective_chat.id)).format( time_there, location)) # MSG_TIME
def build_curr_disabled(chat_id: Union[str, int]) -> str: disabled = sql.get_all_disabled(chat_id) if not disabled: return get_string( "disable", "NO_CMD_DISABLED", lang.get_lang(chat_id.effective_chat.id)) # NO_CMD_DISABLED result = "" for cmd in disabled: result += " - `{}`\n".format(escape_markdown(cmd)) return get_string("disable", "DISABLED_COMMANDS", lang.get_lang(update.effective_chat.id)).format( result) # DISABLED_COMMANDS
def reply_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] to_match = extract_text(message) if not to_match: return chat_filters = sql.get_chat_triggers(chat.id) for keyword in chat_filters: pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])" if re.search(pattern, to_match, flags=re.IGNORECASE): filt = sql.get_filter(chat.id, keyword) if filt.is_sticker: message.reply_sticker(filt.reply) elif filt.is_document: message.reply_document(filt.reply) elif filt.is_image: message.reply_photo(filt.reply) elif filt.is_audio: message.reply_audio(filt.reply) elif filt.is_voice: message.reply_voice(filt.reply) elif filt.is_video: message.reply_video(filt.reply) elif filt.has_markdown: buttons = sql.get_buttons(chat.id, filt.keyword) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) try: message.reply_text(filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Unsupported url protocol": message.reply_text(get_string("filters", "ERR_BAD_URL", lang.get_lang(update.effective_chat.id))) # ERR_BAD_URL elif excp.message == "Reply message not found": bot.send_message(chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) else: message.reply_text(get_string("filters", "ERR_NO_REPLY", lang.get_lang(update.effective_chat.id))) # ERR_NO_REPLY LOGGER.warning(get_string("filters", "ERR_COULDNT_PARSE", DEFAULT_LANG), str(filt.reply)) # ERR_COULDNT_PARSE LOGGER.exception(get_string("filters", "ERR_COULDNT_PARSE_FILTER", DEFAULT_LANG), str(filt.keyword), str(chat.id)) # ERR_COULDNT_PARSE_FILTER else: # LEGACY - all new filters will have has_markdown set to True. message.reply_text(filt.reply) break
def list_cmds(update: Update, context: CallbackContext): if DISABLE_CMDS + DISABLE_OTHER: result = "" for cmd in set(DISABLE_CMDS + DISABLE_OTHER): result += " - `{}`\n".format(escape_markdown(cmd)) update.effective_message.reply_text( get_string("disable", "LIST_OF_COMMANDS", lang.get_lang( update.effective_chat.id)).format(result), parse_mode=ParseMode.MARKDOWN) # LIST_OF_COMMANDS else: update.effective_message.reply_text( get_string("disable", "NO_CMD_AVAILABLE", lang.get_lang( update.effective_chat.id))) # NO_CMD_AVAILABLE
def adminlist(update: Update, context: CallbackContext): administrators = update.effective_chat.get_administrators() text = get_string( "admin", "ADMINS_IN", lang.get_lang(update.effective_chat.id)).format( update.effective_chat.title or get_string( "admin", "THIS_CHAT", lang.get_lang( update.effective_chat.id))) # ADMINS_IN and THIS_CHAT for admin in administrators: user = admin.user name = "[{}](tg://user?id={})".format( user.first_name + (user.last_name or ""), user.id) if user.username: name = escape_markdown("@" + user.username) text += "\n - {}".format(name) # TODO Maybe add some logic for titles and different roles? update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
def adminlist(bot: Bot, update: Update): administrators = update.effective_chat.get_administrators() text = get_string( "admin", "ADMINS_IN", lang.get_lang(update.effective_chat.id)).format( update.effective_chat.title or get_string( "admin", "THIS_CHAT", lang.get_lang( update.effective_chat.id))) # ADMINS_IN and THIS_CHAT for admin in administrators: user = admin.user name = "[{}](tg://user?id={})".format( user.first_name + (user.last_name or ""), user.id) if user.username: name = escape_markdown("@" + user.username) text += "\n - {}".format(name) update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
def pin(bot: Bot, update: Update, args: List[str]) -> str: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] is_group = chat.type != "private" and chat.type != "channel" prev_message = update.effective_message.reply_to_message is_silent = True if len(args) >= 1: is_silent = not (args[0].lower() == 'notify' or args[0].lower() == 'loud' or args[0].lower() == 'violent') if prev_message and is_group: try: bot.pinChatMessage(chat.id, prev_message.message_id, disable_notification=is_silent) except BadRequest as excp: if excp.message == "Chat_not_modified": pass else: raise return get_string("admin", "PINNED_HTML", lang.get_lang(update.effective_chat.id)).format( html.escape(chat.title), mention_html(user.id, user.first_name)) # PINNED_HTML return ""
def setlang(update: Update, context: CallbackContext): chat_id = update.effective_chat.id msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and args if len(args) == 2: txt = args[1] SUPPORTED_LANGUAGES = ["de", "en"] if txt not in SUPPORTED_LANGUAGES: msg.reply_text(get_string("lang", "ERR_UNKNOWN_LANG", sql.get_lang(chat_id))) # ERR_UNKNOWN_LANG else: sql.set_lang(chat_id, txt) msg.reply_text(get_string("lang", "LANG_SET", sql.get_lang(chat_id))) # LANG_SET else: msg.reply_text(get_string("lang", "ERR_NO_LANG", sql.get_lang(chat_id))) # ERR_NO_LANG
def toggle_mute(update: Update, context: CallbackContext) -> str: msg = update.effective_message chat = update.effective_chat is_muted = mute_sql.get_muted(chat.id) if is_muted: mute_sql.set_muted(chat.id, False) msg.reply_text( get_string("admin", "MSG_CHAT_UMUTED", lang.get_lang(chat.id))) # MSG_CHAT_UMUTED return f"{update.effective_user.first_name} unmuted the chat!" if not is_muted: mute_sql.set_muted(chat.id, True) msg.reply_text( get_string("admin", "MSG_CHAT_MUTED", lang.get_lang(chat.id))) # MSG_CHAT_MUTED return f"{update.effective_user.first_name} muted the chat!"
def pin(update: Update, context: CallbackContext) -> str: args = context.args bot = context.bot user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] is_group = chat.type not in ("private", "channel") prev_message = update.effective_message.reply_to_message is_silent = True if len(args) >= 1: is_silent = not (args[0].lower() in ('notify', 'loud', 'violent')) if prev_message and is_group: try: bot.pinChatMessage(chat.id, prev_message.message_id, disable_notification=is_silent) except BadRequest as excp: if excp.message == "Chat_not_modified": pass else: raise return get_string("admin", "PINNED_HTML", lang.get_lang(update.effective_chat.id)).format( html.escape(chat.title), mention_html(user.id, user.first_name)) # PINNED_HTML return ""
def set_flood(update: Update, context: CallbackContext) -> str: args = context.args chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] if len(args) >= 1: val = args[0].lower() if val in ("off", "no", "0"): sql.set_flood(chat.id, 0) message.reply_text(get_string("antiflood", "MSG_DISABLED", lang.get_lang(update.effective_chat.id))) # MSG_DISABLED elif val.isdigit(): amount = int(val) if amount <= 0: sql.set_flood(chat.id, 0) message.reply_text(get_string("antiflood", "MSG_DISABLED", lang.get_lang(update.effective_chat.id))) # MSG_DISABLED return get_string("antiflood", "MSG_DISABLED_HTML", lang.get_lang(update.effective_chat.id)).format(html.escape(chat.title), mention_html(user.id, user.first_name)) # MSG_DISABLED_HTML if amount < 3: message.reply_text(get_string("antiflood", "ERR_BAD_AMOUNT", lang.get_lang(update.effective_chat.id))) # ERR_BAD_AMOUNT return "" sql.set_flood(chat.id, amount) message.reply_text(get_string("antiflood", "MSG_SUCCESS", lang.get_lang(update.effective_chat.id)).format(amount)) # MSG_SUCCESS return get_string("antiflood", "MSG_SUCCESS_HTML", lang.get_lang(update.effective_chat.id)).format(html.escape(chat.title), mention_html(user.id, user.first_name), amount) # MSG_SUCCESS_HTML else: message.reply_text(get_string("antoflood", "ERR_BAD_REQUEST", lang.get_lang(update.effective_chat.id))) # ERR_BAD_REQUEST return ""
def check_and_ban(update, user_id, should_message=True): if sql.is_user_gbanned(user_id): update.effective_chat.kick_member(user_id) if should_message: update.effective_message.reply_text( get_string( "gbans", "THIS_IS_A_BAD_PERSON", lang.get_lang( update.effective_chat.id))) # THIS_IS_A_BAD_PERSON
def no_longer_afk(bot: Bot, update: Update): user = update.effective_user # type: Optional[User] if not user: # ignore channels return res = sql.rm_afk(user.id) if res: update.effective_message.reply_text(get_string("afk", "MSG_IS_NOT_AFK", lang.get_lang(update.effective_chat.id)).format(update.effective_user.first_name)) # MSG_IS_NOT_AFK