feat: add mcping and help command

This commit is contained in:
2024-09-03 16:15:26 +08:00
parent ca7a9f5002
commit 00c9ae7a64
6 files changed
+135 -13

No files matched your search

@@ -10,8 +10,10 @@ package cn.rtast.fancybot
import cn.rtast.fancybot.commands.AntiRevokeCommand
import cn.rtast.fancybot.commands.EchoCommand
import cn.rtast.fancybot.commands.FKXQSCommand
import cn.rtast.fancybot.commands.HelpCommand
import cn.rtast.fancybot.commands.HitokotoCommand
import cn.rtast.fancybot.commands.JrrpCommand
import cn.rtast.fancybot.commands.MCPingCommand
import cn.rtast.fancybot.commands.MusicCommand
import cn.rtast.fancybot.commands.MyPointCommand
import cn.rtast.fancybot.commands.QRCodeCommand
@@ -21,9 +23,9 @@ import cn.rtast.fancybot.entity.enums.WSType
import cn.rtast.fancybot.items.BaisiItem
import cn.rtast.fancybot.items.HeisiItem
import cn.rtast.fancybot.items.SetuItem
import cn.rtast.fancybot.util.item.ItemManager
import cn.rtast.fancybot.util.file.ConfigManager
import cn.rtast.fancybot.util.initDatabase
import cn.rtast.fancybot.util.item.ItemManager
import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.GetMessage
import cn.rtast.rob.entity.GroupMessage
@@ -64,14 +66,10 @@ class FancyBot : OBMessage {
}
val commands = listOf(
EchoCommand(),
JrrpCommand(),
MusicCommand(),
EchoCommand(), JrrpCommand(), MusicCommand(),
SignCommand(), RedeemCommand(), MyPointCommand(),
HitokotoCommand(),
FKXQSCommand(),
QRCodeCommand(),
AntiRevokeCommand()
HitokotoCommand(), FKXQSCommand(), QRCodeCommand(),
AntiRevokeCommand(), MCPingCommand(), HelpCommand()
)
val items = listOf(
@@ -0,0 +1,30 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/3
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.commands
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OBMessage
class HelpCommand : BaseCommand() {
override val commandNames = listOf("/help", "/帮助")
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
val allCommands = commands.joinToString("\n") {
"[${it.javaClass.name.split(".").last().replace("Command", "")}] 命令:${it.commandNames.joinToString(",")}"
}
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addNewLine()
.addText(allCommands)
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}
@@ -0,0 +1,69 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/3
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.entity.MCPingResponse
import cn.rtast.fancybot.util.fromJson
import cn.rtast.motdpinger.MotdPinger
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OBMessage
class MCPingCommand : BaseCommand() {
override val commandNames = listOf("/mcping", "/mcPing")
private val pingInstance = MotdPinger()
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addNewLine()
.addText("使用方法: /mcping <host>:[port]")
.addNewLine()
.addText("仅限Java版服务器哦~")
.build()
listener.sendGroupMessage(message.groupId, msg)
return
}
val parts = args.first().split(":")
val host: String
val port: Int
if (parts.size == 2) {
host = parts[0]
port = parts[1].toIntOrNull() ?: 25565
} else {
host = args.first()
port = 25565
}
try {
val rawResponse = pingInstance.pingServer(host, port)
val jsonResponse = rawResponse?.fromJson<MCPingResponse>()!!
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addNewLine()
.addImage(jsonResponse.favicon, true)
.addText("服务器地址: $host:$port")
.addNewLine()
.addText("协议版本号: ${jsonResponse.version.protocol}/${jsonResponse.version.name}")
.addNewLine()
.addText("玩家: ${jsonResponse.players.online}/${jsonResponse.players.max}")
.build()
listener.sendGroupMessage(message.groupId, msg)
} catch (_: Exception) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("ping失败~")
.addNewLine()
.addText("检查一下地址或者端口是否正确吧~")
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}
}
@@ -0,0 +1,24 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/3
*/
package cn.rtast.fancybot.entity
data class MCPingResponse(
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,
)
}