Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/lookup/NslookupCommand.kt
T

37 lines
1.2 KiB
Kotlin
Raw Normal View History

2024-09-06 18:03:14 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/6
*/
2024-09-13 12:44:10 +08:00
package cn.rtast.fancybot.commands.lookup
2024-09-06 18:03:14 +08:00
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
2024-09-25 18:18:28 +08:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
2024-09-06 18:03:14 +08:00
import java.net.InetAddress
2024-09-25 18:23:27 +08:00
import java.net.UnknownHostException
2024-09-06 18:03:14 +08:00
class NslookupCommand : BaseCommand() {
override val commandNames = listOf("/nslookup", "/ns")
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
2024-09-25 18:23:27 +08:00
val host = args.joinToString(" ")
try {
val msg = MessageChain.Builder()
.addReply(message.messageId)
.addText("域名: ${host}解析后的IP地址为: ")
.addText(withContext(Dispatchers.IO) { InetAddress.getByName(host) }.hostAddress)
.build()
listener.sendGroupMessage(message.groupId, msg)
} catch (_: UnknownHostException) {
message.reply("未知的主机名: $host")
} catch (e: Exception) {
message.reply("未知错误: ${e.message}")
}
2024-09-06 18:03:14 +08:00
}
}