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