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 do_translate(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat msg = update.effective_message if msg.reply_to_message: to_translate_text = remove_emoji(msg.reply_to_message.text) else: msg.reply_text(tld(chat.id, "translator_no_str")) return if not args: msg.reply_text(tld(chat.id, 'translator_no_args')) return lang = args[0] translator = Translator() try: translated = translator.translate(to_translate_text, dest=lang) except ValueError as e: msg.reply_text(tld(chat.id, 'translator_err').format(e)) return src_lang = LANGUAGES[f'{translated.src.lower()}'].title() dest_lang = LANGUAGES[f'{translated.dest.lower()}'].title() translated_text = translated.text msg.reply_text(tld(chat.id, 'translator_translated').format(src_lang, to_translate_text, dest_lang, translated_text), parse_mode=ParseMode.MARKDOWN)
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 ""
def blacklist(bot: Bot, update: Update, args: List[str]): msg = update.effective_message chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if chat.type == "private": return else: chat_id = update.effective_chat.id chat_name = chat.title filter_list = tld(chat.id, "blacklist_active_list").format(chat_name) all_blacklisted = sql.get_chat_blacklist(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 filter_list == tld(chat.id, "blacklist_active_list").format( chat_name): #We need to translate msg.reply_text(tld(chat.id, "blacklist_no_list").format(chat_name), parse_mode=ParseMode.HTML) return msg.reply_text(text, parse_mode=ParseMode.HTML)
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 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 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 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 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 security_text(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: text = " ".join(args) sql.set_welcome_security(chat.id, getcur, cur_value, text) text = tld(chat.id, 'welcome_mute_btn_text_changed').format(text) update.effective_message.reply_text(text, parse_mode="markdown") else: update.effective_message.reply_text(tld( chat.id, 'welcome_mute_btn_curr_text').format(cust_text), parse_mode="markdown")
def add_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) >= 2: # set trigger -> lower, so as to avoid adding duplicate filters with different cases keyword = extracted[0].lower() content = extracted[1] else: return # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(WARN_HANDLER_GROUP, []): if handler.filters == (keyword, chat.id): dispatcher.remove_handler(handler, WARN_HANDLER_GROUP) sql.add_warn_filter(chat.id, keyword, content) update.effective_message.reply_text( tld(chat.id, 'warns_handler_add_success').format(keyword)) raise DispatcherHandlerStop
def reset_warns(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message chat = update.effective_chat user = update.effective_user user_id = extract_user(message, args) if user_id: sql.reset_warns(user_id, chat.id) message.reply_text(tld(chat.id, 'warns_reset_success')) warned = chat.get_member(user_id).user return tld(chat.id, 'warns_reset_log_channel').format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(warned.id, warned.first_name), warned.id) else: message.reply_text(tld(chat.id, 'common_err_no_user')) return ""
def set_warn_strength(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user msg = update.effective_message if args: if args[0].lower() in ("on", "yes"): sql.set_warn_strength(chat.id, False) msg.reply_text(tld(chat.id, 'warns_strength_on')) return tld(chat.id, 'warns_strength_on_log_channel').format( html.escape(chat.title), mention_html(user.id, user.first_name)) elif args[0].lower() in ("off", "no"): sql.set_warn_strength(chat.id, True) msg.reply_text(tld(chat.id, 'warns_strength_off')) return tld(chat.id, 'warns_strength_off_log_channel').format( html.escape(chat.title), mention_html(user.id, user.first_name)) else: msg.reply_text(tld(chat.id, 'warns_strength_invalid_arg')) else: soft_warn = sql.get_soft_warn(chat.id) if soft_warn: msg.reply_text(tld(chat.id, 'warns_strength_off'), parse_mode=ParseMode.MARKDOWN) else: msg.reply_text(tld(chat.id, 'warns_strength_on'), parse_mode=ParseMode.MARKDOWN) return ""
def paginate_modules(chat_id, page_n: int, module_dict: Dict, prefix, chat=None) -> List: if not chat: modules = sorted([ EqInlineKeyboardButton(tld(chat_id, x.__mod_name__), callback_data="{}_module({})".format( prefix, x.__mod_name__.lower())) for x in module_dict.values() ]) else: modules = sorted([ EqInlineKeyboardButton(tld(chat_id, x.__mod_name__), callback_data="{}_module({},{})".format( prefix, chat, x.__mod_name__.lower())) for x in module_dict.values() ]) pairs = list(zip(modules[::2], modules[1::2])) if len(modules) % 2 == 1: pairs.append((modules[-1], )) max_num_pages = ceil(len(pairs) / 7) modulo_page = page_n % max_num_pages # can only have a certain amount of buttons side by side if len(pairs) > 7: pairs = pairs[modulo_page * 7:7 * (modulo_page + 1)] + [ (EqInlineKeyboardButton( "<<", callback_data="{}_prev({})".format(prefix, modulo_page)), EqInlineKeyboardButton("⬅️ Back", callback_data="bot_start"), EqInlineKeyboardButton(">>", callback_data="{}_next({})".format( prefix, modulo_page))) ] else: pairs += [[ EqInlineKeyboardButton("⬅️ Back", callback_data="bot_start") ]] return pairs
def reset_goodbye(bot: Bot, update: Update) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] sql.set_custom_gdbye(chat.id, None, sql.DEFAULT_GOODBYE, sql.Types.TEXT) update.effective_message.reply_text( tld(chat.id, 'welcome_reset_goodbye_success')) return "<b>{}:</b>" \ "\n#RESET_GOODBYE" \ "\n<b>Admin:</b> {}" \ "\nReset the goodbye message.".format(escape(chat.title), mention_html(user.id, user.first_name))
def check_bot_button(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] query = update.callback_query # type: Optional[CallbackQuery] getalluser = sql.get_chat_userlist(chat.id) if user.id in getalluser: query.answer(text=tld(chat.id, 'welcome_mute_btn_unmuted')) # Unmute user bot.restrict_chat_member(chat.id, user.id, can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True) sql.rm_from_userlist(chat.id, user.id) else: try: query.answer(text=tld(chat.id, 'welcome_mute_btn_old_user')) except Exception: print("Nut")
def get_help(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] args = update.effective_message.text.split(None, 1) # ONLY send help in PM if chat.type != chat.PRIVATE: update.effective_message.reply_text( "Contact me in PM to get the list of possible commands.", reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton(text="Help", url="t.me/{}?start=help".format( bot.username)) ]])) return elif len(args) >= 2 and any(args[1].lower() == x for x in HELPABLE): module = args[1].lower() mod_name = tld(chat.id, HELPABLE[module].__mod_name__) help_txt = tld_help(chat.id, HELPABLE[module].__mod_name__) if help_txt == False: help_txt = HELPABLE[module].__help__ text = tld(chat.id, "Here is the help for the *{}* module:\n{}").format( mod_name, help_txt) send_help( chat.id, text, InlineKeyboardMarkup([[ InlineKeyboardButton(text=tld(chat.id, "Back"), callback_data="help_back") ]])) else: send_help( chat.id, tld(chat.id, "send-help").format( dispatcher.bot.first_name, "" if not ALLOW_EXCL else tld( chat.id, "\nAll commands can either be used with `/` or `!`.\n")))
def getsticker(bot: Bot, update: Update): msg = update.effective_message chat_id = update.effective_chat.id if msg.reply_to_message and msg.reply_to_message.sticker: file_id = msg.reply_to_message.sticker.file_id newFile = bot.get_file(file_id) newFile.download('images/sticker.png') bot.send_document(chat_id, document=open('images/sticker.png', 'rb')) os.remove("images/sticker.png") else: update.effective_message.reply_text( tld(chat_id, 'stickers_getsticker_no_reply'))
def cleanservice(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] if chat.type != chat.PRIVATE: if len(args) >= 1: var = args[0] if (var == "no" or var == "off"): sql.set_clean_service(chat.id, False) update.effective_message.reply_text( tld(chat.id, 'welcome_clean_service_off')) elif (var == "yes" or var == "on"): sql.set_clean_service(chat.id, True) update.effective_message.reply_text( tld(chat.id, 'welcome_clean_service_on')) else: update.effective_message.reply_text( tld(chat.id, 'common_invalid_arg'), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text(tld(chat.id, 'common_invalid_arg'), parse_mode=ParseMode.MARKDOWN) else: curr = sql.clean_service(chat.id) if curr: update.effective_message.reply_text(tld( chat.id, 'welcome_clean_service_on'), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text(tld( chat.id, 'welcome_clean_service_off'), parse_mode=ParseMode.MARKDOWN)
def start(bot: Bot, update: Update, args: List[str]): LOGGER.info("Start") chat = update.effective_chat # type: Optional[Chat] #query = update.callback_query #Unused variable if update.effective_chat.type == "private": if len(args) >= 1: if args[0].lower() == "help": send_help( update.effective_chat.id, tld(chat.id, "send-help").format( dispatcher.bot.first_name, "" if not ALLOW_EXCL else tld( chat.id, "\nAll commands can either be used with `/` or `!`.\n" ))) 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, update, user=False) else: send_settings(match.group(1), update.effective_user.id, update, user=True) elif args[0][1:].isdigit() and "rules" in IMPORTED: IMPORTED["rules"].send_rules(update, args[0], from_pm=True) elif args[0].lower() == "controlpanel": control_panel(bot, update) else: send_start(bot, update) else: update.effective_message.reply_text("I'm alive")
def list_warn_filters(bot: Bot, update: Update): chat = update.effective_chat all_handlers = sql.get_chat_warn_triggers(chat.id) if not all_handlers: update.effective_message.reply_text( tld(chat.id, 'warns_filters_list_empty')) return filter_list = tld(chat.id, 'warns_filters_list') for keyword in all_handlers: entry = " - {}\n".format(html.escape(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: update.effective_message.reply_text(filter_list, parse_mode=ParseMode.HTML) filter_list = entry else: filter_list += entry if not filter_list == tld(chat.id, 'warns_filters_list'): update.effective_message.reply_text(filter_list, parse_mode=ParseMode.HTML)
def add_blacklist(bot: Bot, update: Update): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) 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": return else: chat_name = chat.title 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(tld(chat.id, "blacklist_add").format( html.escape(to_blacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_add").format(len(to_blacklist)), chat_name, parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_err_add_no_args"))
def makepack_internal(msg, user, png_sticker, emoji, bot, packname, packnum, chat): name = user.first_name name = name[:50] try: extra_version = "" if packnum > 0: extra_version = " " + str(packnum) success = bot.create_new_sticker_set(user.id, packname, f"{name}'s Sara😉 pack" + extra_version, png_sticker=png_sticker, emojis=emoji) except TelegramError as e: print(e) if e.message == "Sticker set name is already occupied": msg.reply_text(tld(chat.id, 'stickers_pack_name_exists') % packname, parse_mode=ParseMode.MARKDOWN) elif e.message == "Peer_id_invalid": msg.reply_text(tld(chat.id, 'stickers_pack_contact_pm'), reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton(text="Start", url=f"t.me/{bot.username}") ]])) elif e.message == "Internal Server Error: created sticker set not found (500)": msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) return if success: msg.reply_text(tld(chat.id, 'stickers_kang_success').format(packname, emoji), parse_mode=ParseMode.MARKDOWN) else: msg.reply_text(tld(chat.id, 'stickers_pack_create_error'))
def clean_welcome(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] if not args: clean_pref = sql.get_clean_pref(chat.id) if clean_pref: update.effective_message.reply_text( tld(chat.id, 'welcome_clean_curr_on')) else: update.effective_message.reply_text( tld(chat.id, 'welcome_clean_curr_off')) return "" if args[0].lower() in ("on", "yes"): sql.set_clean_welcome(str(chat.id), True) update.effective_message.reply_text( tld(chat.id, 'welcome_clean_set_on')) return "<b>{}:</b>" \ "\n#CLEAN_WELCOME" \ "\n<b>Admin:</b> {}" \ "\nHas toggled clean welcomes to <code>ON</code>.".format(escape(chat.title), mention_html(user.id, user.first_name)) elif args[0].lower() in ("off", "no"): sql.set_clean_welcome(str(chat.id), False) update.effective_message.reply_text( tld(chat.id, 'welcome_clean_set_off')) return "<b>{}:</b>" \ "\n#CLEAN_WELCOME" \ "\n<b>Admin:</b> {}" \ "\nHas toggled clean welcomes to <code>OFF</code>.".format(escape(chat.title), mention_html(user.id, user.first_name)) else: # idek what you're writing, say yes or no update.effective_message.reply_text( "I understand 'on/yes' or 'off/no' only!") return ""
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 unblacklist(bot: Bot, update: Update): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) 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": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_unblacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_blacklist(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: msg.reply_text(tld(chat.id, "blacklist_del").format( html.escape(to_unblacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_err_not_trigger")) elif successful == len(to_unblacklist): msg.reply_text(tld(chat.id, "blacklist_multi_del").format( successful, chat_name), parse_mode=ParseMode.HTML) elif not successful: msg.reply_text(tld(chat.id, "blacklist_err_multidel_no_trigger").format( successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text(tld( chat.id, "blacklist_err_multidel_some_no_trigger").format( successful, chat_name, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_err_del_no_args"))
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 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 kang(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat msg = update.effective_message user = update.effective_user packnum = 0 packname = "f" + str(user.id) + "_by_" + bot.username packname_found = 0 max_stickers = 120 while packname_found == 0: try: stickerset = bot.get_sticker_set(packname) if len(stickerset.stickers) >= max_stickers: packnum += 1 packname = "f" + str(packnum) + "_" + str( user.id) + "_by_" + bot.username else: packname_found = 1 except TelegramError as e: if e.message == "Stickerset_invalid": packname_found = 1 kangsticker = "images/kangsticker.png" if msg.reply_to_message: if msg.reply_to_message.sticker: file_id = msg.reply_to_message.sticker.file_id elif msg.reply_to_message.photo: file_id = msg.reply_to_message.photo[-1].file_id elif msg.reply_to_message.document: file_id = msg.reply_to_message.document.file_id else: msg.reply_text(tld(chat.id, 'stickers_kang_error')) kang_file = bot.get_file(file_id) kang_file.download('images/kangsticker.png') if args: sticker_emoji = str(args[0]) elif msg.reply_to_message.sticker and msg.reply_to_message.sticker.emoji: sticker_emoji = msg.reply_to_message.sticker.emoji else: sticker_emoji = "🤔" try: im = Image.open(kangsticker) maxsize = (512, 512) if (im.width and im.height) < 512: size1 = im.width size2 = im.height if im.width > im.height: scale = 512 / size1 size1new = 512 size2new = size2 * scale else: scale = 512 / size2 size1new = size1 * scale size2new = 512 size1new = math.floor(size1new) size2new = math.floor(size2new) sizenew = (size1new, size2new) im = im.resize(sizenew) else: im.thumbnail(maxsize) if not msg.reply_to_message.sticker: im.save(kangsticker, "PNG") bot.add_sticker_to_set(user_id=user.id, name=packname, png_sticker=open('images/kangsticker.png', 'rb'), emojis=sticker_emoji) msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) except OSError as e: msg.reply_text(tld(chat.id, 'stickers_kang_only_img')) print(e) return except TelegramError as e: if e.message == "Stickerset_invalid": makepack_internal(msg, user, open('images/kangsticker.png', 'rb'), sticker_emoji, bot, packname, packnum, chat) elif e.message == "Sticker_png_dimensions": im.save(kangsticker, "PNG") bot.add_sticker_to_set(user_id=user.id, name=packname, png_sticker=open( 'images/kangsticker.png', 'rb'), emojis=sticker_emoji) msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) elif e.message == "Invalid sticker emojis": msg.reply_text(tld(chat.id, 'stickers_kang_invalid_emoji')) elif e.message == "Stickers_too_much": msg.reply_text(tld(chat.id, 'stickers_kang_too_much')) elif e.message == "Internal Server Error: sticker set not found (500)": msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) print(e) elif args: try: try: urlemoji = msg.text.split(" ") png_sticker = urlemoji[1] sticker_emoji = urlemoji[2] except IndexError: sticker_emoji = "🤔" urllib.urlretrieve(png_sticker, kangsticker) im = Image.open(kangsticker) maxsize = (512, 512) if (im.width and im.height) < 512: size1 = im.width size2 = im.height if im.width > im.height: scale = 512 / size1 size1new = 512 size2new = size2 * scale else: scale = 512 / size2 size1new = size1 * scale size2new = 512 size1new = math.floor(size1new) size2new = math.floor(size2new) sizenew = (size1new, size2new) im = im.resize(sizenew) else: im.thumbnail(maxsize) im.save(kangsticker, "PNG") msg.reply_photo(photo=open('images/kangsticker.png', 'rb')) bot.add_sticker_to_set(user_id=user.id, name=packname, png_sticker=open('images/kangsticker.png', 'rb'), emojis=sticker_emoji) msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) except OSError as e: msg.reply_text(tld(chat.id, 'stickers_kang_only_img')) print(e) return except TelegramError as e: if e.message == "Stickerset_invalid": makepack_internal(msg, user, open('images/kangsticker.png', 'rb'), sticker_emoji, bot, packname, packnum, chat) elif e.message == "Sticker_png_dimensions": im.save(kangsticker, "PNG") bot.add_sticker_to_set(user_id=user.id, name=packname, png_sticker=open( 'images/kangsticker.png', 'rb'), emojis=sticker_emoji) msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) elif e.message == "Invalid sticker emojis": msg.reply_text(tld(chat.id, 'stickers_kang_invalid_emoji')) elif e.message == "Stickers_too_much": msg.reply_text(tld(chat.id, 'stickers_kang_too_much')) elif e.message == "Internal Server Error: sticker set not found (500)": msg.reply_text(tld(chat.id, 'stickers_kang_success').format( packname, sticker_emoji), parse_mode=ParseMode.MARKDOWN) print(e) else: packs = tld(chat.id, 'stickers_kang_no_reply') if packnum > 0: firstpackname = "f" + str(user.id) + "_by_" + bot.username for i in range(0, packnum + 1): if i == 0: packs += f"[pack](t.me/addstickers/{firstpackname})\n" else: packs += f"[pack{i}](t.me/addstickers/{packname})\n" else: packs += f"[pack](t.me/addstickers/{packname})" msg.reply_text(packs, parse_mode=ParseMode.MARKDOWN) if os.path.isfile("images/kangsticker.png"): os.remove("images/kangsticker.png")
def settings_button(bot: Bot, update: Update): query = update.callback_query user = update.effective_user chatP = update.effective_chat # type: Optional[Chat] 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 = "*{}* has the following settings for the *{}* module:\n\n".format(escape_markdown(chat.title), CHAT_SETTINGS[ module].__mod_name__) + \ CHAT_SETTINGS[module].__chat_settings__(bot, update, chat, chatP, user) 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(tld( user.id, "send-group-settings").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", chat=chat_id))) 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(tld( user.id, "send-group-settings").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", chat=chat_id))) elif back_match: chat_id = back_match.group(1) chat = bot.get_chat(chat_id) query.message.reply_text(text=tld(user.id, "send-group-settings").format( escape_markdown(chat.title)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( paginate_modules(user.id, 0, CHAT_SETTINGS, "stngs", chat=chat_id))) # 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": pass elif excp.message == "Query_id_invalid": pass elif excp.message == "Message can't be deleted": pass else: LOGGER.exception("Exception in settings buttons. %s", str(query.data))