refactor: mcping command
This commit is contained in:
3 files changed
+62
-61
No files matched your search
@@ -5,7 +5,7 @@ shadowVersion = "8.3.0"
|
|||||||
okhttpVersion = "5.0.0-alpha.14"
|
okhttpVersion = "5.0.0-alpha.14"
|
||||||
exposedVersion = "0.53.0"
|
exposedVersion = "0.53.0"
|
||||||
sqliteJDBCVersion = "3.46.1.0"
|
sqliteJDBCVersion = "3.46.1.0"
|
||||||
motdPingerVersion = "1.0.1"
|
motdPingerVersion = "1.1.0"
|
||||||
animatedGifLibVersion = "1.4"
|
animatedGifLibVersion = "1.4"
|
||||||
simpleJavaMailVersion = "8.11.3"
|
simpleJavaMailVersion = "8.11.3"
|
||||||
jsoupVersion = "1.18.1"
|
jsoupVersion = "1.18.1"
|
||||||
|
|||||||
@@ -7,60 +7,85 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.commands.lookup
|
package cn.rtast.fancybot.commands.lookup
|
||||||
|
|
||||||
import cn.rtast.fancybot.entity.MCPing
|
import cn.rtast.motdpinger.BedrockPing
|
||||||
import cn.rtast.fancybot.util.str.fromJson
|
import cn.rtast.motdpinger.JavaPing
|
||||||
import cn.rtast.motdpinger.MotdPinger
|
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.MessageChain
|
import cn.rtast.rob.util.ob.MessageChain
|
||||||
import cn.rtast.rob.util.ob.OneBotListener
|
import cn.rtast.rob.util.ob.OneBotListener
|
||||||
|
import java.net.SocketException
|
||||||
|
|
||||||
class MCPingCommand : BaseCommand() {
|
class MCPingCommand : BaseCommand() {
|
||||||
override val commandNames = listOf("/mcping", "/mcPing")
|
override val commandNames = listOf("/mcping", "/mcPing")
|
||||||
|
|
||||||
private val pingInstance = MotdPinger()
|
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
if (args.isEmpty()) {
|
if (args.isEmpty()) {
|
||||||
val msg = MessageChain.Builder()
|
val msg = MessageChain.Builder()
|
||||||
.addAt(message.sender.userId)
|
.addAt(message.sender.userId)
|
||||||
.addNewLine()
|
.addNewLine()
|
||||||
.addText("使用方法: /mcping <host>:[port]")
|
.addText("使用方法: /mcping <host>:[port] [java|be]")
|
||||||
.addNewLine()
|
.addNewLine()
|
||||||
.addText("仅限Java版服务器哦~")
|
.addText("不添加平台默认Java版~")
|
||||||
.build()
|
.build()
|
||||||
listener.sendGroupMessage(message.groupId, msg)
|
listener.sendGroupMessage(message.groupId, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val parts = args.first().split(":")
|
val platform = if (args.size == 2) args.last() else "java"
|
||||||
val host: String
|
when (platform) {
|
||||||
val port: Int
|
"java", "je", "Java", "JAVA" -> {
|
||||||
if (parts.size == 2) {
|
val parts = args.first().split(":")
|
||||||
host = parts[0]
|
val port = if (parts.size == 1) {
|
||||||
port = parts[1].toIntOrNull() ?: 25565
|
25565
|
||||||
} else {
|
} else {
|
||||||
host = args.first()
|
parts.last().toInt()
|
||||||
port = 25565
|
}
|
||||||
}
|
val host = parts.first()
|
||||||
try {
|
val response = JavaPing().ping(host, port, 10000)
|
||||||
val rawResponse = pingInstance.pingServer(host, port)
|
if (response == null) {
|
||||||
val jsonResponse = rawResponse?.fromJson<MCPing>()!!
|
message.reply("无法获取服务器的消息请检查输入是否正确 >>> $host:$port")
|
||||||
val msg = MessageChain.Builder().addAt(message.sender.userId).addNewLine()
|
return
|
||||||
if (jsonResponse.favicon != null) msg.addImage(jsonResponse.favicon, true)
|
}
|
||||||
msg.addText("服务器地址: $host:$port")
|
val msg = MessageChain.Builder()
|
||||||
msg.addNewLine()
|
response.favicon?.let {
|
||||||
msg.addText("协议版本号: ${jsonResponse.version.protocol}/${jsonResponse.version.name}")
|
msg.addImage(response.favicon!!.replace("data:image/png;base64,", ""), true)
|
||||||
msg.addNewLine()
|
}
|
||||||
msg.addText("玩家: ${jsonResponse.players.online}/${jsonResponse.players.max}")
|
msg.addText("服务器地址: $host:$port | 服务器类型: Java")
|
||||||
listener.sendGroupMessage(message.groupId, msg.build())
|
.addNewLine()
|
||||||
} catch (_: Exception) {
|
.addText("服务器版本: ${response.version.name}/${response.version.protocol}")
|
||||||
val msg = MessageChain.Builder()
|
.addNewLine()
|
||||||
.addAt(message.sender.userId)
|
.addText("在线玩家数: ${response.players.online}/${response.players.max}")
|
||||||
.addText("ping失败~")
|
message.reply(msg.build())
|
||||||
.addNewLine()
|
}
|
||||||
.addText("检查一下地址或者端口是否正确吧~")
|
|
||||||
.build()
|
"be", "bedrock", "BE", "Bedrock" -> {
|
||||||
listener.sendGroupMessage(message.groupId, msg)
|
val parts = args.first().split(":")
|
||||||
|
val port = if (parts.size == 1) {
|
||||||
|
19132
|
||||||
|
} else {
|
||||||
|
parts.last().toInt()
|
||||||
|
}
|
||||||
|
val host = parts.first()
|
||||||
|
try {
|
||||||
|
val response = BedrockPing().ping(host, port, 10000)
|
||||||
|
val msg = MessageChain.Builder()
|
||||||
|
.addText("服务器地址: $host:$port | 服务器类型: Bedrock")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("服务器版本: ${response.version}/${response.protocolVersion}")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("在线玩家: ${response.onlinePlayers}/${response.maxPlayers}")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("MOTD: ${response.motd}")
|
||||||
|
.addNewLine()
|
||||||
|
.addText(response.subTitle)
|
||||||
|
message.reply(msg.build())
|
||||||
|
}catch (_: SocketException) {
|
||||||
|
message.reply("无法从服务器接收Ping结果请检查服务器地址是否正确 >>> $host:$port")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
message.reply("输入错误请在 `java` 和 `be`中选择 >>> $platform")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright © 2024 RTAkland
|
|
||||||
* Author: RTAkland
|
|
||||||
* Date: 2024/9/3
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
package cn.rtast.fancybot.entity
|
|
||||||
|
|
||||||
data class MCPing(
|
|
||||||
val version: Version,
|
|
||||||
val players: Players,
|
|
||||||
val favicon: String?
|
|
||||||
) {
|
|
||||||
data class Version(
|
|
||||||
val protocol: Int,
|
|
||||||
val name: String,
|
|
||||||
)
|
|
||||||
|
|
||||||
data class Players(
|
|
||||||
val online: Int,
|
|
||||||
val max: Int,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user