Skip to content

Commit

Permalink
refactor: remove 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 7aba326 commit c34a10c
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 123 deletions.
53 changes: 27 additions & 26 deletions eduu/plugins/admins/bans.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ async def ban(c: Client, m: Message, strings):
target_user = await get_target_user(c, m)
reason = await get_reason_text(c, m)
check_admin = await m.chat.get_member(target_user.id)
if check_admin.status not in ADMIN_STATUSES:
await m.chat.ban_member(target_user.id)
text = strings("ban_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)
else:
if check_admin.status in ADMIN_STATUSES:
await m.reply_text(strings("i_cant_ban_admins"))
return

await m.chat.ban_member(target_user.id)
text = strings("ban_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)


@Client.on_message(filters.command("kick", PREFIXES))
Expand All @@ -39,19 +40,20 @@ async def kick(c: Client, m: Message, strings):
target_user = await get_target_user(c, m)
reason = await get_reason_text(c, m)
check_admin = await m.chat.get_member(target_user.id)
if check_admin.status not in ADMIN_STATUSES:
await m.chat.ban_member(target_user.id)
await m.chat.unban_member(target_user.id)
text = strings("kick_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)
else:
if check_admin.status in ADMIN_STATUSES:
await m.reply_text(strings("i_cant_kick_admins"))
return

await m.chat.ban_member(target_user.id)
await m.chat.unban_member(target_user.id)
text = strings("kick_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)


@Client.on_message(filters.command("unban", PREFIXES))
Expand All @@ -77,12 +79,12 @@ async def unban(c: Client, m: Message, strings):
async def tban(c: Client, m: Message, strings):
if len(m.command) == 1:
await m.reply_text(strings("error_must_specify_time").format(command=m.command[0]))
return None
return

split_time = m.text.split(None, 1)
ban_time = await extract_time(m, split_time[1])
if not ban_time:
return None
return
await m.chat.ban_member(m.reply_to_message.from_user.id, until_date=ban_time)

await m.reply_text(
Expand All @@ -92,7 +94,6 @@ async def tban(c: Client, m: Message, strings):
time=split_time[1],
)
)
return None


commands.add_command("ban", "admin")
Expand Down
29 changes: 15 additions & 14 deletions eduu/plugins/admins/mutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ async def mute(c: Client, m: Message, strings):
target_user = await get_target_user(c, m)
reason = await get_reason_text(c, m)
check_admin = await m.chat.get_member(target_user.id)
if check_admin.status not in ADMIN_STATUSES:
await m.chat.restrict_member(target_user.id, ChatPermissions(can_send_messages=False))
text = strings("mute_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)
else:
if check_admin.status in ADMIN_STATUSES:
await m.reply_text(strings("i_cant_mute_admins"))
return

await m.chat.restrict_member(target_user.id, ChatPermissions(can_send_messages=False))
text = strings("mute_success").format(
user=target_user.mention,
admin=m.from_user.mention,
)
if reason:
await m.reply_text(text + "\n" + strings("reason_string").format(reason_text=reason))
else:
await m.reply_text(text)


@Client.on_message(filters.command("unmute", PREFIXES))
Expand All @@ -55,12 +56,12 @@ async def unmute(c: Client, m: Message, strings):
async def tmute(c: Client, m: Message, strings):
if len(m.command) == 1:
await m.reply_text(strings("error_must_specify_time").format(command=m.command[0]))
return None
return

split_time = m.text.split(None, 1)
mute_time = await extract_time(m, split_time[1])
if not mute_time:
return None
return
await m.chat.restrict_member(
m.reply_to_message.from_user.id,
ChatPermissions(can_send_messages=False),
Expand All @@ -73,7 +74,7 @@ async def tmute(c: Client, m: Message, strings):
time=split_time[1],
)
)
return None
return


commands.add_command("mute", "admin")
Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/coub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
async def coub(c: Client, m: Message, strings):
if len(m.command) == 1:
await m.reply_text(strings("coub_usage"))
return None
return

text = m.text.split(maxsplit=1)[1]
r = await http.get("https://coub.com/api/v2/search/coubs", params={"q": text})
Expand Down
5 changes: 2 additions & 3 deletions eduu/plugins/gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
async def gif(c: Client, m: Message, strings):
if len(m.command) == 1:
await m.reply_text(strings("gif_usage"))
return None
return

text = m.text.split(maxsplit=1)[1]
r = await http.get(
Expand All @@ -33,11 +33,10 @@ async def gif(c: Client, m: Message, strings):
rjson = r.json()
if not rjson["results"]:
await m.reply_text(strings("no_results", context="general"))
return None
return

res = rjson["results"][0]["media"][0]["mediumgif"]["url"]
await m.reply_animation(res)
return None


commands.add_command("gif", "general")
5 changes: 2 additions & 3 deletions eduu/plugins/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
async def git(c: Client, m: Message, strings):
if len(m.command) == 1:
await m.reply_text(strings("no_username_err"), reply_to_message_id=m.id)
return None
return

text = m.text.split(maxsplit=1)[1]
req = await http.get(f"https://api.github.com/users/{text}")
res = req.json()

if not res.get("login"):
await m.reply_text(strings("not_found_user"), reply_to_message_id=m.id)
return None
return

avatar = res["avatar_url"]

Expand All @@ -42,7 +42,6 @@ async def git(c: Client, m: Message, strings):
),
reply_to_message_id=m.id,
)
return None


commands.add_command("git", "tools")
7 changes: 3 additions & 4 deletions eduu/plugins/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def ids_private(c: Client, m: Message, strings):
)
except BadRequest:
await m.reply_text(strings("user_not_found").format(user=m.command[1]))
return None
return
else:
user_data = m.from_user

Expand All @@ -37,7 +37,7 @@ async def ids_private(c: Client, m: Message, strings):
chat_type=m.chat.type,
)
)
return None
return


@Client.on_message(filters.command("id", PREFIXES) & filters.group)
Expand All @@ -50,7 +50,7 @@ async def ids(c: Client, m: Message, strings):
)
except BadRequest:
await m.reply_text(strings("user_not_found").format(user=m.command[1]))
return None
return

elif m.reply_to_message:
user_data = m.reply_to_message.from_user
Expand All @@ -73,7 +73,6 @@ async def ids(c: Client, m: Message, strings):
message_id=m.id + 1,
)
)
return None


commands.add_command("id", "tools")
3 changes: 1 addition & 2 deletions eduu/plugins/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def user_info(c: Client, m: Message, strings):
)
except BadRequest:
await m.reply_text(strings("user_not_found").format(user=m.command[1]))
return None
return

elif m.reply_to_message:
user = m.reply_to_message.from_user
Expand All @@ -51,7 +51,6 @@ async def user_info(c: Client, m: Message, strings):
text += strings("info_chat_owner")

await m.reply_text(text)
return None


commands.add_command("info", "tools")
3 changes: 1 addition & 2 deletions eduu/plugins/inline_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def inline_search(c: Client, q: InlineQuery, strings):
],
cache_time=0,
)
return None
return

articles = []
for result in results:
Expand All @@ -58,4 +58,3 @@ async def inline_search(c: Client, q: InlineQuery, strings):
)
)
await q.answer(articles, cache_time=0)
return None
3 changes: 1 addition & 2 deletions eduu/plugins/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def info_inline(c: Client, q: InlineQuery, strings):
)
]
)
return None
return
await q.answer(
[
InlineQueryResultArticle(
Expand All @@ -143,7 +143,6 @@ async def info_inline(c: Client, q: InlineQuery, strings):
)
]
)
return None


inline_commands.add_command("faces")
Expand Down
10 changes: 4 additions & 6 deletions eduu/plugins/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ async def get_ips_from_string(hostname: str) -> list[str]:
async def ip_cmd(c: Client, m: Message, strings):
if len(m.text.split()) == 1:
await m.reply_text(strings("ip_err_no_ip"))
return None
return

text = m.text.split(maxsplit=1)[1]

ips = await get_ips_from_string(text)

if not ips:
await m.reply_text(strings("ip_err_no_ips").format(domain=text))
return None
return

if len(ips) == 1:
await m.reply_text(await format_api_return(await get_api_return(ips[0]), strings))
return None
return

await m.reply_text(
strings("ip_select_ip").format(domain=text),
Expand All @@ -115,7 +115,6 @@ async def ip_cmd(c: Client, m: Message, strings):
]
),
)
return None


@Client.on_callback_query(filters.regex(r"^ip .+"))
Expand All @@ -140,7 +139,7 @@ async def ip_inline(c: Client, q: InlineQuery, strings):
)
]
)
return None
return

text = q.query.split(maxsplit=1)[1]

Expand Down Expand Up @@ -205,7 +204,6 @@ async def ip_inline(c: Client, q: InlineQuery, strings):
articles,
cache_time=0,
)
return None


commands.add_command("ip", "tools")
Expand Down
5 changes: 2 additions & 3 deletions eduu/plugins/jsondump.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def jsondump(c: Client, m: Message):
obj = obj[param]
except (IndexError, KeyError) as e:
await m.reply_text(f"{e.__class__.__name__}: {e}")
return None
return
# There is nothing to get anymore.
if obj is None:
break
Expand All @@ -44,12 +44,11 @@ async def jsondump(c: Client, m: Message):

if not as_file:
await m.reply_text(f"<code>{html.escape(obj)}</code>")
return None
return

bio = io.BytesIO(obj.encode())
bio.name = f"dump-{m.chat.id}.json"
await m.reply_document(bio)
return None


commands.add_command("jsondump", "tools")
4 changes: 1 addition & 3 deletions eduu/plugins/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ async def getbotinfo(c: Client, m: Message, strings):
),
reply_to_message_id=m.id,
)
return None


@Client.on_message(filters.reply & filters.group & filters.regex(r"(?i)^rt$"))
Expand Down Expand Up @@ -171,7 +170,7 @@ async def request_cmd(c: Client, m: Message):
await m.reply_text(
"You must specify the url, E.g.: <code>/request https://example.com</code>"
)
return None
return

text = m.text.split(maxsplit=1)[1]
url = text if re.match(r"^(https?)://", text) else f"http://{text}"
Expand All @@ -183,7 +182,6 @@ async def request_cmd(c: Client, m: Message):
)

await m.reply_text(f"<b>Headers:</b>\n{headers}")
return None


@Client.on_message(filters.command("parsebutton"))
Expand Down
3 changes: 1 addition & 2 deletions eduu/plugins/pastes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
async def paste(c: Client, m: Message, strings):
if not m.reply_to_message:
await m.reply_text(strings("reply_to_document_or_text"))
return None
return

if m.reply_to_message.document:
tfile = m.reply_to_message
Expand All @@ -32,7 +32,6 @@ async def paste(c: Client, m: Message, strings):
r = await http.post(url, json={"content": mean})
url = f"https://nekobin.com/{r.json()['result']['key']}"
await m.reply_text(url, disable_web_page_preview=True)
return None


commands.add_command("paste", "tools")
Loading

0 comments on commit c34a10c

Please sign in to comment.