New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored main branch #6
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
if ALLOW_EXCL: | ||
CMD_STARTERS = ('/', '!') | ||
else: | ||
CMD_STARTERS = ('/') | ||
|
||
CMD_STARTERS = ('/', '!') if ALLOW_EXCL else '/' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 13-17
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if chat.get_member(bot.id).can_delete_messages: | ||
if sql.is_enabled(chat.id): | ||
fst_word = message.text.strip().split(None, 1)[0] | ||
if chat.get_member(bot.id).can_delete_messages and sql.is_enabled(chat.id): | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function clean_blue_text_must_click
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
)
if len(args) >= 1: | ||
if args: | ||
val = args[0].lower() | ||
if val == "off" or val == "no": | ||
if val in ["off", "no"]: | ||
sql.set_cleanbt(chat.id, False) | ||
reply = "Bluetext cleaning has been disabled for <b>{}</b>".format(html.escape(chat.title)) | ||
message.reply_text(reply, parse_mode=ParseMode.HTML) | ||
|
||
elif val == "yes" or val == "on": | ||
elif val in ["yes", "on"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function set_blue_text_must_click
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
) - Simplify sequence comparison (
simplify-len-comparison
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function add_bluetext_ignore
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function remove_bluetext_ignore
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
if param == "on" or param == "true": | ||
if param in ["on", "true"]: | ||
sql.set_cas_autoban(chat.id, True) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
elif param == "off" or param == "false": | ||
elif param in ["off", "false"]: | ||
sql.set_cas_autoban(chat.id, False) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
else: | ||
msg.reply_text("Invalid autoban definition to set!") #on or off ffs | ||
return | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function setban
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if param == "on" or param == "true": | ||
if param in ["on", "true"]: | ||
sql.setDefenseStatus(chat.id, True) | ||
msg.reply_text("Defense mode has been turned on, this group is under attack. Every user that now joins will be auto kicked.") | ||
return | ||
elif param == "off" or param == "false": | ||
elif param in ["off", "false"]: | ||
sql.setDefenseStatus(chat.id, False) | ||
msg.reply_text("Defense mode has been turned off, group is no longer under attack.") | ||
return | ||
else: | ||
msg.reply_text("Invalid status to set!") #on or off ffs | ||
return | ||
msg.reply_text("Invalid status to set!") #on or off ffs | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function setDefense
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if chat.type != chat.PRIVATE: | ||
if len(args) >= 1: | ||
var = args[0] | ||
if var == "no": | ||
sql.set_allow_connect_to_chat(chat.id, False) | ||
send_message(update.effective_message, "Connection has been disabled for this chat") | ||
elif var == "yes": | ||
sql.set_allow_connect_to_chat(chat.id, True) | ||
send_message(update.effective_message, "Connection has been enabled for this chat") | ||
else: | ||
send_message(update.effective_message, "Please enter `yes` or `no`!", parse_mode=ParseMode.MARKDOWN) | ||
if chat.type == chat.PRIVATE: | ||
send_message(update.effective_message, "This command is for group only. Not in PM!") | ||
|
||
elif args: | ||
var = args[0] | ||
if var == "no": | ||
sql.set_allow_connect_to_chat(chat.id, False) | ||
send_message(update.effective_message, "Connection has been disabled for this chat") | ||
elif var == "yes": | ||
sql.set_allow_connect_to_chat(chat.id, True) | ||
send_message(update.effective_message, "Connection has been enabled for this chat") | ||
else: | ||
get_settings = sql.allow_connect_to_chat(chat.id) | ||
if get_settings: | ||
send_message(update.effective_message, "Connections to this group are *Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "Connection to this group are *Not Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
send_message(update.effective_message, "Please enter `yes` or `no`!", parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "This command is for group only. Not in PM!") | ||
get_settings = sql.allow_connect_to_chat(chat.id) | ||
if get_settings: | ||
send_message(update.effective_message, "Connections to this group are *Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "Connection to this group are *Not Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function allow_connections
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
) - Swap if/else branches (
swap-if-else-branches
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
elif msg.chat.type != "private": | ||
return | ||
else: | ||
if msg.chat.type != "private": | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connection_chat
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connect_chat
refactored with the following changes:
- Merge except handlers with identical code blocks (
merge-except-handler
) - Simplify sequence comparison (
simplify-len-comparison
)
if chat.type == chat.PRIVATE and sql.get_connected_chat(user_id): | ||
|
||
conn_id = sql.get_connected_chat(user_id).chat_id | ||
getstatusadmin = bot.get_chat_member(conn_id, msg.from_user.id) | ||
isadmin = getstatusadmin.status in ADMIN_STATUS | ||
ismember = getstatusadmin.status in MEMBER_STAUS | ||
isallow = sql.allow_connect_to_chat(conn_id) | ||
|
||
if isadmin or (isallow and ismember) or (user.id in SUDO_USERS) or (user.id in DEV_USERS): | ||
if need_admin is True: | ||
if getstatusadmin.status in ADMIN_STATUS or user_id in SUDO_USERS or user.id in DEV_USERS: | ||
return conn_id | ||
else: | ||
send_message(msg, "You must be an admin in the connected group!") | ||
raise Exception("Not admin!") | ||
else: | ||
return conn_id | ||
else: | ||
send_message(msg, "The group changed the connection rights or you are no longer an admin.\n" | ||
"I've disconnected you.") | ||
disconnect_chat(bot, update) | ||
raise Exception("Not admin!") | ||
else: | ||
if chat.type != chat.PRIVATE or not sql.get_connected_chat(user_id): | ||
return False | ||
conn_id = sql.get_connected_chat(user_id).chat_id | ||
getstatusadmin = bot.get_chat_member(conn_id, msg.from_user.id) | ||
isadmin = getstatusadmin.status in ADMIN_STATUS | ||
ismember = getstatusadmin.status in MEMBER_STAUS | ||
isallow = sql.allow_connect_to_chat(conn_id) | ||
|
||
if isadmin or (isallow and ismember) or (user.id in SUDO_USERS) or (user.id in DEV_USERS): | ||
if need_admin is not True: | ||
return conn_id | ||
if getstatusadmin.status in ADMIN_STATUS or user_id in SUDO_USERS or user.id in DEV_USERS: | ||
return conn_id | ||
send_message(msg, "You must be an admin in the connected group!") | ||
else: | ||
send_message(msg, "The group changed the connection rights or you are no longer an admin.\n" | ||
"I've disconnected you.") | ||
disconnect_chat(bot, update) | ||
raise Exception("Not admin!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connected
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
if not leave: | ||
return muted_chats | ||
else: | ||
if leave: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_muted_chats
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Swap if/else to remove empty if body (
remove-pass-body
)
[InlineKeyboardButton("Leave chats", callback_data=f"db_leave_chat")] | ||
[InlineKeyboardButton("Leave chats", callback_data='db_leave_chat')] | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function leave_muted_chats
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function callback_button
refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
reply = "That...is a chat!" | ||
return "That...is a chat!" | ||
|
||
elif user_id == bot.id: | ||
reply = "This does not work that way." | ||
return "This does not work that way." | ||
|
||
else: | ||
reply = None | ||
return reply | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function check_user_id
refactored with the following changes:
- Lift return into if (
lift-return-into-if
)
if info: | ||
meaning = "" | ||
for count, (key, value) in enumerate(info.items(), start=1): | ||
meaning += f"<b>{count}. {word}</b> <i>({key})</i>\n" | ||
for i in value: | ||
defs = i.get("definition") | ||
meaning += f"• <i>{defs}</i>\n" | ||
msg.reply_text(meaning, parse_mode=ParseMode.HTML) | ||
else: | ||
return | ||
if not info: | ||
return | ||
meaning = "" | ||
for count, (key, value) in enumerate(info.items(), start=1): | ||
meaning += f"<b>{count}. {word}</b> <i>({key})</i>\n" | ||
for i in value: | ||
defs = i.get("definition") | ||
meaning += f"• <i>{defs}</i>\n" | ||
msg.reply_text(meaning, parse_mode=ParseMode.HTML) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function define
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
f744821
to
c60d395
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
SUPPORT_USERS = set(int(x) for x in Config.SUPPORT_USERS or []) | ||
except ValueError: | ||
raise Exception("Your support users list does not contain valid integers.") | ||
|
||
try: | ||
SPAMMERS = set(int(x) for x in Config.SPAMMERS or []) | ||
except ValueError: | ||
raise Exception("Your spammers users list does not contain valid integers.") | ||
|
||
try: | ||
WHITELIST_USERS = set(int(x) for x in Config.WHITELIST_USERS or []) | ||
except ValueError: | ||
raise Exception("Your whitelisted users list does not contain valid integers.") | ||
|
||
try: | ||
TIGER_USERS = set(int(x) for x in Config.TIGER_USERS or []) | ||
except ValueError: | ||
raise Exception("Your tiger users list does not contain valid integers.") | ||
|
||
GBAN_LOGS = Config.GBAN_LOGS | ||
WEBHOOK = Config.WEBHOOK | ||
URL = Config.URL | ||
PORT = Config.PORT | ||
CERT_PATH = Config.CERT_PATH | ||
|
||
DB_URI = Config.SQLALCHEMY_DATABASE_URI | ||
DONATION_LINK = Config.DONATION_LINK | ||
LOAD = Config.LOAD | ||
NO_LOAD = Config.NO_LOAD | ||
DEL_CMDS = Config.DEL_CMDS | ||
STRICT_GBAN = Config.STRICT_GBAN | ||
WORKERS = Config.WORKERS | ||
BAN_STICKER = Config.BAN_STICKER | ||
ALLOW_EXCL = Config.ALLOW_EXCL | ||
CASH_API_KEY = Config.CASH_API_KEY | ||
TIME_API_KEY = Config.TIME_API_KEY | ||
API_OPENWEATHER = Config.API_OPENWEATHER | ||
AI_API_KEY = Config.AI_API_KEY | ||
WALL_API = Config.WALL_API | ||
STRICT_GMUTE = Config.STRICT_GMUTE | ||
from tg_bot.config import Development as Config | ||
TOKEN = Config.API_KEY | ||
|
||
try: | ||
OWNER_ID = int(Config.OWNER_ID) | ||
except ValueError: | ||
raise Exception("Your OWNER_ID variable is not a valid integer.") | ||
|
||
MESSAGE_DUMP = Config.MESSAGE_DUMP | ||
OWNER_USERNAME = Config.OWNER_USERNAME | ||
|
||
try: | ||
SUDO_USERS = {int(x) for x in Config.SUDO_USERS or []} | ||
DEV_USERS = {int(x) for x in Config.DEV_USERS or []} | ||
except ValueError: | ||
raise Exception("Your sudo or dev users list does not contain valid integers.") | ||
|
||
try: | ||
SUPPORT_USERS = {int(x) for x in Config.SUPPORT_USERS or []} | ||
except ValueError: | ||
raise Exception("Your support users list does not contain valid integers.") | ||
|
||
try: | ||
SPAMMERS = {int(x) for x in Config.SPAMMERS or []} | ||
except ValueError: | ||
raise Exception("Your spammers users list does not contain valid integers.") | ||
|
||
try: | ||
WHITELIST_USERS = {int(x) for x in Config.WHITELIST_USERS or []} | ||
except ValueError: | ||
raise Exception("Your whitelisted users list does not contain valid integers.") | ||
|
||
try: | ||
TIGER_USERS = {int(x) for x in Config.TIGER_USERS or []} | ||
except ValueError: | ||
raise Exception("Your tiger users list does not contain valid integers.") | ||
|
||
GBAN_LOGS = Config.GBAN_LOGS | ||
WEBHOOK = Config.WEBHOOK | ||
URL = Config.URL | ||
PORT = Config.PORT | ||
CERT_PATH = Config.CERT_PATH | ||
|
||
DB_URI = Config.SQLALCHEMY_DATABASE_URI | ||
DONATION_LINK = Config.DONATION_LINK | ||
LOAD = Config.LOAD | ||
NO_LOAD = Config.NO_LOAD | ||
DEL_CMDS = Config.DEL_CMDS | ||
STRICT_GBAN = Config.STRICT_GBAN | ||
WORKERS = Config.WORKERS | ||
BAN_STICKER = Config.BAN_STICKER | ||
ALLOW_EXCL = Config.ALLOW_EXCL | ||
CASH_API_KEY = Config.CASH_API_KEY | ||
TIME_API_KEY = Config.TIME_API_KEY | ||
API_OPENWEATHER = Config.API_OPENWEATHER | ||
AI_API_KEY = Config.AI_API_KEY | ||
WALL_API = Config.WALL_API | ||
STRICT_GMUTE = Config.STRICT_GMUTE | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 29-148
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 73-73
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function start
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
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", | ||
]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function help_button
refactored with the following changes:
- Simplify conditional into switch-like form (
switch
) - Swap if/else to remove empty if body (
remove-pass-body
)
else: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function help_button.get_help.imdb
refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass
)
if len(args) >= 1: | ||
if args: | ||
|
||
val = args[0].lower() | ||
|
||
if val == "off" or val == "no" or val == "0": | ||
if val in ["off", "no", "0"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function set_flood
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
) - Simplify sequence comparison (
simplify-len-comparison
)
if excp.message == "User not found": | ||
message.reply_text("Can't seem to find this person.") | ||
return log_message | ||
else: | ||
if excp.message != "User not found": | ||
raise | ||
|
||
message.reply_text("Can't seem to find this person.") | ||
return log_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ban
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if excp.message == "User not found": | ||
message.reply_text("I can't seem to find this user.") | ||
return log_message | ||
else: | ||
if excp.message != "User not found": | ||
raise | ||
|
||
message.reply_text("I can't seem to find this user.") | ||
return log_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function temp_ban
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if excp.message == "User not found": | ||
message.reply_text("I can't seem to find this user.") | ||
return log_message | ||
else: | ||
if excp.message != "User not found": | ||
raise | ||
|
||
message.reply_text("I can't seem to find this user.") | ||
return log_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function punch
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if excp.message == "User not found": | ||
message.reply_text("I can't seem to find this user.") | ||
return log_message | ||
else: | ||
if excp.message != "User not found": | ||
raise | ||
|
||
message.reply_text("I can't seem to find this user.") | ||
return log_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function unban
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if len(args) > 0 and args[0].lower() == 'copy': | ||
if args and args[0].lower() == 'copy': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function blacklist
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) | ||
to_blacklist = list( | ||
{ | ||
trigger.strip() | ||
for trigger in text.split("\n") | ||
if trigger.strip() | ||
} | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function add_blacklist
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
to_unblacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) | ||
to_unblacklist = list( | ||
{ | ||
trigger.strip() | ||
for trigger in text.split("\n") | ||
if trigger.strip() | ||
} | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function unblacklist
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
if excp.message == "Message to delete not found": | ||
pass | ||
else: | ||
if excp.message != "Message to delete not found": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function del_blacklist
refactored with the following changes:
- Swap if/else to remove empty if body (
remove-pass-body
)
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function blackliststicker
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function add_bluetext_ignore_global
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function remove_bluetext_ignore_global
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
)
if param == "on" or param == "true": | ||
if param in ["on", "true"]: | ||
sql.set_cas_status(chat.id, True) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
elif param == "off" or param == "false": | ||
elif param in ["off", "false"]: | ||
sql.set_cas_status(chat.id, False) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
else: | ||
msg.reply_text("Invalid status to set!") #on or off ffs | ||
return | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function setcas
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if param == "on" or param == "true": | ||
if param in ["on", "true"]: | ||
sql.set_cas_autoban(chat.id, True) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
elif param == "off" or param == "false": | ||
elif param in ["off", "false"]: | ||
sql.set_cas_autoban(chat.id, False) | ||
msg.reply_text("Successfully updated configuration.") | ||
return | ||
else: | ||
msg.reply_text("Invalid autoban definition to set!") #on or off ffs | ||
return | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function setban
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
elif user_id and int(user_id) == 777000: | ||
elif user_id: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function caschecker
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
)
if param == "on" or param == "true": | ||
if param in ["on", "true"]: | ||
sql.setDefenseStatus(chat.id, True) | ||
msg.reply_text("Defense mode has been turned on, this group is under attack. Every user that now joins will be auto kicked.") | ||
return | ||
elif param == "off" or param == "false": | ||
elif param in ["off", "false"]: | ||
sql.setDefenseStatus(chat.id, False) | ||
msg.reply_text("Defense mode has been turned off, group is no longer under attack.") | ||
return | ||
else: | ||
msg.reply_text("Invalid status to set!") #on or off ffs | ||
return | ||
msg.reply_text("Invalid status to set!") #on or off ffs | ||
|
||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function setDefense
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if chat.type != chat.PRIVATE: | ||
if len(args) >= 1: | ||
var = args[0] | ||
if var == "no": | ||
sql.set_allow_connect_to_chat(chat.id, False) | ||
send_message(update.effective_message, "Connection has been disabled for this chat") | ||
elif var == "yes": | ||
sql.set_allow_connect_to_chat(chat.id, True) | ||
send_message(update.effective_message, "Connection has been enabled for this chat") | ||
else: | ||
send_message(update.effective_message, "Please enter `yes` or `no`!", parse_mode=ParseMode.MARKDOWN) | ||
if chat.type == chat.PRIVATE: | ||
send_message(update.effective_message, "This command is for group only. Not in PM!") | ||
|
||
elif args: | ||
var = args[0] | ||
if var == "no": | ||
sql.set_allow_connect_to_chat(chat.id, False) | ||
send_message(update.effective_message, "Connection has been disabled for this chat") | ||
elif var == "yes": | ||
sql.set_allow_connect_to_chat(chat.id, True) | ||
send_message(update.effective_message, "Connection has been enabled for this chat") | ||
else: | ||
get_settings = sql.allow_connect_to_chat(chat.id) | ||
if get_settings: | ||
send_message(update.effective_message, "Connections to this group are *Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "Connection to this group are *Not Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
send_message(update.effective_message, "Please enter `yes` or `no`!", parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "This command is for group only. Not in PM!") | ||
get_settings = sql.allow_connect_to_chat(chat.id) | ||
if get_settings: | ||
send_message(update.effective_message, "Connections to this group are *Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
send_message(update.effective_message, "Connection to this group are *Not Allowed* for members!", | ||
parse_mode=ParseMode.MARKDOWN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function allow_connections
refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison
) - Swap if/else branches (
swap-if-else-branches
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
elif msg.chat.type != "private": | ||
return | ||
else: | ||
if msg.chat.type != "private": | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connection_chat
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
if len(args) >= 1: | ||
if args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connect_chat
refactored with the following changes:
- Merge except handlers with identical code blocks (
merge-except-handler
) - Simplify sequence comparison (
simplify-len-comparison
)
if chat.type == chat.PRIVATE and sql.get_connected_chat(user_id): | ||
|
||
conn_id = sql.get_connected_chat(user_id).chat_id | ||
getstatusadmin = bot.get_chat_member(conn_id, msg.from_user.id) | ||
isadmin = getstatusadmin.status in ADMIN_STATUS | ||
ismember = getstatusadmin.status in MEMBER_STAUS | ||
isallow = sql.allow_connect_to_chat(conn_id) | ||
|
||
if isadmin or (isallow and ismember) or (user.id in SUDO_USERS) or (user.id in DEV_USERS): | ||
if need_admin is True: | ||
if getstatusadmin.status in ADMIN_STATUS or user_id in SUDO_USERS or user.id in DEV_USERS: | ||
return conn_id | ||
else: | ||
send_message(msg, "You must be an admin in the connected group!") | ||
raise Exception("Not admin!") | ||
else: | ||
return conn_id | ||
else: | ||
send_message(msg, "The group changed the connection rights or you are no longer an admin.\n" | ||
"I've disconnected you.") | ||
disconnect_chat(bot, update) | ||
raise Exception("Not admin!") | ||
else: | ||
if chat.type != chat.PRIVATE or not sql.get_connected_chat(user_id): | ||
return False | ||
conn_id = sql.get_connected_chat(user_id).chat_id | ||
getstatusadmin = bot.get_chat_member(conn_id, msg.from_user.id) | ||
isadmin = getstatusadmin.status in ADMIN_STATUS | ||
ismember = getstatusadmin.status in MEMBER_STAUS | ||
isallow = sql.allow_connect_to_chat(conn_id) | ||
|
||
if isadmin or (isallow and ismember) or (user.id in SUDO_USERS) or (user.id in DEV_USERS): | ||
if need_admin is not True: | ||
return conn_id | ||
if getstatusadmin.status in ADMIN_STATUS or user_id in SUDO_USERS or user.id in DEV_USERS: | ||
return conn_id | ||
send_message(msg, "You must be an admin in the connected group!") | ||
else: | ||
send_message(msg, "The group changed the connection rights or you are no longer an admin.\n" | ||
"I've disconnected you.") | ||
disconnect_chat(bot, update) | ||
raise Exception("Not admin!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function connected
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
if not leave: | ||
return muted_chats | ||
else: | ||
if leave: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_muted_chats
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Swap if/else to remove empty if body (
remove-pass-body
)
[InlineKeyboardButton("Leave chats", callback_data=f"db_leave_chat")] | ||
[InlineKeyboardButton("Leave chats", callback_data='db_leave_chat')] | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function leave_muted_chats
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function callback_button
refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
reply = "That...is a chat!" | ||
return "That...is a chat!" | ||
|
||
elif user_id == bot.id: | ||
reply = "This does not work that way." | ||
return "This does not work that way." | ||
|
||
else: | ||
reply = None | ||
return reply | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function check_user_id
refactored with the following changes:
- Lift return into if (
lift-return-into-if
)
if info: | ||
meaning = "" | ||
for count, (key, value) in enumerate(info.items(), start=1): | ||
meaning += f"<b>{count}. {word}</b> <i>({key})</i>\n" | ||
for i in value: | ||
defs = i.get("definition") | ||
meaning += f"• <i>{defs}</i>\n" | ||
msg.reply_text(meaning, parse_mode=ParseMode.HTML) | ||
else: | ||
return | ||
if not info: | ||
return | ||
meaning = "" | ||
for count, (key, value) in enumerate(info.items(), start=1): | ||
meaning += f"<b>{count}. {word}</b> <i>({key})</i>\n" | ||
for i in value: | ||
defs = i.get("definition") | ||
meaning += f"• <i>{defs}</i>\n" | ||
msg.reply_text(meaning, parse_mode=ParseMode.HTML) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function define
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.69%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. 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! Help us improve this quality report! |
Branch
main
refactored 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
main
branch, then run:Help us improve this pull request!