mirror of
https://github.com/jie65535/JGrasscutterCommand.git
synced 2025-06-01 17:29:13 +08:00
Update to allow batch execution of commands (#4)
This commit is contained in:
parent
93f3235dd0
commit
781979f937
35
README.md
35
README.md
@ -1,11 +1,17 @@
|
|||||||
# J Grasscutter Command
|
# J Grasscutter Command
|
||||||
# 在QQ群里远程执行命令的插件
|
|
||||||
|
This repo is only used for the Chinese social software QQ, so only the Chinese version is available.
|
||||||
|
|
||||||
|
# 用QQ执行GC命令的机器人插件
|
||||||
|
|
||||||
- 基于 [Mirai-Console](https://github.com/mamoe/mirai-console) 开发的插件
|
- 基于 [Mirai-Console](https://github.com/mamoe/mirai-console) 开发的插件
|
||||||
- 服务端必须使用 [OpenCommand](https://github.com/jie65535/gc-opencommand-plugin) 插件
|
> Mirai机器人相关文档请参阅 [用户手册](https://github.com/mamoe/mirai/blob/dev/docs/UserManual.md),
|
||||||
|
> 本项目**不会教你**如何安装和登录机器人,请自行了解Mirai相关信息。_目前暂不考虑其它框架或平台,有意者可自行移植_
|
||||||
|
|
||||||
Mirai机器人相关文档请参阅 [用户手册](https://github.com/mamoe/mirai/blob/dev/docs/UserManual.md),
|
- 服务端必须使用 [OpenCommand](https://github.com/jie65535/gc-opencommand-plugin) 插件
|
||||||
本项目**不会教你**如何安装和登录机器人,请自行了解Mirai相关信息。
|
- 若使用后觉得满意,可以给我一个 Star 作为鼓励 ; )
|
||||||
|
- 若有问题或者建议,欢迎提出 Issue 进行反馈。
|
||||||
|
- 建议 Watch 本项目以接收更新推送。
|
||||||
|
|
||||||
# 插件用法
|
# 插件用法
|
||||||
|
|
||||||
@ -83,6 +89,24 @@ _可以通过 `/jgc setBindCommand <prefix>` 来修改执行命令前缀 _(例
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
你还可以一次性执行多条命令,并且可以通过在别名中设置多行命令来实现组合命令
|
||||||
|
|
||||||
|
例如:
|
||||||
|
```shell
|
||||||
|
!give 102 9999
|
||||||
|
give 203 999
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
|
还可以设置别名为多条命令,用`|`分隔,例如:
|
||||||
|
|
||||||
|
`/jgc setCommand 新手礼包 give 102 9999|give 202 99|give 203 99`
|
||||||
|
|
||||||
|
然后通过别名批量执行命令,例如:`!新手礼包`
|
||||||
|
|
||||||
|
|
||||||
## 私聊执行
|
## 私聊执行
|
||||||
v0.3.0 开始,玩家可以**私聊机器人**进行账号的绑定和命令的执行,
|
v0.3.0 开始,玩家可以**私聊机器人**进行账号的绑定和命令的执行,
|
||||||
但是目前只能在**默认服务器**中执行,无法指定执行的服务器。
|
但是目前只能在**默认服务器**中执行,无法指定执行的服务器。
|
||||||
@ -176,7 +200,8 @@ defaultServerId: 1
|
|||||||
|
|
||||||
## 命令相关
|
## 命令相关
|
||||||
```shell
|
```shell
|
||||||
/jgc setCommand <alias> <command> # 添加命令别名
|
/jgc listCommands # 列出所有别名
|
||||||
|
/jgc setCommand <alias> <command> # 添加命令别名(聊天执行可传多行命令)
|
||||||
/jgc removeCommand <alias> # 删除命令别名
|
/jgc removeCommand <alias> # 删除命令别名
|
||||||
/jgc addPublicCommand <command> # 添加公开命令(可用别名)(游客可用)
|
/jgc addPublicCommand <command> # 添加公开命令(可用别名)(游客可用)
|
||||||
/jgc removePublicCommand <command> # 删除公开命令
|
/jgc removePublicCommand <command> # 删除公开命令
|
||||||
|
BIN
screenshot/batch.jpg
Normal file
BIN
screenshot/batch.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 180 KiB |
@ -111,16 +111,16 @@ object JGrasscutterCommand : KotlinPlugin(
|
|||||||
// 处理执行游戏命令
|
// 处理执行游戏命令
|
||||||
else if (message.startsWith(PluginConfig.commandPrefix)) {
|
else if (message.startsWith(PluginConfig.commandPrefix)) {
|
||||||
message = message.removePrefix(PluginConfig.commandPrefix).trim()
|
message = message.removePrefix(PluginConfig.commandPrefix).trim()
|
||||||
if (message.isEmpty() || (message[0] == '/' && message.length == 1)) {
|
if (message.isEmpty()) {
|
||||||
return@subscribeAlways
|
return@subscribeAlways
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否使用别名
|
// 检查是否使用别名
|
||||||
var command = PluginConfig.commandAlias[message]
|
var command = PluginConfig.commandAlias[message]
|
||||||
if (command.isNullOrEmpty())
|
command = if (command.isNullOrEmpty())
|
||||||
command = message
|
message
|
||||||
// 如果是斜杠开头,则移除斜杠,在控制台执行不需要斜杠
|
else
|
||||||
if (command[0] == '/') command = command.substring(1)
|
command.replace('|', '\n') // 若为多行命令,替换为换行
|
||||||
|
|
||||||
// 执行的用户
|
// 执行的用户
|
||||||
var user: User? = null
|
var user: User? = null
|
||||||
@ -153,12 +153,8 @@ object JGrasscutterCommand : KotlinPlugin(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 调用接口执行命令
|
// 调用接口执行命令
|
||||||
val response = OpenCommandApi.runCommand(server.address, token, command)
|
val response = OpenCommandApi.runCommands(server.address, token, command)
|
||||||
if (response.isNullOrEmpty()) {
|
subject.sendMessage(this.message.quote() + response)
|
||||||
subject.sendMessage(this.message.quote() + "OK")
|
|
||||||
} else {
|
|
||||||
subject.sendMessage(this.message.quote() + response)
|
|
||||||
}
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
// 计数并更新最后运行时间
|
// 计数并更新最后运行时间
|
||||||
++user.runCount
|
++user.runCount
|
||||||
@ -193,7 +189,7 @@ object JGrasscutterCommand : KotlinPlugin(
|
|||||||
// 否则如果启用了同步消息,且控制台令牌不为空,且为群消息时
|
// 否则如果启用了同步消息,且控制台令牌不为空,且为群消息时
|
||||||
else if (server.consoleToken.isNotEmpty() && server.syncMessage && this is GroupMessageEvent) {
|
else if (server.consoleToken.isNotEmpty() && server.syncMessage && this is GroupMessageEvent) {
|
||||||
try {
|
try {
|
||||||
OpenCommandApi.runCommand(
|
OpenCommandApi.runCommands(
|
||||||
server.address,
|
server.address,
|
||||||
server.consoleToken,
|
server.consoleToken,
|
||||||
"say <color=green>${sender.nameCardOrNick}</color>:\n${this.message.contentToString()}")
|
"say <color=green>${sender.nameCardOrNick}</color>:\n${this.message.contentToString()}")
|
||||||
|
@ -332,7 +332,13 @@ object PluginCommands : CompositeCommand(
|
|||||||
// region 命令别名部分
|
// region 命令别名部分
|
||||||
|
|
||||||
@SubCommand
|
@SubCommand
|
||||||
@Description("添加命令别名")
|
@Description("列出所有别名")
|
||||||
|
suspend fun CommandSender.listCommands() {
|
||||||
|
sendMessage(PluginConfig.commandAlias.map { "[${it.key}] ${it.value}" }.joinToString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubCommand
|
||||||
|
@Description("添加命令别名,多条命令用|隔开")
|
||||||
suspend fun CommandSender.setCommand(alias: String, vararg command: String) {
|
suspend fun CommandSender.setCommand(alias: String, vararg command: String) {
|
||||||
if (alias.isEmpty() || command.isEmpty() || command[0].isEmpty()) {
|
if (alias.isEmpty() || command.isEmpty() || command[0].isEmpty()) {
|
||||||
sendMessage("参数不能为空")
|
sendMessage("参数不能为空")
|
||||||
|
@ -140,13 +140,47 @@ object OpenCommandApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 运行命令,成功时返回命令执行结果,失败时抛出异常,异常详情参考doRequest描述
|
* 运行命令,成功时返回命令执行结果,失败时抛出异常,异常详情参考doRequest描述
|
||||||
|
* 允许单次执行多条命令,用换行(\n)分隔
|
||||||
* @param host 服务器地址
|
* @param host 服务器地址
|
||||||
* @param token 持久令牌
|
* @param token 持久令牌
|
||||||
* @param command 命令行
|
* @param rawCommands 命令行
|
||||||
* @return 命令执行结果
|
* @return 命令执行结果
|
||||||
* @see doRequest
|
* @see doRequest
|
||||||
*/
|
*/
|
||||||
suspend fun runCommand(host: String, token: String, command: String): String? {
|
suspend fun runCommands(host: String, token: String, rawCommands: String): String {
|
||||||
return doRequest(host, json.encodeToString(CommandRequest(token, command)))
|
// 去除首尾空白、命令前缀。使用api执行命令不需要前缀
|
||||||
|
val commands = rawCommands.splitToSequence('\n')
|
||||||
|
.map { it.trim().trimStart('/').trimStart('!') }
|
||||||
|
.toList()
|
||||||
|
return if (commands.isEmpty())
|
||||||
|
throw IllegalArgumentException("命令不能为空!")
|
||||||
|
else if (commands.size == 1) {
|
||||||
|
val ret = doRequest(host, json.encodeToString(CommandRequest(token, commands[0])))
|
||||||
|
if (ret.isNullOrEmpty()) "OK" else ret
|
||||||
|
} else {
|
||||||
|
val msg = StringBuilder()
|
||||||
|
var okCount = 0
|
||||||
|
for (cmd in commands) {
|
||||||
|
val ret = doRequest(host, json.encodeToString(CommandRequest(token, cmd)))
|
||||||
|
if (ret.isNullOrEmpty()) {
|
||||||
|
if (okCount++ == 0)
|
||||||
|
msg.append("OK")
|
||||||
|
} else {
|
||||||
|
if (okCount > 0) {
|
||||||
|
if (okCount > 1) {
|
||||||
|
msg.append('*').append(okCount)
|
||||||
|
}
|
||||||
|
msg.appendLine()
|
||||||
|
okCount = 0
|
||||||
|
}
|
||||||
|
msg.appendLine(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (okCount > 1) // OK*n
|
||||||
|
msg.append('*').append(okCount)
|
||||||
|
else if (msg[msg.length-1] == '\n') // 移除额外的换行
|
||||||
|
msg.deleteCharAt(msg.length-1)
|
||||||
|
msg.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user