Conversation
| 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: |
There was a problem hiding this comment.
Lines 75-75 refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| 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 != "Message is not modified" | ||
| and excp.message != "Query_id_invalid" | ||
| and excp.message != "Message can't be deleted" | ||
| ): |
There was a problem hiding this comment.
Function help_button refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Swap if/else to remove empty if body (
remove-pass-body)
| 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 != "Message is not modified" | ||
| and excp.message != "Query_id_invalid" | ||
| and excp.message != "Message can't be deleted" | ||
| ): |
There was a problem hiding this comment.
Function settings_button refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Swap if/else to remove empty if body (
remove-pass-body)
| return log_message | ||
|
|
||
| if user_member.status == 'administrator' or user_member.status == 'creator': | ||
| if user_member.status in ['administrator', 'creator']: |
There was a problem hiding this comment.
Function promote refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| if not user_member.status == 'administrator': | ||
| if user_member.status != 'administrator': |
There was a problem hiding this comment.
Function demote refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| else: | ||
| message += '\n'.join(users) | ||
|
|
||
| message += '\n'.join(users) if users else "Noone is being ignored as of yet." |
There was a problem hiding this comment.
Function bl_users refactored with the following changes:
- Ensure first condition in if is positive (
swap-if-else-branches) - Replace if statement with if expression (
assign-if-exp)
| else: | ||
| CMD_STARTERS = '/' | ||
|
|
||
| CMD_STARTERS = ('/', '!') if ALLOW_EXCL else '/' |
There was a problem hiding this comment.
Lines 13-17 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| message = update.effective_message | ||
| if chat.get_member(bot.id).can_delete_messages and sql.is_enabled(chat.id): | ||
| message = update.effective_message | ||
|
|
||
| if chat.get_member(bot.id).can_delete_messages: | ||
| if sql.is_enabled(chat.id): | ||
| fst_word = message.text.strip().split(None, 1)[0] | ||
| fst_word = message.text.strip().split(None, 1)[0] | ||
|
|
||
| if len(fst_word) > 1 and any(fst_word.startswith(start) | ||
| for start in CMD_STARTERS): | ||
| if len(fst_word) > 1 and any(fst_word.startswith(start) | ||
| for start in CMD_STARTERS): | ||
|
|
||
| command = fst_word[1:].split('@') | ||
| chat = update.effective_chat | ||
| command = fst_word[1:].split('@') | ||
| chat = update.effective_chat | ||
|
|
||
| ignored = sql.is_command_ignored(chat.id, command[0]) | ||
| if ignored: | ||
| return | ||
| ignored = sql.is_command_ignored(chat.id, command[0]) | ||
| if ignored: | ||
| return | ||
|
|
||
| if command[0] not in command_list: | ||
| message.delete() | ||
| if command[0] not in command_list: | ||
| message.delete() |
There was a problem hiding this comment.
Function clean_blue_text_must_click refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Merge nested if conditions (
merge-nested-ifs)
| if clean_status: | ||
| clean_status = "Enabled" | ||
| else: | ||
| clean_status = "Disabled" | ||
| clean_status = "Enabled" if clean_status else "Disabled" |
There was a problem hiding this comment.
Function set_blue_text_must_click refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if excp.message == "Unsupported url protocol": | ||
| message.reply_text( | ||
| "You seem to be trying to use an unsupported url protocol. Telegram " | ||
| "doesn't support buttons for some protocols, such as tg://. Please try " | ||
| "again, or ask @Aman_Ahmed for help.") | ||
| elif excp.message == "Reply message not found": | ||
| if 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) | ||
| elif excp.message == "Unsupported url protocol": | ||
| message.reply_text( | ||
| "You seem to be trying to use an unsupported url protocol. Telegram " | ||
| "doesn't support buttons for some protocols, such as tg://. Please try " | ||
| "again, or ask @Aman_Ahmed for help.") |
There was a problem hiding this comment.
Function reply_filter refactored with the following changes:
- Simplify conditional into switch-like form (
switch)
| pass | ||
|
|
||
| try: | ||
| progress_message.delete() | ||
| except Exception as e: | ||
| print(e) | ||
| pass | ||
|
|
||
| if not remove: | ||
| return kicked_chats | ||
| else: | ||
| if remove: | ||
| for muted_chat in chat_list: | ||
| sleep(0.1) | ||
| user_sql.rem_chat(muted_chat) | ||
| return kicked_chats | ||
|
|
||
| return kicked_chats |
There was a problem hiding this comment.
Function get_invalid_chats refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Swap if/else to remove empty if body (
remove-pass-body)
| gban_sql.ungban_user(user_id) | ||
| return ungbanned_users | ||
|
|
||
| return ungbanned_users |
There was a problem hiding this comment.
Function get_invalid_gban refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Swap if/else to remove empty if body (
remove-pass-body)
| progress_bar, chat_id, progress_message.message_id) | ||
| except Exception as e: | ||
| print(e) | ||
| pass |
There was a problem hiding this comment.
Function get_muted_chats refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Swap if/else to remove empty if body (
remove-pass-body)
| if query_type == "db_leave_chat": | ||
| if query.from_user.id in admin_list: | ||
| bot.editMessageText( | ||
| "Leaving chats ...", | ||
| chat_id, | ||
| message.message_id) | ||
| chat_count = get_muted_chats(bot, update, True) | ||
| bot.sendMessage(chat_id, f"Left {chat_count} chats.") | ||
| else: | ||
| query.answer("You are not allowed to use this.") | ||
| if query_type == "db_leave_chat" and query.from_user.id in admin_list: | ||
| bot.editMessageText( | ||
| "Leaving chats ...", | ||
| chat_id, | ||
| message.message_id) | ||
| chat_count = get_muted_chats(bot, update, True) | ||
| bot.sendMessage(chat_id, f"Left {chat_count} chats.") | ||
| elif ( | ||
| query_type == "db_leave_chat" | ||
| or query_type == "db_cleanup" | ||
| and query.from_user.id not in admin_list | ||
| ): | ||
| query.answer("You are not allowed to use this.") | ||
| elif query_type == "db_cleanup": | ||
| if query.from_user.id in admin_list: | ||
| bot.editMessageText( | ||
| "Cleaning up DB ...", | ||
| chat_id, | ||
| message.message_id) | ||
| invalid_chat_count = get_invalid_chats(bot, update, True) | ||
| invalid_gban_count = get_invalid_gban(bot, update, True) | ||
| reply = "Cleaned up {} chats and {} gbanned users from db.".format( | ||
| invalid_chat_count, invalid_gban_count) | ||
| bot.sendMessage(chat_id, reply) | ||
| else: | ||
| query.answer("You are not allowed to use this.") | ||
| bot.editMessageText( | ||
| "Cleaning up DB ...", | ||
| chat_id, | ||
| message.message_id) | ||
| invalid_chat_count = get_invalid_chats(bot, update, True) | ||
| invalid_gban_count = get_invalid_gban(bot, update, True) | ||
| reply = "Cleaned up {} chats and {} gbanned users from db.".format( | ||
| invalid_chat_count, invalid_gban_count) | ||
| bot.sendMessage(chat_id, reply) |
There was a problem hiding this comment.
Function callback_button refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
| return False | ||
| else: | ||
| return True | ||
| return not sql.is_command_disabled(chat.id, self.friendly) |
There was a problem hiding this comment.
Function DisableAbleMessageHandler.check_update refactored with the following changes:
- Simplify conditional into return statement (
return-identity)
| if user.id in SUDO_USERS: | ||
| pass | ||
| else: | ||
| if user.id not in SUDO_USERS: | ||
| for admin in administrators: | ||
| status = admin.status | ||
| if status == "creator": | ||
| if str(admin.user.id) == str(user.id): | ||
| pass | ||
| else: | ||
| message.reply_text( | ||
| "Only group creators can use this command!") | ||
| return | ||
| if status == "creator" and str(admin.user.id) != str(user.id): | ||
| message.reply_text( | ||
| "Only group creators can use this command!") | ||
| return |
There was a problem hiding this comment.
Function join_fed refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs) - Swap if/else to remove empty if body (
remove-pass-body)
| if get_fedlog: | ||
| if eval(get_fedlog): | ||
| bot.send_message( | ||
| get_fedlog, "Chat *{}* has left the federation *{}*".format( | ||
| chat.title, fed_info['fname']), parse_mode="markdown") | ||
| if get_fedlog and eval(get_fedlog): | ||
| bot.send_message( | ||
| get_fedlog, "Chat *{}* has left the federation *{}*".format( | ||
| chat.title, fed_info['fname']), parse_mode="markdown") |
There was a problem hiding this comment.
Function leave_fed refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| elif not message.reply_to_message and (not args or ( | ||
| len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not message.parse_entities( | ||
| [MessageEntity.TEXT_MENTION]))): | ||
| elif ( | ||
| not message.reply_to_message | ||
| and len(args) >= 1 | ||
| and not args[0].startswith("@") | ||
| and not args[0].isdigit() | ||
| and not message.parse_entities([MessageEntity.TEXT_MENTION]) | ||
| ): |
There was a problem hiding this comment.
Function user_join_fed refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| elif not message.reply_to_message and (not args or ( | ||
| len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not message.parse_entities( | ||
| [MessageEntity.TEXT_MENTION]))): | ||
| elif ( | ||
| not message.reply_to_message | ||
| and len(args) >= 1 | ||
| and not args[0].startswith("@") | ||
| and not args[0].isdigit() | ||
| and not message.parse_entities([MessageEntity.TEXT_MENTION]) | ||
| ): |
There was a problem hiding this comment.
Function user_demote_fed refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| message = update.effective_message | ||
| if args: | ||
| fed_id = args[0] | ||
| info = sql.get_fed_info(fed_id) |
There was a problem hiding this comment.
Function fed_info refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if)
| if get_fedlog: | ||
| if int(get_fedlog) != int(chat.id): | ||
| bot.send_message(get_fedlog, | ||
| "<b>FedBan reason updated</b>" | ||
| "\n<b>Federation:</b> {}" | ||
| "\n<b>Federation Admin:</b> {}" | ||
| "\n<b>User:</b> {}" | ||
| "\n<b>User ID:</b> <code>{}</code>" | ||
| "\n<b>Reason:</b> {}".format(fed_name, | ||
| mention_html(user.id, | ||
| user.first_name), | ||
| user_target, | ||
| fban_user_id, | ||
| reason), | ||
| parse_mode="HTML") | ||
| if get_fedlog and int(get_fedlog) != int(chat.id): | ||
| bot.send_message(get_fedlog, | ||
| "<b>FedBan reason updated</b>" | ||
| "\n<b>Federation:</b> {}" | ||
| "\n<b>Federation Admin:</b> {}" | ||
| "\n<b>User:</b> {}" | ||
| "\n<b>User ID:</b> <code>{}</code>" | ||
| "\n<b>Reason:</b> {}".format(fed_name, | ||
| mention_html(user.id, | ||
| user.first_name), | ||
| user_target, | ||
| fban_user_id, | ||
| reason), | ||
| parse_mode="HTML") |
There was a problem hiding this comment.
Function fed_ban refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| if get_fedlog: | ||
| if int(get_fedlog) != int(chat.id): | ||
| bot.send_message(get_fedlog, | ||
| "<b>Un-FedBan</b>" | ||
| "\n<b>Federation:</b> {}" | ||
| "\n<b>Federation Admin:</b> {}" | ||
| "\n<b>User:</b> {}" | ||
| "\n<b>User ID:</b> <code>{}</code>".format(info['fname'], | ||
| mention_html(user.id, | ||
| user.first_name), | ||
| user_target, | ||
| fban_user_id), | ||
| parse_mode="HTML") | ||
| if get_fedlog and int(get_fedlog) != int(chat.id): | ||
| bot.send_message(get_fedlog, | ||
| "<b>Un-FedBan</b>" | ||
| "\n<b>Federation:</b> {}" | ||
| "\n<b>Federation Admin:</b> {}" | ||
| "\n<b>User:</b> {}" | ||
| "\n<b>User ID:</b> <code>{}</code>".format(info['fname'], | ||
| mention_html(user.id, | ||
| user.first_name), | ||
| user_target, | ||
| fban_user_id), | ||
| parse_mode="HTML") |
There was a problem hiding this comment.
Function unfban refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass) - Merge nested if conditions (
merge-nested-ifs)
| "*{}* has updated federation rules for fed *{}*".format( | ||
| user.first_name, | ||
| getfed['fname']), | ||
| parse_mode="markdown") |
There was a problem hiding this comment.
Function set_frules refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| if get_fedlog: | ||
| if eval(get_fedlog): | ||
| teks = "Fed *{}* has successfully imported data. {} banned.".format( | ||
| getfed['fname'], success) | ||
| if failed >= 1: | ||
| teks += " {} Failed to import.".format(failed) | ||
| bot.send_message(get_fedlog, teks, parse_mode="markdown") | ||
| if get_fedlog and eval(get_fedlog): | ||
| teks = "Fed *{}* has successfully imported data. {} banned.".format( | ||
| getfed['fname'], success) | ||
| if failed >= 1: | ||
| teks += " {} Failed to import.".format(failed) | ||
| bot.send_message(get_fedlog, teks, parse_mode="markdown") |
There was a problem hiding this comment.
Function fed_import_bans refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| if args: | ||
| if args[0].isdigit(): | ||
| user_id = args[0] | ||
| else: | ||
| user_id = extract_user(message, args) | ||
| if args and args[0].isdigit(): | ||
| user_id = args[0] | ||
| else: | ||
| user_id = extract_user(message, args) | ||
|
|
There was a problem hiding this comment.
Function fed_stat_user refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks)
| if bool(random.getrandbits(1)): | ||
| reply_text += c.upper() | ||
| else: | ||
| reply_text += c.lower() | ||
| reply_text += c.upper() if bool(random.getrandbits(1)) else c.lower() |
There was a problem hiding this comment.
Function copypasta refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if excp.message in UNGBAN_ERRORS: | ||
| pass | ||
| else: | ||
| if excp.message not in UNGBAN_ERRORS: |
There was a problem hiding this comment.
Function ungban refactored with the following changes:
- Swap if/else to remove empty if body (
remove-pass-body)
| return | ||
| else: | ||
| return | ||
| return | ||
| except Exception as e: | ||
| print(e) | ||
| pass | ||
|
|
There was a problem hiding this comment.
Function check_and_ban refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass) - Hoist repeated code outside conditional statement (
hoist-statement-from-if)
| if sql.does_chat_gban( | ||
| update.effective_chat.id) and update.effective_chat.get_member( | ||
| bot.id).can_restrict_members: | ||
| user = update.effective_user | ||
| chat = update.effective_chat | ||
| msg = update.effective_message | ||
| if ( | ||
| not sql.does_chat_gban(update.effective_chat.id) | ||
| or not update.effective_chat.get_member(bot.id).can_restrict_members | ||
| ): | ||
| return | ||
| user = update.effective_user | ||
| chat = update.effective_chat | ||
| msg = update.effective_message | ||
|
|
||
| if user and not is_user_admin(chat, user.id): | ||
| check_and_ban(update, user.id) | ||
| if user and not is_user_admin(chat, user.id): | ||
| check_and_ban(update, user.id) | ||
|
|
||
| if msg.new_chat_members: | ||
| new_members = update.effective_message.new_chat_members | ||
| for mem in new_members: | ||
| check_and_ban(update, mem.id) | ||
| if msg.new_chat_members: | ||
| new_members = update.effective_message.new_chat_members | ||
| for mem in new_members: | ||
| check_and_ban(update, mem.id) | ||
|
|
||
| if msg.reply_to_message: | ||
| user = msg.reply_to_message.from_user | ||
| if user and not is_user_admin(chat, user.id): | ||
| check_and_ban(update, user.id, should_message=False) | ||
| if msg.reply_to_message: | ||
| user = msg.reply_to_message.from_user | ||
| if user and not is_user_admin(chat, user.id): | ||
| check_and_ban(update, user.id, should_message=False) |
There was a problem hiding this comment.
Function enforce_gban refactored with the following changes:
- Add guard clause (
last-if-guard)
| if len(args) > 0: | ||
| if args: |
There was a problem hiding this comment.
Function gbanstat refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison)
Sourcery Code Quality Report (beta)✅ Merging this PR will increase code quality in the affected files by 0.05 out of 10.
Here are some functions in these files that still need a tune-up:
Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! |
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: