Skip to content

Commit

Permalink
feat(command_aliases): ✨ add new plugin command_aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 22, 2024
1 parent 5c19321 commit d4be252
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .release-please/release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"src/bot": {
"package-name": "bot"
},
"src/command_aliases": {
"package-name": "command_aliases"
},
"src/database_api": {
"package-name": "database_api"
},
Expand Down
31 changes: 31 additions & 0 deletions src/command_aliases/command_aliases/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from mcdreforged.api.command import *
from mcdreforged.api.types import PluginServerInterface
from mcdreforged.api.utils.serializer import Serializable


class Config(Serializable):
alias: dict[str, str] = {}


def on_load(server: PluginServerInterface, prev_module):
config = server.load_config_simple(target_class=Config)

for alias, command in config.alias.items():
server.register_command(
Literal(alias)
.runs(get_handler(command))
.then(
GreedyText('content')
.runs(get_handler(command))
)
)


def get_handler(command):
def handler(src, ctx):
src.get_server().execute_command(
f'{command} {ctx.get("content", "")}',
src
)

return handler
11 changes: 11 additions & 0 deletions src/command_aliases/mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "command_aliases",
"version": "1.0.0",
"name": "Command Aliases",
"description": {
"en_us": "Aliases commands",
"zh_cn": "为命令添加别名"
},
"author": "Andy Zhang",
"link": "https://github.com/AnzhiZhang/MCDReforgedPlugins/tree/master/src/command_aliases"
}
20 changes: 20 additions & 0 deletions src/command_aliases/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Command Aliases

> Alias commands by config
## Usage

You need to add all commands you would like to alias in the config file, where keys are the alias and values are the original commands.

```json
{
"alias": {
"!!mcdr": "!!MCDR"
}
}
```

Now you can use `!!mcdr` as an alias for `!!MCDR`.

> [!NOTE]
> There will be no completion for alias commands. If you want to use completion, you need to use original commands. It does not make sense if you need to use alias in an environment where you have completion.

0 comments on commit d4be252

Please sign in to comment.