mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-03 15:10:14 +08:00
docs add more java example (#1943)
* docs add java example * Update mirai-console/docs/Commands.md Co-authored-by: Him188 <Him188@mamoe.net> * Update mirai-console/docs/Commands.md Co-authored-by: Him188 <Him188@mamoe.net> * Update mirai-console/docs/Commands.md Co-authored-by: Him188 <Him188@mamoe.net> * Update mirai-console/docs/Commands.md Co-authored-by: Him188 <Him188@mamoe.net> * Update mirai-console/docs/Commands.md Co-authored-by: Him188 <Him188@mamoe.net> * apply doc suggestions Co-authored-by: Him188 <Him188@mamoe.net>
This commit is contained in:
parent
7c87f2869b
commit
ad5132e187
@ -130,6 +130,7 @@ Mirai Console 内建 [`SimpleCommand`] 与 [`CompositeCommand`] 拥有 [`Command
|
||||
|
||||
此时示例一定比理论有意义。
|
||||
|
||||
Kotlin 示例:
|
||||
```kotlin
|
||||
object MySimpleCommand : SimpleCommand(
|
||||
MyPluginMain, "tell", "私聊",
|
||||
@ -142,6 +143,22 @@ object MySimpleCommand : SimpleCommand(
|
||||
}
|
||||
```
|
||||
|
||||
Java 示例:
|
||||
```java
|
||||
public class MySimpleCommand extends SimpleCommand {
|
||||
|
||||
public MySimpleCommand(JvmPlugin plugin) {
|
||||
super(plugin, "tell", new String[]{"私聊"}, "Tell somebody privately", plugin.getParentPermission(), CommandArgumentContext.EMPTY);
|
||||
}
|
||||
|
||||
@Handler // 标记这是指令处理器,方法名随意
|
||||
public void handle(CommandSender sender, User target, String message) { // 后两个参数会被作为指令参数要求
|
||||
target.sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
指令 `/tell 123456 Hello` 的解析流程:
|
||||
1. 被分割为 `/`, `"tell"`, `"123456"`, `"Hello"`
|
||||
2. `MySimpleCommand` 被匹配到,根据 `/` 和 `"test"`。`"123456"`, `"Hello"` 被作为指令的原生参数。
|
||||
@ -157,6 +174,7 @@ object MySimpleCommand : SimpleCommand(
|
||||
|
||||
示例:
|
||||
|
||||
Kotlin 示例:
|
||||
```kotlin
|
||||
@OptIn(ConsoleExperimentalAPI::class)
|
||||
object MyCompositeCommand : CompositeCommand(
|
||||
@ -206,6 +224,58 @@ object MyCompositeCommand : CompositeCommand(
|
||||
}
|
||||
```
|
||||
|
||||
Java 示例:
|
||||
```java
|
||||
public class MyCompositeCommand extends CompositeCommand {
|
||||
|
||||
public MyCompositeCommand(JvmPlugin plugin) {
|
||||
// "manage" 是主指令名, 还有更多参数可填, 此处忽略
|
||||
super(plugin, "manage", new String[]{}, "示例指令", plugin.getParentPermission(), CommandArgumentContext.EMPTY);
|
||||
}
|
||||
|
||||
// [参数智能解析]
|
||||
//
|
||||
// 在控制台执行 "/manage <群号>.<群员> <持续时间>",
|
||||
// 或在聊天群内发送 "/manage <@一个群员> <持续时间>",
|
||||
// 或在聊天群内发送 "/manage <目标群员的群名> <持续时间>",
|
||||
// 或在聊天群内发送 "/manage <目标群员的账号> <持续时间>"
|
||||
// 时调用这个函数
|
||||
@SubCommand // 表示这是一个子指令,使用函数名作为子指令名称
|
||||
public void mute(CommandSender sender, Member target, int duration) { // 通过 /manage mute <target> <duration> 调用
|
||||
sender.sendMessage("/manage mute 被调用了, 参数为: " + target.toString() + ", " + duration);
|
||||
|
||||
String result;
|
||||
try {
|
||||
target.mute(duration);
|
||||
result = "成功";
|
||||
} catch (Exception e) {
|
||||
result = "失败," + e.getMessage();
|
||||
}
|
||||
|
||||
sender.sendMessage("结果: " + result);
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
public void foo(CommandSender sender) {
|
||||
// 使用 ConsoleCommandSender 作为接收者,表示指令只能由控制台执行。
|
||||
// 当用户尝试在聊天环境执行时将会收到错误提示。
|
||||
}
|
||||
|
||||
@SubCommand(value = {"list", "查看列表"}) // 可以设置多个子指令名。此时函数名会被忽略。
|
||||
public void ignoredFunctionName(CommandSender sender) { // 执行 "/manage list" 时调用这个函数
|
||||
sender.sendMessage("/manage list 被调用了");
|
||||
}
|
||||
|
||||
// 支持 Image 类型, 需在聊天中执行此指令.
|
||||
@SubCommand
|
||||
public void test(CommandSender sender, Image image) { // 执行 "/manage test <一张图片>" 时调用这个函数
|
||||
// 由于 Image 类型消息只可能在聊天环境,可以直接使用 UserCommandSender。
|
||||
|
||||
sender.sendMessage("/manage image 被调用了, 图片是 " + image.getImageId());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 文本参数的转义
|
||||
|
||||
不同的参数默认用空格分隔。有时用户希望在文字参数中包含空格本身,参数解析器可以接受三种表示方法。
|
||||
|
@ -348,6 +348,14 @@ MyPluginMain.launch {
|
||||
|
||||
详见 [`PluginFileExtensions`]。
|
||||
|
||||
Java 示例:
|
||||
```java
|
||||
File dataFile = JExample.INSTANCE.resolveDataFile("myDataFile.txt");
|
||||
// dataFile do something
|
||||
File configFile = JExample.INSTANCE.resolveConfigFile("myConfigFile.txt");
|
||||
// configFile do something
|
||||
```
|
||||
|
||||
#### 物理目录路径
|
||||
用 `$root` 表示 Mirai Console 运行路径,`$name` 表示插件名,
|
||||
插件数据目录一般在 `$root/data/$name`,插件配置目录一般在 `$root/config/$name`。
|
||||
@ -417,6 +425,17 @@ object MyData : AutoSavePluginData() {
|
||||
### 附录:Java 插件的多线程调度器 - [`JavaPluginScheduler`]
|
||||
拥有生命周期管理的简单 Java 线程池。其中所有的任务都会在插件被关闭时自动停止。
|
||||
|
||||
Java 示例:
|
||||
```
|
||||
JExample.INSTANCE.getScheduler().repeating(1 * 1000, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JExample.INSTANCE.getLogger().info("clock task arrival");
|
||||
// or do something
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
> 下一步,[Commands](Commands.md#mirai-console-backend---commands)
|
||||
>
|
||||
> 返回 [开发文档索引](README.md#mirai-console)
|
||||
|
Loading…
Reference in New Issue
Block a user