From 2b2b1abbf30caceb3ff78d4883c2d2ae1f0415f1 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:35:43 +0100 Subject: [PATCH] =?UTF-8?q?refactor(bot):=20=F0=9F=8F=B7=EF=B8=8F=20update?= =?UTF-8?q?=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot/bot/command_handler.py | 88 +++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 33 deletions(-) diff --git a/src/bot/bot/command_handler.py b/src/bot/bot/command_handler.py index b430d62..0f6784f 100644 --- a/src/bot/bot/command_handler.py +++ b/src/bot/bot/command_handler.py @@ -1,7 +1,7 @@ from enum import Enum -from typing import TYPE_CHECKING, Dict, Any, List, Set, Callable, Union +from typing import TYPE_CHECKING, List, Set, Callable -from mcdreforged.api.types import ConsoleCommandSource, PlayerCommandSource +from mcdreforged.api.types import CommandSource from mcdreforged.api.command import * from mcdreforged.api.rtext import * from mcdreforged.api.decorator import new_thread @@ -15,8 +15,6 @@ if TYPE_CHECKING: from bot.plugin import Plugin -Source = Union[ConsoleCommandSource, PlayerCommandSource] - class PermissionsRequirements(Serializable): LIST: Callable @@ -47,7 +45,7 @@ def __init__(self, plugin: 'Plugin'): } ) - def bot_list(online: bool = None) -> Callable[[bool], List[str]]: + def bot_list(online: bool = None) -> Callable[[], List[str]]: return lambda: [ name for name, bot in self.__plugin.bot_manager.bots.items() @@ -349,7 +347,7 @@ def parse_name(self, name: str) -> str: # Return return name - def __command_list(self, src: Source, ctx: Dict[str, Any]): + def __command_list(self, src: CommandSource, ctx: CommandContext): index = ctx.get('index', 0) arg = ctx.get('arg', ListArguments.ALL) try: @@ -445,7 +443,7 @@ def __command_list(self, src: Source, ctx: Dict[str, Any]): ) @new_thread('commandSpawn') - def __command_spawn(self, src: Source, ctx: Dict[str, Any]): + def __command_spawn(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.spawn( @@ -458,7 +456,7 @@ def __command_spawn(self, src: Source, ctx: Dict[str, Any]): except BotOnlineException as e: src.reply(RTextMCDRTranslation('bot.error.botOnline', e.name)) - def __command_kill(self, src: Source, ctx: Dict[str, Any]): + def __command_kill(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.kill(name) @@ -468,7 +466,7 @@ def __command_kill(self, src: Source, ctx: Dict[str, Any]): except BotOfflineException as e: src.reply(RTextMCDRTranslation('bot.error.botOffline', e.name)) - def __command_action(self, src: Source, ctx: Dict[str, Any]): + def __command_action(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.action( @@ -484,13 +482,13 @@ def __command_action(self, src: Source, ctx: Dict[str, Any]): 'bot.error.illegalActionIndex', e.index )) - def __command_tag_list(self, src: Source): + def __command_tag_list(self, src: CommandSource): src.reply(RTextMCDRTranslation( 'bot.command.tag.list', list(self.tag_list()) )) - def __command_tag_spawn(self, src: Source, ctx: Dict[str, Any]): + def __command_tag_spawn(self, src: CommandSource, ctx: CommandContext): tag = ctx['tag'] try: # check tag exists @@ -511,7 +509,7 @@ def __command_tag_spawn(self, src: Source, ctx: Dict[str, Any]): except TagNotExistsException: src.reply(RTextMCDRTranslation('bot.error.tagNotExists', tag)) - def __command_tag_kill(self, src: Source, ctx: Dict[str, Any]): + def __command_tag_kill(self, src: CommandSource, ctx: CommandContext): tag = ctx['tag'] try: # check tag exists @@ -532,7 +530,7 @@ def __command_tag_kill(self, src: Source, ctx: Dict[str, Any]): except TagNotExistsException: src.reply(RTextMCDRTranslation('bot.error.tagNotExists', tag)) - def __command_info(self, src: Source, ctx: Dict[str, Any]): + def __command_info(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) def get_config_button( @@ -777,7 +775,7 @@ def get_config_button( src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) @new_thread('commandSave') - def __command_save(self, src: Source, ctx: Dict[str, Any]): + def __command_save(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) position = ctx.get('position') facing = ctx.get('facing', [0.0, 0.0]) @@ -803,7 +801,7 @@ def __command_save(self, src: Source, ctx: Dict[str, Any]): RTextMCDRTranslation('bot.error.botAlreadySaved', e.name) ) - def __command_del(self, src: Source, ctx: Dict[str, Any]): + def __command_del(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.delete(name) @@ -813,7 +811,7 @@ def __command_del(self, src: Source, ctx: Dict[str, Any]): except BotNotSavedException as e: src.reply(RTextMCDRTranslation('bot.error.botNotSaved', e.name)) - def __command_config_name(self, src: Source, ctx: Dict[str, Any]): + def __command_config_name(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) new_name = self.parse_name(ctx['newName']) try: @@ -828,7 +826,9 @@ def __command_config_name(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_position(self, src: Source, ctx: Dict[str, Any]): + def __command_config_position( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) position = ctx['position'] try: @@ -846,7 +846,7 @@ def __command_config_position(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_facing(self, src: Source, ctx: Dict[str, Any]): + def __command_config_facing(self, src: CommandSource, ctx: CommandContext): name = self.parse_name(ctx['name']) facing = ctx['facing'] try: @@ -864,7 +864,9 @@ def __command_config_facing(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_dimension(self, src: Source, ctx: Dict[str, Any]): + def __command_config_dimension( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) dimension = ctx['dimension'] try: @@ -891,7 +893,9 @@ def __command_config_dimension(self, src: Source, ctx: Dict[str, Any]): RTextMCDRTranslation('bot.error.illegalDimension', e.dimension) ) - def __command_config_comment(self, src: Source, ctx: Dict[str, Any]): + def __command_config_comment( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) comment = ctx['comment'] try: @@ -908,7 +912,7 @@ def __command_config_comment(self, src: Source, ctx: Dict[str, Any]): src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) def __command_config_actions_append( - self, src: Source, ctx: Dict[str, Any] + self, src: CommandSource, ctx: CommandContext ): name = self.parse_name(ctx['name']) action = ctx['action'] @@ -927,7 +931,7 @@ def __command_config_actions_append( src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) def __command_config_actions_insert( - self, src: Source, ctx: Dict[str, Any] + self, src: CommandSource, ctx: CommandContext ): name = self.parse_name(ctx['name']) index = ctx['index'] @@ -955,7 +959,7 @@ def __command_config_actions_insert( )) def __command_config_actions_delete( - self, src: Source, ctx: Dict[str, Any] + self, src: CommandSource, ctx: CommandContext ): name = self.parse_name(ctx['name']) index = ctx['index'] @@ -981,7 +985,9 @@ def __command_config_actions_delete( 'bot.error.illegalActionIndex', e.index )) - def __command_config_actions_edit(self, src: Source, ctx: Dict[str, Any]): + def __command_config_actions_edit( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) index = ctx['index'] action = ctx['action'] @@ -1007,7 +1013,9 @@ def __command_config_actions_edit(self, src: Source, ctx: Dict[str, Any]): 'bot.error.illegalActionIndex', e.index )) - def __command_config_actions_clear(self, src: Source, ctx: Dict[str, Any]): + def __command_config_actions_clear( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.get_bot(name).set_actions([]) @@ -1020,7 +1028,9 @@ def __command_config_actions_clear(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_tags_append(self, src: Source, ctx: Dict[str, Any]): + def __command_config_tags_append( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) tag = ctx['tag'] try: @@ -1037,7 +1047,9 @@ def __command_config_tags_append(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_tags_insert(self, src: Source, ctx: Dict[str, Any]): + def __command_config_tags_insert( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) index = ctx['index'] tag = ctx['tag'] @@ -1063,7 +1075,9 @@ def __command_config_tags_insert(self, src: Source, ctx: Dict[str, Any]): 'bot.error.illegalTagIndex', e.index )) - def __command_config_tags_delete(self, src: Source, ctx: Dict[str, Any]): + def __command_config_tags_delete( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) index = ctx['index'] try: @@ -1088,7 +1102,9 @@ def __command_config_tags_delete(self, src: Source, ctx: Dict[str, Any]): 'bot.error.illegalTagIndex', e.index )) - def __command_config_tags_edit(self, src: Source, ctx: Dict[str, Any]): + def __command_config_tags_edit( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) index = ctx['index'] tag = ctx['tag'] @@ -1114,7 +1130,9 @@ def __command_config_tags_edit(self, src: Source, ctx: Dict[str, Any]): 'bot.error.illegalTagIndex', e.index )) - def __command_config_tags_clear(self, src: Source, ctx: Dict[str, Any]): + def __command_config_tags_clear( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) try: self.__plugin.bot_manager.get_bot(name).set_tags([]) @@ -1127,7 +1145,9 @@ def __command_config_tags_clear(self, src: Source, ctx: Dict[str, Any]): except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_auto_login(self, src: Source, ctx: Dict[str, Any]): + def __command_config_auto_login( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) auto_login = ctx['autoLogin'] try: @@ -1142,7 +1162,7 @@ def __command_config_auto_login(self, src: Source, ctx: Dict[str, Any]): src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) def __command_config_auto_run_actions( - self, src: Source, ctx: Dict[str, Any] + self, src: CommandSource, ctx: CommandContext ): name = self.parse_name(ctx['name']) auto_run_actions = ctx['autoRunActions'] @@ -1160,7 +1180,9 @@ def __command_config_auto_run_actions( except BotNotExistsException as e: src.reply(RTextMCDRTranslation('bot.error.botNotExists', e.name)) - def __command_config_auto_update(self, src: Source, ctx: Dict[str, Any]): + def __command_config_auto_update( + self, src: CommandSource, ctx: CommandContext + ): name = self.parse_name(ctx['name']) auto_update = ctx['autoUpdate'] try: