mirror of
https://github.com/jie65535/JGrasscutterCommand.git
synced 2025-10-16 17:25:47 +08:00
Update to allow batch execution of commands (#4)
This commit is contained in:
@@ -111,16 +111,16 @@ object JGrasscutterCommand : KotlinPlugin(
|
||||
// 处理执行游戏命令
|
||||
else if (message.startsWith(PluginConfig.commandPrefix)) {
|
||||
message = message.removePrefix(PluginConfig.commandPrefix).trim()
|
||||
if (message.isEmpty() || (message[0] == '/' && message.length == 1)) {
|
||||
if (message.isEmpty()) {
|
||||
return@subscribeAlways
|
||||
}
|
||||
|
||||
// 检查是否使用别名
|
||||
var command = PluginConfig.commandAlias[message]
|
||||
if (command.isNullOrEmpty())
|
||||
command = message
|
||||
// 如果是斜杠开头,则移除斜杠,在控制台执行不需要斜杠
|
||||
if (command[0] == '/') command = command.substring(1)
|
||||
command = if (command.isNullOrEmpty())
|
||||
message
|
||||
else
|
||||
command.replace('|', '\n') // 若为多行命令,替换为换行
|
||||
|
||||
// 执行的用户
|
||||
var user: User? = null
|
||||
@@ -153,12 +153,8 @@ object JGrasscutterCommand : KotlinPlugin(
|
||||
|
||||
try {
|
||||
// 调用接口执行命令
|
||||
val response = OpenCommandApi.runCommand(server.address, token, command)
|
||||
if (response.isNullOrEmpty()) {
|
||||
subject.sendMessage(this.message.quote() + "OK")
|
||||
} else {
|
||||
subject.sendMessage(this.message.quote() + response)
|
||||
}
|
||||
val response = OpenCommandApi.runCommands(server.address, token, command)
|
||||
subject.sendMessage(this.message.quote() + response)
|
||||
if (user != null) {
|
||||
// 计数并更新最后运行时间
|
||||
++user.runCount
|
||||
@@ -193,7 +189,7 @@ object JGrasscutterCommand : KotlinPlugin(
|
||||
// 否则如果启用了同步消息,且控制台令牌不为空,且为群消息时
|
||||
else if (server.consoleToken.isNotEmpty() && server.syncMessage && this is GroupMessageEvent) {
|
||||
try {
|
||||
OpenCommandApi.runCommand(
|
||||
OpenCommandApi.runCommands(
|
||||
server.address,
|
||||
server.consoleToken,
|
||||
"say <color=green>${sender.nameCardOrNick}</color>:\n${this.message.contentToString()}")
|
||||
|
@@ -332,7 +332,13 @@ object PluginCommands : CompositeCommand(
|
||||
// region 命令别名部分
|
||||
|
||||
@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) {
|
||||
if (alias.isEmpty() || command.isEmpty() || command[0].isEmpty()) {
|
||||
sendMessage("参数不能为空")
|
||||
|
@@ -140,13 +140,47 @@ object OpenCommandApi {
|
||||
|
||||
/**
|
||||
* 运行命令,成功时返回命令执行结果,失败时抛出异常,异常详情参考doRequest描述
|
||||
* 允许单次执行多条命令,用换行(\n)分隔
|
||||
* @param host 服务器地址
|
||||
* @param token 持久令牌
|
||||
* @param command 命令行
|
||||
* @param rawCommands 命令行
|
||||
* @return 命令执行结果
|
||||
* @see doRequest
|
||||
*/
|
||||
suspend fun runCommand(host: String, token: String, command: String): String? {
|
||||
return doRequest(host, json.encodeToString(CommandRequest(token, command)))
|
||||
suspend fun runCommands(host: String, token: String, rawCommands: String): String {
|
||||
// 去除首尾空白、命令前缀。使用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()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user