diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index cbc60f8..4f13230 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -19,17 +19,9 @@ PM_START_TEXT = """ -ഹായ് {}, എന്റെ പേര് {}! ഞാൻ [ഇദ്ദേഹം](tg://user?id={}) നോക്കി നടത്തുന്ന ഒരു അടിപൊളി അഡ്മിൻ ബോട്ടാണ്. +hello {}, my name is {}! welocome welcome [welocome](tg://user?id={}) welocme. -എന്നെ നിർമ്മിച്ചിരിക്കുന്നത് python3 യിൽ python-telegram-bot ലൈബ്രറി ഉപയോഗിച്ചാണ്. ഞാൻ പൂർണ്ണമായിട്ടും ഓപ്പൺസോഴ്സ്ഡ് ആണ്. എന്റെ കോഡ് നിങ്ങൾക്ക് [ഇവിടെ](https://youtu.be/wKL90i3cjPw) കാണുവാൻ സാധിക്കും. - -എന്നെപ്പോലെ ഒരു അഡ്മിൻ ബോട്ടിനെ ഉണ്ടാക്കുവാൻ താഴെ കൊടുത്തിരിക്കുന്ന വീഡിയോ കാണുക. - -എന്റെ അപ്ഡേറ്റുകളെക്കുറിച്ചും പ്രവർത്തനത്തെപറ്റിയും അറിയുവാൻ അപ്ഡേറ്റ് ചാനൽ സബ്സ്ക്രൈബ് ചെയ്യുക. - -കൂടെ താഴെ കൊടുത്തിരിക്കുന്ന ടെക് ഗെയിമിംഗ് യൂട്യൂബ് ചാനൽ സബ്സ്ക്രൈബ് ചെയ്യാനും മറക്കേണ്ട. - -ലഭ്യമായ കമാന്റുകളെപ്പറ്റി അറിയുവാൻ /help അമർത്തുക. +add me in your group and /help to check commands if you have any doubts contact my owner https://t.me/DHRUVATHEGENIUS for any help. """ @@ -47,7 +39,10 @@ - in a group: will redirect you to pm, with all that chat's settings. {} And the following: -""".format(dispatcher.bot.first_name, "" if not ALLOW_EXCL else "\nAll commands can either be used with / or !.\n") +""".format( + dispatcher.bot.first_name, + "\nAll commands can either be used with / or !.\n" if ALLOW_EXCL else "", +) DONATE_STRING = """Heya, glad to hear you want to donate! It took lots of work for [my creator](t.me/SonOfLars) to get me to where I am now, and every donation helps \ @@ -69,11 +64,11 @@ GDPR = [] for module_name in ALL_MODULES: - imported_module = importlib.import_module("tg_bot.modules." + module_name) + imported_module = importlib.import_module(f"tg_bot.modules.{module_name}") if not hasattr(imported_module, "__mod_name__"): imported_module.__mod_name__ = imported_module.__name__ - if not imported_module.__mod_name__.lower() in IMPORTED: + if imported_module.__mod_name__.lower() not in IMPORTED: IMPORTED[imported_module.__mod_name__.lower()] = imported_module else: raise Exception("Can't have two modules with the same name! Please change one") @@ -128,18 +123,18 @@ def test(bot: Bot, update: Update): @run_async def start(bot: Bot, update: Update, args: List[str]): if update.effective_chat.type == "private": - if len(args) >= 1: + if args: if args[0].lower() == "help": 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)) + chat = dispatcher.bot.getChat(match[1]) if is_user_admin(chat, update.effective_user.id): - send_settings(match.group(1), update.effective_user.id, False) + send_settings(match[1], update.effective_user.id, False) else: - send_settings(match.group(1), update.effective_user.id, True) + send_settings(match[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) @@ -147,12 +142,47 @@ def start(bot: Bot, update: Update, args: List[str]): else: first_name = update.effective_user.first_name update.effective_message.reply_text( - PM_START_TEXT.format(escape_markdown(first_name), escape_markdown(bot.first_name), OWNER_ID), - - parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton(text="🎉 Add me to your group", url="t.me/{}?startgroup=true".format(bot.username)), InlineKeyboardButton(text="🤖 Make Own Admin Bot", url="https://youtu.be/W6CLKrehy6w")], - [InlineKeyboardButton(text="👥 Support Group", url="https://t.me/Mo_Tech_Group"), InlineKeyboardButton(text="🔔 Update Channel", url="https://t.me/Mo_Tech_YT")], - [InlineKeyboardButton(text="👨‍💻 Make", url="https://youtu.be/wKL90i3cjPw"), InlineKeyboardButton(text="🛠 Help", url="https://t.me/{}?start=help".format(bot.username)) ]])) + PM_START_TEXT.format( + escape_markdown(first_name), + escape_markdown(bot.first_name), + OWNER_ID, + ), + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + text="🎉 Add me to your group", + url=f"t.me/{bot.username}?startgroup=true", + ), + InlineKeyboardButton( + text="🤖 Make Own Admin Bot", + url="https://youtu.be/W6CLKrehy6w", + ), + ], + [ + InlineKeyboardButton( + text="👥 Support Group", + url="https://t.me/Mo_Tech_Group", + ), + InlineKeyboardButton( + text="🔔 Update Channel", + url="https://t.me/Mo_Tech_YT", + ), + ], + [ + InlineKeyboardButton( + text="👨‍💻 Make", + url="https://youtu.be/wKL90i3cjPw", + ), + InlineKeyboardButton( + text="🛠 Help", + url=f"https://t.me/{bot.username}?start=help", + ), + ], + ] + ), + ) else: update.effective_message.reply_text("ചത്തിട്ടില്ലാ...") @@ -196,23 +226,22 @@ def help_button(bot: Bot, update: Update): back_match = re.match(r"help_back", query.data) try: if mod_match: - module = mod_match.group(1) - text = "Here is the help for the *{}* module:\n".format(HELPABLE[module].__mod_name__) \ - + HELPABLE[module].__help__ + module = mod_match[1] + text = f"Here is the help for the *{HELPABLE[module].__mod_name__}* module:\n{HELPABLE[module].__help__}" query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="help_back")]])) elif prev_match: - curr_page = int(prev_match.group(1)) + curr_page = int(prev_match[1]) query.message.reply_text(HELP_STRINGS, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, HELPABLE, "help"))) elif next_match: - next_page = int(next_match.group(1)) + next_page = int(next_match[1]) query.message.reply_text(HELP_STRINGS, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( @@ -227,13 +256,11 @@ def help_button(bot: Bot, update: Update): 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: + if excp.message not in [ + "Message is not modified", + "Query_id_invalid", + "Message can't be deleted", + ]: LOGGER.exception("Exception in help buttons. %s", str(query.data)) @@ -245,17 +272,23 @@ def get_help(bot: Bot, update: Update): # 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))]])) + update.effective_message.reply_text( + "Contact me in PM to get the list of possible commands.", + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + text="Help", url=f"t.me/{bot.username}?start=help" + ) + ] + ] + ), + ) return elif len(args) >= 2 and any(args[1].lower() == x for x in HELPABLE): module = args[1].lower() - text = "Here is the available help for the *{}* module:\n".format(HELPABLE[module].__mod_name__) \ - + HELPABLE[module].__help__ + text = f"Here is the available help for the *{HELPABLE[module].__mod_name__}* module:\n{HELPABLE[module].__help__}" send_help(chat.id, text, InlineKeyboardMarkup([[InlineKeyboardButton(text="Back", callback_data="help_back")]])) else: @@ -266,7 +299,9 @@ def send_settings(chat_id, user_id, user=False): if user: if USER_SETTINGS: settings = "\n\n".join( - "*{}*:\n{}".format(mod.__mod_name__, mod.__user_settings__(user_id)) for mod in USER_SETTINGS.values()) + f"*{mod.__mod_name__}*:\n{mod.__user_settings__(user_id)}" + for mod in USER_SETTINGS.values() + ) dispatcher.bot.send_message(user_id, "These are your current settings:" + "\n\n" + settings, parse_mode=ParseMode.MARKDOWN) @@ -274,18 +309,19 @@ def send_settings(chat_id, user_id, user=False): dispatcher.bot.send_message(user_id, "Seems like there aren't any user specific settings available :'(", parse_mode=ParseMode.MARKDOWN) + elif CHAT_SETTINGS: + chat_name = dispatcher.bot.getChat(chat_id).title + dispatcher.bot.send_message( + user_id, + text=f"Which module would you like to check {chat_name}'s settings for?", + reply_markup=InlineKeyboardMarkup( + paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id) + ), + ) else: - if CHAT_SETTINGS: - chat_name = dispatcher.bot.getChat(chat_id).title - dispatcher.bot.send_message(user_id, - text="Which module would you like to check {}'s settings for?".format( - chat_name), - reply_markup=InlineKeyboardMarkup( - paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id))) - else: - dispatcher.bot.send_message(user_id, "Seems like there aren't any chat settings available :'(\nSend this " - "in a group chat you're admin in to find its current settings!", - parse_mode=ParseMode.MARKDOWN) + dispatcher.bot.send_message(user_id, "Seems like there aren't any chat settings available :'(\nSend this " + "in a group chat you're admin in to find its current settings!", + parse_mode=ParseMode.MARKDOWN) @run_async @@ -298,58 +334,71 @@ def settings_button(bot: Bot, update: Update): 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_id = mod_match[1] + module = mod_match[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__(chat_id, user.id) - query.message.reply_text(text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton(text="Back", - callback_data="stngs_back({})".format(chat_id))]])) + text = f"*{escape_markdown(chat.title)}* has the following settings for the *{CHAT_SETTINGS[module].__mod_name__}* module:\n\n{CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id)}" + query.message.reply_text( + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + text="Back", + callback_data=f"stngs_back({chat_id})", + ) + ] + ] + ), + ) elif prev_match: - chat_id = prev_match.group(1) - curr_page = int(prev_match.group(2)) + chat_id = prev_match[1] + curr_page = int(prev_match[2]) chat = bot.get_chat(chat_id) - query.message.reply_text("Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.reply_text( + f"Hi there! There are quite a few settings for {chat.title} - go ahead and pick what you're interested in.", + 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_id = next_match[1] + next_page = int(next_match[2]) chat = bot.get_chat(chat_id) - query.message.reply_text("Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.reply_text( + f"Hi there! There are quite a few settings for {chat.title} - go ahead and pick what you're interested in.", + reply_markup=InlineKeyboardMarkup( + paginate_modules( + next_page + 1, CHAT_SETTINGS, "stngs", chat=chat_id + ) + ), + ) elif back_match: - chat_id = back_match.group(1) + chat_id = back_match[1] chat = bot.get_chat(chat_id) - query.message.reply_text(text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(escape_markdown(chat.title)), - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.reply_text( + text=f"Hi there! There are quite a few settings for {escape_markdown(chat.title)} - go ahead and pick what you're interested in.", + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + paginate_modules(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: + if excp.message not in [ + "Message is not modified", + "Query_id_invalid", + "Message can't be deleted", + ]: LOGGER.exception("Exception in settings buttons. %s", str(query.data)) @@ -361,35 +410,43 @@ def get_settings(bot: Bot, update: Update): args = msg.text.split(None, 1) # ONLY send settings in PM - if chat.type != chat.PRIVATE: - if is_user_admin(chat, user.id): - text = "Click here to get this chat's settings, as well as yours." - msg.reply_text(text, - reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton(text="Settings", - url="t.me/{}?start=stngs_{}".format( - bot.username, chat.id))]])) - else: - text = "Click here to check your settings." + if chat.type == chat.PRIVATE: + send_settings(chat.id, user.id, True) + elif is_user_admin(chat, user.id): + text = "Click here to get this chat's settings, as well as yours." + msg.reply_text( + text, + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + text="Settings", + url=f"t.me/{bot.username}?start=stngs_{chat.id}", + ) + ] + ] + ), + ) else: - send_settings(chat.id, user.id, True) + text = "Click here to check your settings." @run_async def donate(bot: Bot, update: Update): - user = update.effective_message.from_user chat = update.effective_chat # type: Optional[Chat] if chat.type == "private": update.effective_message.reply_text(DONATE_STRING, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True) if OWNER_ID != 254318997 and DONATION_LINK: - update.effective_message.reply_text("You can also donate to the person currently running me " - "[here]({})".format(DONATION_LINK), - parse_mode=ParseMode.MARKDOWN) + update.effective_message.reply_text( + f"You can also donate to the person currently running me [here]({DONATION_LINK})", + parse_mode=ParseMode.MARKDOWN, + ) else: + user = update.effective_message.from_user try: bot.send_message(user.id, DONATE_STRING, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True) @@ -421,12 +478,10 @@ def migrate_chats(bot: Bot, update: Update): def kcfrsct_fnc(bot: Bot, update: Update): query = update.callback_query user = update.effective_user - _match = re.match(r"rsct_(.*)_33801", query.data) - # ensure no spinny white circle - if _match: + if _match := re.match(r"rsct_(.*)_33801", query.data): try: from tg_bot.modules.sql.cust_filters_sql import get_btn_with_di - _soqka = get_btn_with_di(int(_match.group(1))) + _soqka = get_btn_with_di(int(_match[1])) query.answer( text=_soqka.url.replace("\\n", "\n").replace("\\t", "\t"), # HPFPOCWBANER: https://stackoverflow.com/a/42965750 @@ -484,5 +539,5 @@ def main(): if __name__ == '__main__': - LOGGER.info("Successfully loaded modules: " + str(ALL_MODULES)) + LOGGER.info(f"Successfully loaded modules: {str(ALL_MODULES)}") main()