chore: add exception catch

This commit is contained in:
2024-09-25 18:23:27 +08:00
parent 19e980be86
commit ecd50ffea6
2 files changed
+15 -11

No files matched your search

@@ -14,18 +14,24 @@ import cn.rtast.rob.util.ob.OneBotListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.net.InetAddress
import java.net.UnknownHostException
class NslookupCommand : BaseCommand() {
override val commandNames = listOf("/nslookup", "/ns")
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val host = args.joinToString(" ")
try {
val msg = MessageChain.Builder()
.addReply(message.messageId)
.addText("域名: ${args.first()}解析后的IP地址为: ")
.addText(withContext(Dispatchers.IO) {
InetAddress.getByName(args.first())
}.hostAddress)
.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}")
}
}
}
@@ -86,9 +86,7 @@ class AsciiArtCommand : BaseCommand() {
if (message.sender.userId in waitingList) {
if (message.message.any { it.type == ArrayMessageType.image }) {
val url = message.message.find { it.type == ArrayMessageType.image }!!.data.file!!
val bufferedImage = withContext(Dispatchers.IO) {
ImageIO.read(URI(url).toURL())
}
val bufferedImage = withContext(Dispatchers.IO) { ImageIO.read(URI(url).toURL()) }
val imageBase64 =
bufferedImage.convertToAscii().saveAsciiArtToImage(bufferedImage.width, bufferedImage.height)
val msg = MessageChain.Builder().addImage(imageBase64, true).build()