Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions lynda/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 75-75 refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

IMPORTED[imported_module.__mod_name__.lower()] = imported_module
else:
raise Exception(
Expand Down Expand Up @@ -242,13 +242,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 != "Message is not modified"
and excp.message != "Query_id_invalid"
and excp.message != "Message can't be deleted"
):
Comment on lines -245 to +249
Copy link
Contributor Author

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:

  • 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)

LOGGER.exception("Exception in help buttons. %s", str(query.data))


Expand Down Expand Up @@ -379,13 +377,11 @@ def settings_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 != "Message is not modified"
and excp.message != "Query_id_invalid"
and excp.message != "Message can't be deleted"
):
Comment on lines -382 to +384
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

LOGGER.exception(
"Exception in settings buttons. %s", str(
query.data))
Expand Down
12 changes: 5 additions & 7 deletions lynda/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def promote(bot: Bot, update: Update, args: List[str]) -> str:
except Exception:
return log_message

if user_member.status == 'administrator' or user_member.status == 'creator':
if user_member.status in ['administrator', 'creator']:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function promote 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)

message.reply_text("How am I meant to promote someone that's already an admin?")
return log_message

Expand All @@ -61,11 +61,9 @@ def promote(bot: Bot, update: Update, args: List[str]) -> str:
except BadRequest as err:
if err.message == "User_not_mutual_contact":
message.reply_text("I can't promote someone who isn't in the group.")
return log_message
else:
message.reply_text("An error occured while promoting.")
return log_message

return log_message
bot.sendMessage(chat.id, f"Sucessfully promoted <b>{user_member.user.first_name or user_id}</b>!",
parse_mode=ParseMode.HTML)

Expand Down Expand Up @@ -104,7 +102,7 @@ def demote(bot: Bot, update: Update, args: List[str]) -> str:
message.reply_text("This person CREATED the chat, how would I demote them?")
return log_message

if not user_member.status == 'administrator':
if user_member.status != 'administrator':
Comment on lines -107 to +105
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function demote refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

message.reply_text("Can't demote what wasn't promoted!")
return log_message

Expand Down Expand Up @@ -163,7 +161,7 @@ def set_title(bot: Bot, update: Update, args: List[str]):
message.reply_text("This person CREATED the chat, how can i set custom title for him?")
return

if not user_member.status == 'administrator':
if user_member.status != 'administrator':
Comment on lines -166 to +164
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function set_title refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

message.reply_text("Can't set title for non-admins!\nPromote them first to set custom title!")
return

Expand Down Expand Up @@ -256,7 +254,7 @@ def invite(bot: Bot, update: Update):

if chat.username:
update.effective_message.reply_text(chat.username)
elif chat.type == chat.SUPERGROUP or chat.type == chat.CHANNEL:
elif chat.type in [chat.SUPERGROUP, chat.CHANNEL]:
Comment on lines -259 to +257
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function invite refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

bot_member = chat.get_member(bot.id)
if bot_member.can_invite_users:
invitelink = bot.exportChatInviteLink(chat.id)
Expand Down
4 changes: 2 additions & 2 deletions lynda/modules/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def formatJSON(outData):
res = list(jsonData.keys())
if "errors" in res:
msg += f"**Error** : `{jsonData['errors'][0]['message']}`"
return msg
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function formatJSON refactored with the following changes:

  • Hoist repeated code outside conditional statement (hoist-statement-from-if)

else:
jsonData = jsonData['data']['Media']
if "bannerImage" in jsonData.keys():
Expand All @@ -78,7 +77,8 @@ def formatJSON(outData):
msg += f"\n**Score** : {jsonData['averageScore']}"
msg += f"\n**Duration** : {jsonData['duration']} min"
msg += f"\n\n __{jsonData['description']}__"
return msg

return msg


@run_async
Expand Down
5 changes: 1 addition & 4 deletions lynda/modules/antiflood.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,9 @@ def set_flood(_bot: Bot, update: Update, args: List[str]) -> str:
f"<b>Admin</b>: {mention_html(user.id, user.first_name)}\n"
f"Disabled antiflood.")

return log_message
elif amount < 3:
message.reply_text(
"Antiflood has to be either 0 (disabled), or a number bigger than 3!")
return log_message

Comment on lines -107 to -112
Copy link
Contributor Author

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)

else:
sql.set_flood(chat.id, amount)
message.reply_text(
Expand All @@ -121,7 +118,7 @@ def set_flood(_bot: Bot, update: Update, args: List[str]) -> str:
f"<b>Admin</b>: {mention_html(user.id, user.first_name)}\n"
f"Set antiflood to <code>{amount}</code>.")

return log_message
return log_message
else:
message.reply_text(
"Unrecognised argument - please use a number, 'off', or 'no'.")
Expand Down
6 changes: 1 addition & 5 deletions lynda/modules/bans.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ def temp_ban(bot: Bot, update: Update, args: List[str]) -> str:
split_reason = reason.split(None, 1)

time_val = split_reason[0].lower()
if len(split_reason) > 1:
reason = split_reason[1]
else:
reason = ""

reason = split_reason[1] if len(split_reason) > 1 else ""
Copy link
Contributor Author

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)

bantime = extract_time(message, time_val)

if not bantime:
Expand Down
6 changes: 2 additions & 4 deletions lynda/modules/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def blacklist(_bot: Bot, update: Update, args: List[str]):

filter_list = base_blacklist_string

if len(args) > 0 and args[0].lower() == 'copy':
if args and args[0].lower() == 'copy':
Copy link
Contributor Author

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)

for trigger in all_blacklisted:
filter_list += f"<code>{html.escape(trigger)}</code>\n"
else:
Expand Down Expand Up @@ -150,9 +150,7 @@ def del_blacklist(_bot: Bot, update: Update):
try:
message.delete()
except BadRequest as excp:
if excp.message == "Message to delete not found":
pass
else:
if excp.message != "Message to delete not found":
Copy link
Contributor Author

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)

LOGGER.exception("Error while deleting blacklist message.")
break

Expand Down
11 changes: 4 additions & 7 deletions lynda/modules/blacklist_stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def blackliststicker(bot: Bot, update: Update, args: List[str]):

all_stickerlist = sql.get_chat_stickers(chat_id)

if len(args) > 0 and args[0].lower() == 'copy':
if args and args[0].lower() == 'copy':
Copy link
Contributor Author

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)

for trigger in all_stickerlist:
sticker_list += "<code>{}</code>\n".format(html.escape(trigger))
elif len(args) == 0:
Expand Down Expand Up @@ -257,11 +257,10 @@ def blacklist_mode(bot: Bot, update: Update, args: List[str]):
chat_name = update.effective_message.chat.title

if args:
if args[0].lower() == 'off' or args[0].lower(
) == 'nothing' or args[0].lower() == 'no':
if args[0].lower() in ['off', 'nothing', 'no']:
settypeblacklist = 'turn off'
sql.set_blacklist_strength(chat_id, 0, "0")
elif args[0].lower() == 'del' or args[0].lower() == 'delete':
elif args[0].lower() in ['del', 'delete']:
Comment on lines -260 to +263
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function blacklist_mode refactored with the following changes:

  • Simplify conditional into switch-like form (switch)
  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

settypeblacklist = 'left, the message will be deleted'
sql.set_blacklist_strength(chat_id, 1, "0")
elif args[0].lower() == 'warn':
Expand Down Expand Up @@ -446,9 +445,7 @@ def del_blackliststicker(bot: Bot, update: Update):
parse_mode="markdown")
return
except BadRequest as excp:
if excp.message == "Message to delete not found":
pass
else:
if excp.message != "Message to delete not found":
Comment on lines -449 to +448
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function del_blackliststicker refactored with the following changes:

  • Swap if/else to remove empty if body (remove-pass-body)

LOGGER.exception("Error while deleting blacklist message.")
break

Expand Down
6 changes: 1 addition & 5 deletions lynda/modules/blacklistusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ def bl_users(bot: Bot, update: Update):
users.append(f"• {mention_html(user.id, user.first_name)}")

message = "<b>Blacklisted Users</b>\n"
if not users:
message += "Noone is being ignored as of yet."
else:
message += '\n'.join(users)

message += '\n'.join(users) if users else "Noone is being ignored as of yet."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

update.effective_message.reply_text(message, parse_mode=ParseMode.HTML)


Expand Down
36 changes: 14 additions & 22 deletions lynda/modules/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
from lynda.modules.helper_funcs.chat_status import user_admin, bot_can_delete, dev_plus, connection_status
from lynda.modules.sql import cleaner_sql as sql

if ALLOW_EXCL:
CMD_STARTERS = ('/', '!')
else:
CMD_STARTERS = '/'

CMD_STARTERS = ('/', '!') if ALLOW_EXCL else '/'
Copy link
Contributor Author

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)

BLUE_TEXT_CLEAN_GROUP = 15
CommandHandlerList = (
CommandHandler,
Expand Down Expand Up @@ -43,24 +39,23 @@
def clean_blue_text_must_click(bot: Bot, update: Update):

chat = update.effective_chat
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()
Comment on lines -46 to +58
Copy link
Contributor Author

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:

  • Move assignments closer to their usage (move-assign)
  • Merge nested if conditions (merge-nested-ifs)



@run_async
Expand Down Expand Up @@ -91,10 +86,7 @@ def set_blue_text_must_click(_bot: Bot, update: Update, args: List[str]):
message.reply_text(reply)
else:
clean_status = sql.is_enabled(chat.id)
if clean_status:
clean_status = "Enabled"
else:
clean_status = "Disabled"
clean_status = "Enabled" if clean_status else "Disabled"
Comment on lines -94 to +89
Copy link
Contributor Author

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)

reply = "Bluetext cleaning for <b>{}</b> : <b>{}</b>".format(
chat.title, clean_status)
message.reply_text(reply, parse_mode=ParseMode.HTML)
Expand Down
12 changes: 6 additions & 6 deletions lynda/modules/cust_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,18 @@ def reply_filter(bot: Bot, update: Update):
disable_web_page_preview=True,
reply_markup=keyboard)
except BadRequest as excp:
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.")
Comment on lines -218 to +229
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function reply_filter refactored with the following changes:

  • Simplify conditional into switch-like form (switch)

else:
message.reply_text(
"This note could not be sent, as it is incorrectly formatted. Ask in "
Expand Down
Loading