Skip to content

Commit

Permalink
refactor: remove more dangling returns and early-return some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alissonlauffer committed Nov 15, 2023
1 parent c34a10c commit c664a0f
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 183 deletions.
1 change: 0 additions & 1 deletion eduu/plugins/admins/mutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ async def tmute(c: Client, m: Message, strings):
time=split_time[1],
)
)
return


commands.add_command("mute", "admin")
Expand Down
21 changes: 11 additions & 10 deletions eduu/plugins/admins/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
@require_admin(ChatPrivileges(can_pin_messages=True))
@use_chat_lang
async def setantichannelpin(c: Client, m: Message, strings):
if len(m.text.split()) > 1:
if m.command[1] == "on":
await toggle_antichannelpin(m.chat.id, True)
await m.reply_text(strings("antichannelpin_enabled"))
elif m.command[1] == "off":
await toggle_antichannelpin(m.chat.id, None)
await m.reply_text(strings("antichannelpin_disabled"))
else:
await m.reply_text(strings("antichannelpin_invalid_arg"))
else:
if len(m.text.split()) == 1:
check_acp = await check_if_antichannelpin(m.chat.id)
if not check_acp:
await m.reply_text(strings("antichannelpin_status_disabled"))
else:
await m.reply_text(strings("antichannelpin_status_enabled"))
return

if m.command[1] == "on":
await toggle_antichannelpin(m.chat.id, True)
await m.reply_text(strings("antichannelpin_enabled"))
elif m.command[1] == "off":
await toggle_antichannelpin(m.chat.id, None)
await m.reply_text(strings("antichannelpin_disabled"))
else:
await m.reply_text(strings("antichannelpin_invalid_arg"))


@Client.on_message(filters.linked_channel, group=-1)
Expand Down
53 changes: 28 additions & 25 deletions eduu/plugins/cmds_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from itertools import zip_longest

from pyrogram import Client, filters
from pyrogram.enums import ChatType
from pyrogram.types import (
CallbackQuery,
InlineKeyboardButton,
Expand Down Expand Up @@ -47,35 +46,39 @@ async def cmds_list(c: Client, m: CallbackQuery, strings):
await m.message.edit_text(strings("select_command_category"), reply_markup=keyboard)


@Client.on_message(filters.command(["help", "start help"]) & filters.private)
@use_chat_lang
@stop_here
async def show_private_help(c: Client, m: Message, strings):
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
*gen_categories_kb(strings),
[
InlineKeyboardButton(
strings("back_btn", context="general"),
callback_data="start_back",
)
],
]
)
await m.reply_text(strings("select_command_category"), reply_markup=keyboard)


@Client.on_message(filters.command(["help", "start help"]))
@use_chat_lang
@stop_here
async def show_help(c: Client, m: Message, strings):
if m.chat.type == ChatType.PRIVATE:
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
*gen_categories_kb(strings),
[
InlineKeyboardButton(
strings("back_btn", context="general"),
callback_data="start_back",
)
],
]
)
await m.reply_text(strings("select_command_category"), reply_markup=keyboard)
else:
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
strings("start_chat", context="start"),
url=f"https://t.me/{c.me.username}?start=help",
)
]
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
strings("start_chat", context="start"),
url=f"https://t.me/{c.me.username}?start=help",
)
]
)
await m.reply_text(strings("group_help"), reply_markup=keyboard)
]
)
await m.reply_text(strings("group_help"), reply_markup=keyboard)


@Client.on_callback_query(filters.regex("^view_category .+"))
Expand Down
47 changes: 25 additions & 22 deletions eduu/plugins/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ async def delete_filter(c: Client, m: Message, strings):
trigger = args[1].lower()
chat_id = m.chat.id
check_filter = await check_for_filters(chat_id, trigger)
if check_filter:
await rm_filter(chat_id, trigger)
await m.reply_text(strings("remove_filter_success").format(trigger=trigger), quote=True)
else:

if not check_filter:
await m.reply_text(strings("no_filter_with_name").format(trigger=trigger), quote=True)
return

await rm_filter(chat_id, trigger)
await m.reply_text(strings("remove_filter_success").format(trigger=trigger), quote=True)


@Client.on_message(filters.command("filters", PREFIXES))
Expand All @@ -115,8 +117,9 @@ async def get_all_filter(c: Client, m: Message, strings):

if not all_filters:
await m.reply_text(strings("filters_list_empty"), quote=True)
else:
await m.reply_text(reply_text, quote=True)
return

await m.reply_text(reply_text, quote=True)


@Client.on_message((filters.group | filters.private) & filters.text & filters.incoming, group=1)
Expand All @@ -126,68 +129,68 @@ async def serve_filter(c: Client, m: Message):
targeted_message = m.reply_to_message or m

all_filters = await get_all_filters(chat_id)
for filter_s in all_filters:
keyword = filter_s[1]
for filter in all_filters:
keyword = filter[1]
pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])"
if not re.search(pattern, text, flags=re.IGNORECASE):
continue

data, button = button_parser(filter_s[2])
if filter_s[4] == "text":
data, button = button_parser(filter[2])
if filter[4] == "text":
await targeted_message.reply_text(
data,
quote=True,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)
elif filter_s[4] == "photo":
elif filter[4] == "photo":
await targeted_message.reply_photo(
filter_s[3],
filter[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif filter_s[4] == "document":
elif filter[4] == "document":
await targeted_message.reply_document(
filter_s[3],
filter[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif filter_s[4] == "video":
elif filter[4] == "video":
await targeted_message.reply_video(
filter_s[3],
filter[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif filter_s[4] == "audio":
elif filter[4] == "audio":
await targeted_message.reply_audio(
filter_s[3],
filter[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif filter_s[4] == "animation":
elif filter[4] == "animation":
await targeted_message.reply_animation(
filter_s[3],
filter[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif filter_s[4] == "sticker":
elif filter[4] == "sticker":
await targeted_message.reply_sticker(
filter_s[3],
filter[3],
quote=True,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)
Expand Down
1 change: 0 additions & 1 deletion eduu/plugins/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async def ids_private(c: Client, m: Message, strings):
chat_type=m.chat.type,
)
)
return


@Client.on_message(filters.command("id", PREFIXES) & filters.group)
Expand Down
32 changes: 16 additions & 16 deletions eduu/plugins/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,68 +119,68 @@ async def serve_note(c: Client, m: Message, txt):
text = txt

all_notes = await get_all_notes(chat_id)
for note_s in all_notes:
keyword = note_s[1]
for note in all_notes:
keyword = note[1]
pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])"
if not re.search(pattern, text, flags=re.IGNORECASE):
continue

data, button = button_parser(note_s[2])
if note_s[4] == "text":
data, button = button_parser(note[2])
if note[4] == "text":
await m.reply_text(
data,
quote=True,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)
elif note_s[4] == "photo":
elif note[4] == "photo":
await m.reply_photo(
note_s[3],
note[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif note_s[4] == "document":
elif note[4] == "document":
await m.reply_document(
note_s[3],
note[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif note_s[4] == "video":
elif note[4] == "video":
await m.reply_video(
note_s[3],
note[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif note_s[4] == "audio":
elif note[4] == "audio":
await m.reply_audio(
note_s[3],
note[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif note_s[4] == "animation":
elif note[4] == "animation":
await m.reply_animation(
note_s[3],
note[3],
quote=True,
caption=data,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)

elif note_s[4] == "sticker":
elif note[4] == "sticker":
await m.reply_sticker(
note_s[3],
note[3],
quote=True,
reply_markup=InlineKeyboardMarkup(button) if len(button) != 0 else None,
)
Expand Down
61 changes: 31 additions & 30 deletions eduu/plugins/prints.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ async def prints(c: Client, m: Message, strings):
target_url = entity.url
break
else:
if m.reply_to_message:
for entity in m.reply_to_message.entities or m.reply_to_message.caption_entities:
if entity.type == MessageEntityType.URL:
if m.reply_to_message.text:
target_url = m.reply_to_message.text[
entity.offset : entity.offset + entity.length
]
else:
target_url = m.reply_to_message.caption[
entity.offset : entity.offset + entity.length
]
break
if entity.type == MessageEntityType.TEXT_LINK:
target_url = entity.url
break
else:
await m.reply_text(strings("print_usage"))
return
if not m.reply_to_message:
await m.reply_text(strings("print_usage"))
return

for entity in m.reply_to_message.entities or m.reply_to_message.caption_entities:
if entity.type == MessageEntityType.URL:
if m.reply_to_message.text:
target_url = m.reply_to_message.text[
entity.offset : entity.offset + entity.length
]
else:
target_url = m.reply_to_message.caption[
entity.offset : entity.offset + entity.length
]
break
if entity.type == MessageEntityType.TEXT_LINK:
target_url = entity.url
break
else:
await m.reply_text(strings("print_usage"))
return
Expand All @@ -62,19 +62,20 @@ async def prints(c: Client, m: Message, strings):
await sent.edit_text(f"<b>API returned an error:</b> <code>{e}</code>")
return

if response:
try:
await m.reply_photo(response)
except BaseException as e:
# if failed to send the message, it's not API's fault.
# most probably there are some other kind of problem,
# for example it failed to delete its message.
# or the bot doesn't have access to send media in the chat.
await sent.edit_text(f"Failed to send the screenshot due to: {e!s}")
else:
await sent.delete()
else:
if not response:
await m.reply_text("Couldn't get url value, most probably API is not accessible.")
return

try:
await m.reply_photo(response)
except BaseException as e:
# if failed to send the message, it's not API's fault.
# most probably there are some other kind of problem,
# for example it failed to delete its message.
# or the bot doesn't have access to send media in the chat.
await sent.edit_text(f"Failed to send the screenshot due to: {e!s}")
else:
await sent.delete()


async def screenshot_page(target_url: str) -> str:
Expand Down
Loading

0 comments on commit c664a0f

Please sign in to comment.