Skip to content
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

Merged
merged 1 commit into from Dec 13, 2021
Merged

Sourcery refactored main branch #6

merged 1 commit into from Dec 13, 2021

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Dec 4, 2021

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:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from PANDITHAN December 4, 2021 10:43
Copy link
Author

@sourcery-ai sourcery-ai bot left a 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 '/'
Copy link
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:

Comment on lines -35 to +44
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()
Copy link
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:

Comment on lines -61 to +63
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"]:
Copy link
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:

Comment on lines -93 to +85
if len(args) >= 1:
if args:
Copy link
Author

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:

Comment on lines -114 to +106
if len(args) >= 1:
if args:
Copy link
Author

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:

tg_bot/modules/cleaner.py Show resolved Hide resolved
tg_bot/modules/cleaner.py Show resolved Hide resolved
tg_bot/modules/combot_antispam_system.py Show resolved Hide resolved
Comment on lines -60 to +68
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
Copy link
Author

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:

tg_bot/modules/combot_antispam_system.py Show resolved Hide resolved
Comment on lines -234 to +241
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
Copy link
Author

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:

Comment on lines -25 to +45
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)
Copy link
Author

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:

Comment on lines +58 to -60
elif msg.chat.type != "private":
return
else:
if msg.chat.type != "private":
return
Copy link
Author

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:

if len(args) >= 1:
if args:
Copy link
Author

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:

Comment on lines -193 to +209
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!")
Copy link
Author

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:

Comment on lines -139 to +133
if not leave:
return muted_chats
else:
if leave:
Copy link
Author

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:

Comment on lines -160 to 154
[InlineKeyboardButton("Leave chats", callback_data=f"db_leave_chat")]
[InlineKeyboardButton("Leave chats", callback_data='db_leave_chat')]
]

Copy link
Author

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:

Comment on lines -179 to +187
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)
Copy link
Author

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:

Comment on lines -21 to +27
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
Copy link
Author

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:

Comment on lines -15 to +23
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)
Copy link
Author

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:

Copy link
Author

@sourcery-ai sourcery-ai bot left a 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.

Comment on lines -29 to +151
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

Copy link
Author

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:

if not imported_module.__mod_name__.lower() in IMPORTED:
if imported_module.__mod_name__.lower() not in IMPORTED:
Copy link
Author

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:
Copy link
Author

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:

Comment on lines -238 to +242
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",
]:
Copy link
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:

Comment on lines -335 to -336
else:
pass
Copy link
Author

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:

Comment on lines -78 to +82
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"]:
Copy link
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:

Comment on lines -39 to +43
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
Copy link
Author

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:

Comment on lines -103 to +106
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
Copy link
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:

Comment on lines -185 to +183
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
Copy link
Author

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:

Comment on lines -256 to +253
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
Copy link
Author

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:

if len(args) > 0 and args[0].lower() == 'copy':
if args and args[0].lower() == 'copy':
Copy link
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:

Comment on lines -66 to +73
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()
}
)

Copy link
Author

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:

Comment on lines -93 to +107
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()
}
)

Copy link
Author

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:

Comment on lines -140 to +154
if excp.message == "Message to delete not found":
pass
else:
if excp.message != "Message to delete not found":
Copy link
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:

Comment on lines -32 to +33


Copy link
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:

Comment on lines -134 to +126
if len(args) >= 1:
if args:
Copy link
Author

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:

Comment on lines -154 to +146
if len(args) >= 1:
if args:
Copy link
Author

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:

Comment on lines -38 to +47
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
Copy link
Author

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:

Comment on lines -60 to +68
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
Copy link
Author

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:

Comment on lines -123 to +121
elif user_id and int(user_id) == 777000:
elif user_id:
Copy link
Author

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:

Comment on lines -234 to +241
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
Copy link
Author

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:

Comment on lines -25 to +45
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)
Copy link
Author

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:

Comment on lines +58 to -60
elif msg.chat.type != "private":
return
else:
if msg.chat.type != "private":
return
Copy link
Author

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:

if len(args) >= 1:
if args:
Copy link
Author

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:

Comment on lines -193 to +209
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!")
Copy link
Author

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:

Comment on lines -139 to +133
if not leave:
return muted_chats
else:
if leave:
Copy link
Author

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:

Comment on lines -160 to 154
[InlineKeyboardButton("Leave chats", callback_data=f"db_leave_chat")]
[InlineKeyboardButton("Leave chats", callback_data='db_leave_chat')]
]

Copy link
Author

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:

Comment on lines -179 to +187
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)
Copy link
Author

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:

Comment on lines -21 to +27
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
Copy link
Author

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:

Comment on lines -15 to +23
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)
Copy link
Author

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:

@PANDITHAN PANDITHAN merged commit 31a9410 into main Dec 13, 2021
@sourcery-ai
Copy link
Author

sourcery-ai bot commented Dec 13, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.69%.

Quality metrics Before After Change
Complexity 16.63 🙂 15.87 🙂 -0.76 👍
Method Length 86.92 🙂 85.96 🙂 -0.96 👍
Working memory 12.64 😞 12.45 😞 -0.19 👍
Quality 51.10% 🙂 51.79% 🙂 0.69% 👍
Other metrics Before After Change
Lines 15336 14970 -366
Changed files Quality Before Quality After Quality Change
tg_bot/init.py 7.39% ⛔ 7.40% ⛔ 0.01% 👍
tg_bot/main.py 51.82% 🙂 53.04% 🙂 1.22% 👍
tg_bot/modules/admin.py 51.20% 🙂 52.31% 🙂 1.11% 👍
tg_bot/modules/afk.py 64.67% 🙂 63.67% 🙂 -1.00% 👎
tg_bot/modules/antiflood.py 57.95% 🙂 59.71% 🙂 1.76% 👍
tg_bot/modules/bans.py 45.30% 😞 46.38% 😞 1.08% 👍
tg_bot/modules/blacklist.py 57.24% 🙂 57.81% 🙂 0.57% 👍
tg_bot/modules/blacklist2.py 28.26% 😞 29.28% 😞 1.02% 👍
tg_bot/modules/blacklistusers.py 67.96% 🙂 68.94% 🙂 0.98% 👍
tg_bot/modules/cleaner.py 64.30% 🙂 68.52% 🙂 4.22% 👍
tg_bot/modules/combot_antispam_system.py 64.41% 🙂 66.20% 🙂 1.79% 👍
tg_bot/modules/connection.py 34.42% 😞 36.43% 😞 2.01% 👍
tg_bot/modules/covid.py 72.32% 🙂 72.43% 🙂 0.11% 👍
tg_bot/modules/covid19.py 17.03% ⛔ 18.31% ⛔ 1.28% 👍
tg_bot/modules/currency_converter.py 64.37% 🙂 64.46% 🙂 0.09% 👍
tg_bot/modules/cust_filters.py 35.95% 😞 36.22% 😞 0.27% 👍
tg_bot/modules/dbcleanup.py 54.87% 🙂 56.00% 🙂 1.13% 👍
tg_bot/modules/devpromoter.py 59.58% 🙂 59.53% 🙂 -0.05% 👎
tg_bot/modules/dictionary.py 60.05% 🙂 63.24% 🙂 3.19% 👍
tg_bot/modules/disable.py 65.10% 🙂 65.53% 🙂 0.43% 👍
tg_bot/modules/disasters.py 57.28% 🙂 57.24% 🙂 -0.04% 👎
tg_bot/modules/dogbin.py 59.48% 🙂 60.44% 🙂 0.96% 👍
tg_bot/modules/feds.py 35.25% 😞 35.34% 😞 0.09% 👍
tg_bot/modules/gettime.py 60.34% 🙂 61.69% 🙂 1.35% 👍
tg_bot/modules/global_bans.py 52.92% 🙂 54.92% 🙂 2.00% 👍
tg_bot/modules/global_kick.py 55.66% 🙂 56.85% 🙂 1.19% 👍
tg_bot/modules/global_mutes.py 41.44% 😞 43.19% 😞 1.75% 👍
tg_bot/modules/google_reverse_search.py 33.04% 😞 33.23% 😞 0.19% 👍
tg_bot/modules/gps.py 76.00% ⭐ 76.32% ⭐ 0.32% 👍
tg_bot/modules/gtranslator.py 9.85% ⛔ 10.24% ⛔ 0.39% 👍
tg_bot/modules/locks.py 61.42% 🙂 62.86% 🙂 1.44% 👍
tg_bot/modules/log_channel.py 64.47% 🙂 65.54% 🙂 1.07% 👍
tg_bot/modules/misc.py 34.45% 😞 35.29% 😞 0.84% 👍
tg_bot/modules/modules.py 44.34% 😞 44.36% 😞 0.02% 👍
tg_bot/modules/music.py 55.77% 🙂 55.89% 🙂 0.12% 👍
tg_bot/modules/muting.py 49.11% 😞 50.51% 🙂 1.40% 👍
tg_bot/modules/notes.py 53.85% 🙂 54.20% 🙂 0.35% 👍
tg_bot/modules/ping.py 71.28% 🙂 72.21% 🙂 0.93% 👍
tg_bot/modules/remote_cmds.py 39.82% 😞 41.42% 😞 1.60% 👍
tg_bot/modules/reporting.py 36.82% 😞 38.23% 😞 1.41% 👍
tg_bot/modules/rss.py 52.66% 🙂 53.15% 🙂 0.49% 👍
tg_bot/modules/rules.py 76.43% ⭐ 76.68% ⭐ 0.25% 👍
tg_bot/modules/shout.py 83.61% ⭐ 84.61% ⭐ 1.00% 👍
tg_bot/modules/special.py 67.78% 🙂 68.10% 🙂 0.32% 👍
tg_bot/modules/stickers.py 18.79% ⛔ 18.66% ⛔ -0.13% 👎
tg_bot/modules/sudo.py 76.65% ⭐ 76.71% ⭐ 0.06% 👍
tg_bot/modules/userinfo.py 64.32% 🙂 64.34% 🙂 0.02% 👍
tg_bot/modules/users.py 74.96% 🙂 75.48% ⭐ 0.52% 👍
tg_bot/modules/warns.py 55.32% 🙂 55.41% 🙂 0.09% 👍
tg_bot/modules/weather.py 42.55% 😞 42.55% 😞 0.00%
tg_bot/modules/welcome.py 37.95% 😞 38.64% 😞 0.69% 👍
tg_bot/modules/helper_funcs/cas_api.py 95.61% ⭐ 96.71% ⭐ 1.10% 👍
tg_bot/modules/helper_funcs/chat_status.py 74.09% 🙂 74.46% 🙂 0.37% 👍
tg_bot/modules/helper_funcs/extraction.py 35.21% 😞 36.47% 😞 1.26% 👍
tg_bot/modules/helper_funcs/handlers.py 56.62% 🙂 53.53% 🙂 -3.09% 👎
tg_bot/modules/helper_funcs/misc.py 74.62% 🙂 75.22% ⭐ 0.60% 👍
tg_bot/modules/helper_funcs/msg_types.py 31.08% 😞 32.93% 😞 1.85% 👍
tg_bot/modules/helper_funcs/string_handling.py 56.23% 🙂 58.01% 🙂 1.78% 👍
tg_bot/modules/sql/antiflood_sql.py 82.87% ⭐ 83.29% ⭐ 0.42% 👍
tg_bot/modules/sql/blacklistusers_sql.py 90.67% ⭐ 90.73% ⭐ 0.06% 👍
tg_bot/modules/sql/chatbot_sql.py 88.10% ⭐ 88.33% ⭐ 0.23% 👍
tg_bot/modules/sql/cleaner_sql.py 82.07% ⭐ 82.18% ⭐ 0.11% 👍
tg_bot/modules/sql/connection_sql.py 73.53% 🙂 74.66% 🙂 1.13% 👍
tg_bot/modules/sql/feds_sql.py 75.36% ⭐ 76.88% ⭐ 1.52% 👍
tg_bot/modules/sql/rules_sql.py 91.50% ⭐ 91.64% ⭐ 0.14% 👍
tg_bot/modules/translations/Russian.py 44.68% 😞 44.68% 😞 0.00%
tg_bot/modules/translations/Ukraine.py 48.47% 😞 48.52% 😞 0.05% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
tg_bot/modules/welcome.py new_member 71 ⛔ 577 ⛔ 34 ⛔ 1.62% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tg_bot/modules/feds.py fed_import_bans 119 ⛔ 618 ⛔ 26 ⛔ 1.85% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tg_bot/modules/stickers.py kang 76 ⛔ 793 ⛔ 27 ⛔ 2.12% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tg_bot/modules/gtranslator.py totranslate 75 ⛔ 477 ⛔ 25 ⛔ 3.27% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tg_bot/modules/feds.py fed_ban_list 58 ⛔ 609 ⛔ 26 ⛔ 3.80% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant