Skip to content

Commit

Permalink
refactor(command.misc): improve readability and maintainability
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Jul 28, 2024
1 parent 31e78a9 commit bf571d0
Showing 1 changed file with 65 additions and 32 deletions.
97 changes: 65 additions & 32 deletions src/command/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,40 @@
from telethon.errors import RPCError

from .. import env, db
from .utils import command_gatekeeper, get_group_migration_help_msg, set_bot_commands, logger, \
parse_callback_data_with_page, get_callback_tail
from .utils import (
command_gatekeeper, get_group_migration_help_msg, set_bot_commands, logger, parse_callback_data_with_page,
get_callback_tail,
)
from ..i18n import i18n, get_commands_list
from . import inner


@command_gatekeeper(only_manager=False, ignore_tg_lang=True)
async def cmd_start(event: Union[events.NewMessage.Event, Message], *_, lang=None, **__):
async def cmd_start(
event: Union[
events.NewMessage.Event, Message,
events.ChatAction.Event,
],
*_,
lang=None,
**__,
):
if lang is None:
await cmd_lang.__wrapped__(event)
return
await cmd_or_callback_help.__wrapped__(event, lang=lang)


@command_gatekeeper(only_manager=False)
async def cmd_lang(event: Union[events.NewMessage.Event, Message],
*_,
chat_id: Optional[int] = None,
**__):
async def cmd_lang(
event: Union[
events.NewMessage.Event, Message,
events.ChatAction.Event,
],
*_,
chat_id: Optional[int] = None,
**__,
):
chat_id = chat_id or event.chat_id
callback_tail = get_callback_tail(event, chat_id)
buttons, langs = inner.utils.get_lang_buttons(callback='set_lang', tail=callback_tail)
Expand All @@ -50,10 +65,12 @@ async def cmd_lang(event: Union[events.NewMessage.Event, Message],


@command_gatekeeper(only_manager=False)
async def callback_set_lang(event: events.CallbackQuery.Event,
*_,
chat_id: Optional[int] = None,
**__): # callback data: set_lang={lang_code}
async def callback_set_lang(
event: events.CallbackQuery.Event,
*_,
chat_id: Optional[int] = None,
**__,
): # callback data: set_lang={lang_code}
chat_id = chat_id or event.chat_id
lang, _ = parse_callback_data_with_page(event.data)
welcome_msg = i18n[lang]['welcome_prompt']
Expand All @@ -67,16 +84,24 @@ async def callback_set_lang(event: events.CallbackQuery.Event,


@command_gatekeeper(only_manager=False)
async def cmd_or_callback_help(event: Union[events.NewMessage.Event, Message, events.CallbackQuery.Event],
*_,
lang: Optional[str] = None,
**__): # callback data: help; command: /help
async def cmd_or_callback_help(
event: Union[
events.NewMessage.Event, Message,
events.ChatAction.Event,
events.CallbackQuery.Event,
],
*_,
lang: Optional[str] = None,
**__,
): # callback data: help; command: /help
msg = i18n[lang]['manager_help_msg_html' if event.chat_id in env.MANAGER else 'help_msg_html']
if event.is_private:
msg += '\n\n' + i18n[lang]['usage_in_channel_or_group_prompt_html']
await event.respond(msg, parse_mode='html', link_preview=False) \
if isinstance(event, events.NewMessage.Event) or not hasattr(event, 'edit') \
else await event.edit(msg, parse_mode='html', link_preview=False)
await (
event.respond(msg, parse_mode='html', link_preview=False)
if isinstance(event, events.NewMessage.Event) or not hasattr(event, 'edit')
else event.edit(msg, parse_mode='html', link_preview=False)
)


@command_gatekeeper(only_manager=False)
Expand All @@ -85,17 +110,21 @@ async def cmd_version(event: Union[events.NewMessage.Event, Message], *_, **__):


@command_gatekeeper(only_manager=False)
async def callback_cancel(event: events.CallbackQuery.Event,
*_,
lang: Optional[str] = None,
**__): # callback data = cancel
async def callback_cancel(
event: events.CallbackQuery.Event,
*_,
lang: Optional[str] = None,
**__,
): # callback data = cancel
await event.edit(i18n[lang]['canceled_by_user'])


@command_gatekeeper(only_manager=False, allow_in_old_fashioned_groups=True)
async def callback_get_group_migration_help(event: events.CallbackQuery.Event,
*_,
**__): # callback data: get_group_migration_help={lang_code}
async def callback_get_group_migration_help(
event: events.CallbackQuery.Event,
*_,
**__,
): # callback data: get_group_migration_help={lang_code}
lang, _ = parse_callback_data_with_page(event.data)
chat = await event.get_chat()
if not isinstance(chat, types.Chat) or chat.migrated_to: # already a supergroup
Expand All @@ -113,19 +142,23 @@ async def callback_null(event: events.CallbackQuery.Event): # callback data = n


@command_gatekeeper(only_manager=False)
async def callback_del_buttons(event: events.CallbackQuery.Event,
*_,
**__): # callback data = del_buttons
async def callback_del_buttons(
event: events.CallbackQuery.Event,
*_,
**__,
): # callback data = del_buttons
msg = await event.get_message()
await event.answer(cache_time=3600)
await msg.edit(buttons=None)


@command_gatekeeper(only_manager=False, allow_in_others_private_chat=False, quiet=True)
async def inline_command_constructor(event: events.InlineQuery.Event,
*_,
lang: Optional[str] = None,
**__):
async def inline_command_constructor(
event: events.InlineQuery.Event,
*_,
lang: Optional[str] = None,
**__,
):
query: types.UpdateBotInlineQuery = event.query
builder = event.builder
text = query.query.strip()
Expand Down

0 comments on commit bf571d0

Please sign in to comment.