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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
237 changes: 120 additions & 117 deletions tg_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,126 +26,129 @@
ENV = bool(os.environ.get('ENV', False))

if ENV:
TOKEN = os.environ.get('TOKEN', None)

try:
OWNER_ID = int(os.environ.get('OWNER_ID', None))
except ValueError:
raise Exception("Your OWNER_ID env variable is not a valid integer.")

MESSAGE_DUMP = os.environ.get('MESSAGE_DUMP', None)
OWNER_USERNAME = os.environ.get("OWNER_USERNAME", None)

try:
SUDO_USERS = set(int(x) for x in os.environ.get("SUDO_USERS", "").split())
DEV_USERS = set(int(x) for x in os.environ.get("DEV_USERS", "").split())
except ValueError:
raise Exception("Your sudo or dev users list does not contain valid integers.")

try:
SUPPORT_USERS = set(int(x) for x in os.environ.get("SUPPORT_USERS", "").split())
except ValueError:
raise Exception("Your support users list does not contain valid integers.")

try:
SPAMMERS = set(int(x) for x in os.environ.get("SPAMMERS", "").split())
except ValueError:
raise Exception("Your spammers users list does not contain valid integers.")

try:
WHITELIST_USERS = set(int(x) for x in os.environ.get("WHITELIST_USERS", "").split())
except ValueError:
raise Exception("Your whitelisted users list does not contain valid integers.")

try:
TIGER_USERS = set(int(x) for x in os.environ.get("TIGER_USERS", "").split())
except ValueError:
raise Exception("Your tiger users list does not contain valid integers.")

GBAN_LOGS = os.environ.get('GBAN_LOGS', None)
WEBHOOK = bool(os.environ.get('WEBHOOK', False))
URL = os.environ.get('URL', "") # Does not contain token
PORT = int(os.environ.get('PORT', 5000))
CERT_PATH = os.environ.get("CERT_PATH")

DB_URI = os.environ.get('DATABASE_URL')
DONATION_LINK = os.environ.get('DONATION_LINK')
LOAD = os.environ.get("LOAD", "").split()
NO_LOAD = os.environ.get("NO_LOAD", "translation").split()
DEL_CMDS = bool(os.environ.get('DEL_CMDS', False))
STRICT_GBAN = bool(os.environ.get('STRICT_GBAN', False))
WORKERS = int(os.environ.get('WORKERS', 8))
BAN_STICKER = os.environ.get('BAN_STICKER', 'CAADAgADOwADPPEcAXkko5EB3YGYAg')
START_PHOTTO = os.environ.get('START_PIC', 'https://telegra.ph/file/10cf6a74f348caa0096b9.jpg').split()
ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False)
CASH_API_KEY = os.environ.get('CASH_API_KEY', None)
TIME_API_KEY = os.environ.get('TIME_API_KEY', None)
API_WEATHER = os.environ.get('API_OPENWEATHER',False)
AI_API_KEY = os.environ.get('AI_API_KEY', None)
WALL_API = os.environ.get('WALL_API', None)
STRICT_GMUTE = bool(os.environ.get('STRICT_GMUTE', False))
TOKEN = os.environ.get('TOKEN', None)

try:
OWNER_ID = int(os.environ.get('OWNER_ID', None))
except ValueError:
raise Exception("Your OWNER_ID env variable is not a valid integer.")

MESSAGE_DUMP = os.environ.get('MESSAGE_DUMP', None)
OWNER_USERNAME = os.environ.get("OWNER_USERNAME", None)

try:
SUDO_USERS = {int(x) for x in os.environ.get("SUDO_USERS", "").split()}
DEV_USERS = {int(x) for x in os.environ.get("DEV_USERS", "").split()}
except ValueError:
raise Exception("Your sudo or dev users list does not contain valid integers.")

try:
SUPPORT_USERS = {int(x) for x in os.environ.get("SUPPORT_USERS", "").split()}
except ValueError:
raise Exception("Your support users list does not contain valid integers.")

try:
SPAMMERS = {int(x) for x in os.environ.get("SPAMMERS", "").split()}
except ValueError:
raise Exception("Your spammers users list does not contain valid integers.")

try:
WHITELIST_USERS = {
int(x)
for x in os.environ.get("WHITELIST_USERS", "").split()
}
except ValueError:
raise Exception("Your whitelisted users list does not contain valid integers.")

try:
TIGER_USERS = {int(x) for x in os.environ.get("TIGER_USERS", "").split()}
except ValueError:
raise Exception("Your tiger users list does not contain valid integers.")

GBAN_LOGS = os.environ.get('GBAN_LOGS', None)
WEBHOOK = bool(os.environ.get('WEBHOOK', False))
URL = os.environ.get('URL', "") # Does not contain token
PORT = int(os.environ.get('PORT', 5000))
CERT_PATH = os.environ.get("CERT_PATH")

DB_URI = os.environ.get('DATABASE_URL')
DONATION_LINK = os.environ.get('DONATION_LINK')
LOAD = os.environ.get("LOAD", "").split()
NO_LOAD = os.environ.get("NO_LOAD", "translation").split()
DEL_CMDS = bool(os.environ.get('DEL_CMDS', False))
STRICT_GBAN = bool(os.environ.get('STRICT_GBAN', False))
WORKERS = int(os.environ.get('WORKERS', 8))
BAN_STICKER = os.environ.get('BAN_STICKER', 'CAADAgADOwADPPEcAXkko5EB3YGYAg')
START_PHOTTO = os.environ.get('START_PIC', 'https://telegra.ph/file/10cf6a74f348caa0096b9.jpg').split()
ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False)
CASH_API_KEY = os.environ.get('CASH_API_KEY', None)
TIME_API_KEY = os.environ.get('TIME_API_KEY', None)
API_WEATHER = os.environ.get('API_OPENWEATHER',False)
AI_API_KEY = os.environ.get('AI_API_KEY', None)
WALL_API = os.environ.get('WALL_API', None)
STRICT_GMUTE = bool(os.environ.get('STRICT_GMUTE', False))


else:
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 = set(int(x) for x in Config.SUDO_USERS or [])
DEV_USERS = set(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 = 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

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

DEV_USERS.add(OWNER_ID)
SUDO_USERS.add(OWNER_ID)
try:
Expand Down
72 changes: 32 additions & 40 deletions tg_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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
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)

IMPORTED[imported_module.__mod_name__.lower()] = imported_module
else:
raise Exception("Can't have two modules with the same name! Please change one")
Expand Down Expand Up @@ -121,7 +121,7 @@ def test(bot: Bot, update: Update):
@run_async
def start(bot: Bot, update: Update, args: List[str]):
if update.effective_chat.type == "private":
if len(args) >= 1:
if args:
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:

if args[0].lower() == "help":
send_help(update.effective_chat.id, HELP_STRINGS)
elif args[0].lower() == "disasters":
Expand Down Expand Up @@ -235,13 +235,11 @@ def help_button(bot: Bot, update: Update):
bot.answer_callback_query(query.id)
query.message.delete()
except BadRequest as excp:
if excp.message == "Message is not modified":
pass
elif excp.message == "Query_id_invalid":
pass
elif excp.message == "Message can't be deleted":
pass
else:
if excp.message not in [
"Message is not modified",
"Query_id_invalid",
"Message can't be deleted",
]:
Comment on lines -238 to +242
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:

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


Expand Down Expand Up @@ -332,8 +330,6 @@ def imdb(bot: Bot, update: Update, args):
'Which one? ',
reply_markup = reply_markup
)
else:
pass
Comment on lines -335 to -336
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:



def send_settings(chat_id, user_id, user=False):
Expand All @@ -348,18 +344,17 @@ def send_settings(chat_id, user_id, user=False):
dispatcher.bot.send_message(user_id, "Seems like there aren't any user specific settings available :'(",
parse_mode=ParseMode.MARKDOWN)

elif CHAT_SETTINGS:
chat_name = dispatcher.bot.getChat(chat_id).title
dispatcher.bot.send_message(user_id,
text="Which module would you like to check {}'s settings for?".format(
chat_name),
reply_markup=InlineKeyboardMarkup(
paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id)))
else:
if CHAT_SETTINGS:
chat_name = dispatcher.bot.getChat(chat_id).title
dispatcher.bot.send_message(user_id,
text="Which module would you like to check {}'s settings for?".format(
chat_name),
reply_markup=InlineKeyboardMarkup(
paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id)))
else:
dispatcher.bot.send_message(user_id, "Seems like there aren't any chat settings available :'(\nSend this "
"in a group chat you're admin in to find its current settings!",
parse_mode=ParseMode.MARKDOWN)
dispatcher.bot.send_message(user_id, "Seems like there aren't any chat settings available :'(\nSend this "
"in a group chat you're admin in to find its current settings!",
parse_mode=ParseMode.MARKDOWN)
Comment on lines +347 to +357
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.send_settings refactored with the following changes:



@run_async
Expand Down Expand Up @@ -417,13 +412,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 not in [
"Message is not modified",
"Query_id_invalid",
"Message can't be deleted",
]:
Comment on lines -420 to +419
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.settings_button refactored with the following changes:

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


Expand All @@ -435,19 +428,18 @@ def get_settings(bot: Bot, update: Update):
args = msg.text.split(None, 1)

# ONLY send settings in PM
if chat.type != chat.PRIVATE:
if is_user_admin(chat, user.id):
text = "Click here to get this chat's settings, as well as yours."
msg.reply_text(text,
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton(text="Settings",
url="t.me/{}?start=stngs_{}".format(
bot.username, chat.id))]]))
else:
text = "Click here to check your settings."
if chat.type == chat.PRIVATE:
send_settings(chat.id, user.id, True)

elif is_user_admin(chat, user.id):
text = "Click here to get this chat's settings, as well as yours."
msg.reply_text(text,
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton(text="Settings",
url="t.me/{}?start=stngs_{}".format(
bot.username, chat.id))]]))
else:
send_settings(chat.id, user.id, True)
text = "Click here to check your settings."
Comment on lines -438 to +442
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.settings_button.get_settings refactored with the following changes:




Expand Down