From bf571d02991175348ae6d7a5dc0d1dd9f326df32 Mon Sep 17 00:00:00 2001 From: Rongrong Date: Mon, 29 Jul 2024 00:21:45 +0800 Subject: [PATCH] refactor(command.misc): improve readability and maintainability Signed-off-by: Rongrong --- src/command/misc.py | 97 ++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/src/command/misc.py b/src/command/misc.py index 9d217605ae..f642e31204 100644 --- a/src/command/misc.py +++ b/src/command/misc.py @@ -23,14 +23,24 @@ 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 @@ -38,10 +48,15 @@ async def cmd_start(event: Union[events.NewMessage.Event, Message], *_, lang=Non @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) @@ -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'] @@ -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) @@ -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 @@ -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()