feat: add idiom explain and add mcping latency

This commit is contained in:
2024-10-10 16:03:51 +08:00
parent 1c00093560
commit 90225ae147
8 files changed
+90 -21

No files matched your search

+1 -1
View File
@@ -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.1.2" motdPingerVersion = "1.1.4"
animatedGifLibVersion = "1.4" animatedGifLibVersion = "1.4"
simpleJavaMailVersion = "8.12.2" simpleJavaMailVersion = "8.12.2"
jsoupVersion = "1.18.1" jsoupVersion = "1.18.1"
+3 -2
View File
@@ -77,7 +77,8 @@ val DEFAULT_CONFIG = Config(
cloudflareR2AccessKeyId = "11111", cloudflareR2AccessKeyId = "11111",
cloudflareR2SecretKey = "222", cloudflareR2SecretKey = "222",
cloudflareR2BucketName = "fancybot", cloudflareR2BucketName = "fancybot",
cloudflareR2PublicUrl = "https://114514.com" cloudflareR2PublicUrl = "https://114514.com",
apiSpaceKey = "114514"
) )
val configManager = ConfigManager() val configManager = ConfigManager()
@@ -118,5 +119,5 @@ val commands = listOf(
ReactionCommand(), MCSkinCommand(), GithubLatestCommitCommand(), RandomMusicCommand(), ReactionCommand(), MCSkinCommand(), GithubLatestCommitCommand(), RandomMusicCommand(),
ZDJDCommand(), ScanQRCodeCommand(), TTSCommand(), SlimeChunkHelperCommand(), ZDJDCommand(), ScanQRCodeCommand(), TTSCommand(), SlimeChunkHelperCommand(),
SupportMeCommand(), TheCatCommand(), TheHistoryOfTodayCommand(), MCVersionCommand(), SupportMeCommand(), TheCatCommand(), TheHistoryOfTodayCommand(), MCVersionCommand(),
MinecraftWikiCommand(), GaoKaoDaysRemainCommand() MinecraftWikiCommand(), GaoKaoDaysRemainCommand(), IdiomExplainCommand()
) )
@@ -31,7 +31,6 @@ import cn.rtast.fancybot.util.str.fromJson
import cn.rtast.fancybot.util.str.uriEncode import cn.rtast.fancybot.util.str.uriEncode
import cn.rtast.motdpinger.BedrockPing import cn.rtast.motdpinger.BedrockPing
import cn.rtast.motdpinger.JavaPing import cn.rtast.motdpinger.JavaPing
import cn.rtast.motdpinger.removeColorCodes
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
@@ -310,6 +309,10 @@ class MCSkinCommand : BaseCommand() {
class MCPingCommand : BaseCommand() { class MCPingCommand : BaseCommand() {
override val commandNames = listOf("/mcping") override val commandNames = listOf("/mcping")
private fun String.removeColorCodes(): String {
return this.replace(Regex("§."), "")
}
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()
@@ -324,11 +327,7 @@ class MCPingCommand : BaseCommand() {
when (platform) { when (platform) {
"java", "je", "Java", "JAVA" -> { "java", "je", "Java", "JAVA" -> {
val parts = args.first().split(":") val parts = args.first().split(":")
val port = if (parts.size == 1) { val port = if (parts.size == 1) 25565 else parts.last().toInt()
25565
} else {
parts.last().toInt()
}
val host = parts.first() val host = parts.first()
val response = JavaPing().ping(host, port, 10000) val response = JavaPing().ping(host, port, 10000)
if (response == null) { if (response == null) {
@@ -336,10 +335,10 @@ class MCPingCommand : BaseCommand() {
return return
} }
val msg = MessageChain.Builder() val msg = MessageChain.Builder()
response.favicon?.let { response.favicon?.let { msg.addImage(response.favicon!!.replace("data:image/png;base64,", ""), true) }
msg.addImage(response.favicon!!.replace("data:image/png;base64,", ""), true) msg.addText("服务器地址: $host:$port")
} .addNewLine()
msg.addText("服务器地址: $host:$port | 服务器类型: Java") .addText("服务器类型: Java | 延迟: ${response.latency}ms")
.addNewLine() .addNewLine()
.addText("服务器版本: ${response.version.name}/${response.version.protocol}") .addText("服务器版本: ${response.version.name}/${response.version.protocol}")
.addNewLine() .addNewLine()
@@ -349,16 +348,14 @@ class MCPingCommand : BaseCommand() {
"be", "bedrock", "BE", "Bedrock" -> { "be", "bedrock", "BE", "Bedrock" -> {
val parts = args.first().split(":") val parts = args.first().split(":")
val port = if (parts.size == 1) { val port = if (parts.size == 1) 19132 else parts.last().toInt()
19132
} else {
parts.last().toInt()
}
val host = parts.first() val host = parts.first()
try { try {
val response = BedrockPing().ping(host, port, 10000) val response = BedrockPing().ping(host, port, 10000)
val msg = MessageChain.Builder() val msg = MessageChain.Builder()
.addText("服务器地址: $host:$port | 服务器类型: Bedrock") .addText("服务器地址: $host:$port")
.addNewLine()
.addText("服务器类型: Bedrock(基岩版) | 延迟: ${response.latency}ms")
.addNewLine() .addNewLine()
.addText("服务器版本: ${response.version}/${response.protocolVersion}") .addText("服务器版本: ${response.version}/${response.protocolVersion}")
.addNewLine() .addNewLine()
@@ -0,0 +1,49 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/10
*/
package cn.rtast.fancybot.commands.misc
import cn.rtast.fancybot.configManager
import cn.rtast.fancybot.entity.apispace.IdiomExplainResponse
import cn.rtast.fancybot.util.Http
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.OneBotListener
class IdiomExplainCommand : BaseCommand() {
override val commandNames = listOf("成语解释")
private val apiSpaceUrl = "https://eolink.o.apispace.com/cyudac/api/v1/idioms/SearchIdiomsByPinyin/search"
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
message.reply("发送`成语解释 <成语>`就能解释这个成语的意思了")
return
}
try {
val idiom = args.joinToString(" ").trim()
val response = Http.get<IdiomExplainResponse>(
apiSpaceUrl,
mapOf("name" to idiom, "search_type" to "name"),
mapOf("X-APISpace-Token" to configManager.apiSpaceKey)
).data.first()
val msg = MessageChain.Builder()
.addText("成语: ${response.name} 的解释如下")
.addNewLine()
.addText("解释: ${response.explanation}")
.addNewLine(2)
.addText("出处: ${response.provenance}")
.addNewLine(2)
.addText("发音: ${response.sound}")
.build()
message.reply(msg)
} catch (_: Exception) {
message.reply("这个词似乎不是一个成语呢~~")
}
}
}
@@ -44,5 +44,6 @@ data class Config(
val cloudflareR2AccessKeyId: String, val cloudflareR2AccessKeyId: String,
val cloudflareR2SecretKey: String, val cloudflareR2SecretKey: String,
val cloudflareR2BucketName: String, val cloudflareR2BucketName: String,
val cloudflareR2PublicUrl: String val cloudflareR2PublicUrl: String,
val apiSpaceKey: String,
) )
@@ -0,0 +1,19 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/10
*/
package cn.rtast.fancybot.entity.apispace
data class IdiomExplainResponse(
val data: List<Data>
) {
data class Data(
val name: String,
val sound: String,
val explanation: String,
val provenance: String,
)
}
@@ -46,4 +46,5 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
val cloudflareR2SecretKey = config.cloudflareR2SecretKey val cloudflareR2SecretKey = config.cloudflareR2SecretKey
val cloudflareR2BucketName = config.cloudflareR2BucketName val cloudflareR2BucketName = config.cloudflareR2BucketName
val cloudflareR2PublicUrl = config.cloudflareR2PublicUrl val cloudflareR2PublicUrl = config.cloudflareR2PublicUrl
val apiSpaceKey = config.apiSpaceKey
} }
+2 -1
View File
@@ -36,5 +36,6 @@
"cloudflareR2AccessKeyId": "11111", "cloudflareR2AccessKeyId": "11111",
"cloudflareR2SecretKey": "222", "cloudflareR2SecretKey": "222",
"cloudflareR2BucketName": "fancybot", "cloudflareR2BucketName": "fancybot",
"cloudflareR2PublicUrl": "https://pub-2df243c8ff83452c9dcdd42c87b662a7.r2.dev" "cloudflareR2PublicUrl": "https://114514.com",
"apiSpaceKey": "114514"
} }