Skip to content

Commit

Permalink
feat(bot): ✨ improve list command (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Aug 18, 2024
1 parent d6c3845 commit 3b43b00
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 120 deletions.
14 changes: 9 additions & 5 deletions src/bot/bot/bot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,24 @@ def list(
self,
index: int,
online: bool,
saved: bool
saved: bool,
tag: str = None
) -> Tuple[List[Bot], int]:
"""
List bots with filters.
:param index: Page index.
:param online: Include online bots.
:param saved: Include saved bots.
:param tag: Tag, only include bots with this tag if not None.
:return: A list of bots.
"""
# Filter bots by online and saved
bots = [
bot for bot in self.bots.values()
if (bot.online and online) or (bot.saved and saved)
]
bots = []
for bot in self.bots.values():
condition = (online and bot.online) or (saved and bot.saved)
condition = condition and (tag is None or tag in bot.tags)
if condition:
bots.append(bot)

# Check index and filter bots to page
max_index = math.ceil(len(bots) / 10) - 1
Expand Down
192 changes: 99 additions & 93 deletions src/bot/bot/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from mcdreforged.api.command import *
from mcdreforged.api.rtext import *
from mcdreforged.api.decorator import new_thread
from mcdreforged.api.utils.serializer import Serializable
from more_command_nodes import Position, Facing, EnumeratedText

from bot.exceptions import *
Expand All @@ -16,77 +15,69 @@
from bot.plugin import Plugin


class PermissionsRequirements(Serializable):
LIST: Callable
SPAWN: Callable
KILL: Callable
ACTION: Callable
TAGS: Callable
INFO: Callable
SAVE: Callable
DEL: Callable
CONFIG: Callable


class ListArguments(Enum):
ALL = '--all'
ONLINE = '--online'
SAVED = '--saved'


class CommandHandler:
def __init__(self, plugin: 'Plugin'):
self.__plugin: 'Plugin' = plugin
permissions = PermissionsRequirements(
**{
key.upper(): Requirements.has_permission(value)
for key, value
in self.__plugin.config.permissions.items()
}
)
self.register_commands()

def register_commands(self):
def bot_list(online: bool = None) -> Callable[[], List[str]]:
return lambda: [
name for name, bot in
self.__plugin.bot_manager.bots.items()
if online is None or bot.online == online
]

list_literal = (
Literal('list')
.requires(permissions.LIST)
.runs(self.__command_list)
.then(
Integer('index')
.runs(self.__command_list)
.then(
EnumeratedText('arg', ListArguments)
.runs(self.__command_list)
)
def create_subcommand(literal: str) -> Literal:
node = Literal(literal)
permission = self.__plugin.config.permissions[literal]
node.requires(
Requirements.has_permission(permission),
lambda: self.__plugin.server.rtr('bot.error.permissionDenied')
)
)
spawn_literal = (
Literal('spawn')
.requires(permissions.SPAWN)
.then(
return node

def make_list_command() -> Literal:
list_literal = create_subcommand('list').runs(self.__command_list)
list_literal.then(
Literal('--index')
.then(Integer('index').redirects(list_literal))
)
list_literal.then(
CountingLiteral('--online', 'online')
.redirects(list_literal)
)
list_literal.then(
CountingLiteral('--saved', 'saved')
.redirects(list_literal)
)
list_literal.then(
Literal('--tag')
.then(Text('tag').redirects(list_literal))
)
return list_literal

def make_spawn_command() -> Literal:
spawn_literal = create_subcommand('spawn')
spawn_literal.then(
Text('name')
.runs(self.__command_spawn)
.suggests(bot_list(False))
)
)
kill_literal = (
Literal('kill')
.requires(permissions.KILL)
.then(
return spawn_literal

def make_kill_command() -> Literal:
kill_literal = create_subcommand('kill')
kill_literal.then(
Text('name')
.runs(self.__command_kill)
.suggests(bot_list(True))
)
)
action_literal = (
Literal('action')
.requires(permissions.ACTION)
.then(
return kill_literal

def make_action_command() -> Literal:
action_literal = create_subcommand('action')
action_literal.then(
Text('name')
.runs(self.__command_action)
.suggests(bot_list(True))
Expand All @@ -95,12 +86,12 @@ def bot_list(online: bool = None) -> Callable[[], List[str]]:
.runs(self.__command_action)
)
)
)
tags_literal = (
Literal('tags')
.requires(permissions.TAGS)
.runs(self.__command_tag_list)
.then(
return action_literal

def make_tags_command() -> Literal:
tags_literal = create_subcommand('tags')
tags_literal.runs(self.__command_tag_list)
tags_literal.then(
Text('tag')
.suggests(self.tag_list)
.then(
Expand All @@ -112,20 +103,20 @@ def bot_list(online: bool = None) -> Callable[[], List[str]]:
.runs(self.__command_tag_kill)
)
)
)
info_literal = (
Literal('info')
.requires(permissions.INFO)
.then(
return tags_literal

def make_info_command() -> Literal:
info_literal = create_subcommand('info')
info_literal.then(
Text('name')
.runs(self.__command_info)
.suggests(bot_list())
)
)
save_literal = (
Literal('save')
.requires(permissions.SAVE)
.then(
return info_literal

def make_save_command() -> Literal:
save_literal = create_subcommand('save')
save_literal.then(
Text('name')
.runs(self.__command_save)
.then(
Expand All @@ -141,20 +132,20 @@ def bot_list(online: bool = None) -> Callable[[], List[str]]:
)
)
)
)
del_literal = (
Literal('del')
.requires(permissions.DEL)
.then(
return save_literal

def make_del_command() -> Literal:
del_literal = create_subcommand('del')
del_literal.then(
Text('name')
.runs(self.__command_del)
.suggests(bot_list())
)
)
config_literal = (
Literal('config')
.requires(permissions.CONFIG)
.then(
return del_literal

def make_config_command() -> Literal:
config_literal = create_subcommand('config')
config_literal.then(
Text('name')
.suggests(bot_list())
.then(
Expand Down Expand Up @@ -296,7 +287,8 @@ def bot_list(online: bool = None) -> Callable[[], List[str]]:
)
)
)
)
return config_literal

self.__plugin.server.register_help_message(
'!!bot',
RTextMCDRTranslation('bot.help.message')
Expand All @@ -310,15 +302,15 @@ def bot_list(online: bool = None) -> Callable[[], List[str]]:
self.__plugin.server.rtr('bot.help.content')
)
)
.then(list_literal)
.then(spawn_literal)
.then(kill_literal)
.then(action_literal)
.then(tags_literal)
.then(info_literal)
.then(save_literal)
.then(del_literal)
.then(config_literal)
.then(make_list_command())
.then(make_spawn_command())
.then(make_kill_command())
.then(make_action_command())
.then(make_tags_command())
.then(make_info_command())
.then(make_save_command())
.then(make_del_command())
.then(make_config_command())
)

def tag_list(self) -> Set[str]:
Expand All @@ -328,13 +320,19 @@ def tag_list(self) -> Set[str]:
return set(tags)

def __command_list(self, src: CommandSource, ctx: CommandContext):
show_online = ctx.get('online', 0) > 0
show_saved = ctx.get('saved', 0) > 0
show_all = (not show_online) and (not show_saved)
index = ctx.get('index', 0)
arg = ctx.get('arg', ListArguments.ALL)
tag = ctx.get('tag')

self.__plugin.server.logger.warning(tag)
try:
bot_list, max_index = self.__plugin.bot_manager.list(
index,
arg == ListArguments.ALL or arg == ListArguments.ONLINE,
arg == ListArguments.ALL or arg == ListArguments.SAVED
show_all or show_online,
show_all or show_saved,
tag
)

# Header
Expand Down Expand Up @@ -404,14 +402,22 @@ def __command_list(self, src: CommandSource, ctx: CommandContext):
if index != max_index
else RColor.dark_gray
)
cmd_template = '!!bot list'
if show_online:
cmd_template += ' --online'
if show_saved:
cmd_template += ' --saved'
if tag is not None:
cmd_template += f' --tag {tag}'
cmd_template += ' --index {}'
message.append(RTextList(
'\n',
RText('<<< ', color=left_color).c(
RAction.run_command, f'!!bot list {index - 1}'
RAction.run_command, cmd_template.format(index - 1)
),
index, ' / ', max_index,
RText(' >>>', color=right_color).c(
RAction.run_command, f'!!bot list {index + 1}'
RAction.run_command, cmd_template.format(index + 1)
)
))

Expand Down
3 changes: 2 additions & 1 deletion src/bot/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bot.help.message.hover: Click to show bot list
bot.help.content: |
-----------------------------------
§6!!bot §7View help
§6!!bot list [index] [filter]§7Show bot list
§6!!bot list [--index <index>] [filters]§7Show bot list
§6!!bot spawn <name> §7Spawn bot
§6!!bot kill <name> §7Kill bot
§6!!bot action <name> [index] §7Execute bot action(s)
Expand All @@ -13,6 +13,7 @@ bot.help.content: |
§6!!bot save <name> [position] [facing] [dimension] §7Save bot
§6!!bot del <name> §7Delete saved bot
§6!!bot config <name> <option> <value> §7onfig bot
bot.error.permissionDenied: §cPermission denied
bot.error.illegalDimension: §cDimension §6{0} §cis illegal!
bot.error.illegalListIndex: §cError list index §6{0}§c!
bot.error.illegalActionIndex: §cError action index §6{0}§c!
Expand Down
3 changes: 2 additions & 1 deletion src/bot/lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bot.help.message.hover: 点击显示假人列表
bot.help.content: |
-----------------------------------
§6!!bot §7查看帮助
§6!!bot list [index] [filter]§7显示假人列表
§6!!bot list [--index <index>] [filters]§7显示假人列表
§6!!bot spawn <name> §7上线假人
§6!!bot kill <name> §7下线假人
§6!!bot action <name> [index] §7执行假人动作
Expand All @@ -13,6 +13,7 @@ bot.help.content: |
§6!!bot save <name> [position] [facing] [dimension] §7保存假人
§6!!bot del <name> §7删除保存的假人
§6!!bot config <name> <option> <value> §7配置假人
bot.error.permissionDenied: §c权限不足
bot.error.illegalDimension: §c维度 §6{0} §c不合法!
bot.error.illegalListIndex: §c错误的列表索引 §6{0}§c!
bot.error.illegalActionIndex: §c错误的动作索引 §6{0}§c!
Expand Down
Loading

0 comments on commit 3b43b00

Please sign in to comment.