Skip to content

Commit

Permalink
refactor(bot): 🏷️ update types
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 1, 2024
1 parent c7f48be commit 2b2b1ab
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions src/bot/bot/command_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,8 +15,6 @@
if TYPE_CHECKING:
from bot.plugin import Plugin

Source = Union[ConsoleCommandSource, PlayerCommandSource]


class PermissionsRequirements(Serializable):
LIST: Callable
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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']
Expand Down Expand Up @@ -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']
Expand All @@ -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']
Expand All @@ -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([])
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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([])
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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:
Expand Down

0 comments on commit 2b2b1ab

Please sign in to comment.