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
|
|
|
|
2024-09-26 05:20:10 +08:00
|
|
|
import cn.rtast.fancybot.annotations.CommandDescription
|
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
|
2024-09-19 11:06:40 +08:00
|
|
|
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
|
|
|
|
2024-09-26 05:20:10 +08:00
|
|
|
@CommandDescription("解析域名获取IP地址")
|
2024-09-06 18:03:14 +08:00
|
|
|
class NslookupCommand : BaseCommand() {
|
2024-09-26 05:20:10 +08:00
|
|
|
override val commandNames = listOf("/ns")
|
2024-09-06 18:03:14 +08:00
|
|
|
|
2024-09-19 11:06:40 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|