feat: add auto download image

This commit is contained in:
2024-09-20 22:23:43 +08:00
parent aa874ab95e
commit 862de4dacd
1 file changed
+20 -1
+20 -1
View File
@@ -65,11 +65,17 @@ import cn.rtast.rob.entity.GroupRevokeMessage
import cn.rtast.rob.enums.ArrayMessageType import cn.rtast.rob.enums.ArrayMessageType
import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OneBotListener import cn.rtast.rob.util.ob.OneBotListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.net.URI
import java.time.Instant import java.time.Instant
class FancyBot : OneBotListener { class FancyBot : OneBotListener {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
override suspend fun onGroupMessage(message: GroupMessage, json: String) { override suspend fun onGroupMessage(message: GroupMessage, json: String) {
val sender = message.sender.nickname val sender = message.sender.nickname
val senderId = message.sender.userId val senderId = message.sender.userId
@@ -78,6 +84,19 @@ class FancyBot : OneBotListener {
val messageId = message.messageId val messageId = message.messageId
println("$sender($senderId: $groupId >>> $messageId): $msg") println("$sender($senderId: $groupId >>> $messageId): $msg")
coroutineScope.launch {
message.message.forEach {
if (it.type == ArrayMessageType.image) {
val filename = it.data.file!!.split("=").last() + ".png"
URI(it.data.file!!).toURL().openConnection().inputStream.use { input ->
File("./files/images/$filename").outputStream().use { output ->
output.write(input.readBytes())
}
}
}
}
}
AsciiArtCommand.callback(message) AsciiArtCommand.callback(message)
if (message.rawMessage.startsWith("https://github.com/") || message.rawMessage.startsWith("git@github.com:")) { if (message.rawMessage.startsWith("https://github.com/") || message.rawMessage.startsWith("git@github.com:")) {
@@ -202,7 +221,7 @@ val commands = listOf(
val START_UP_TIME = Instant.now().epochSecond val START_UP_TIME = Instant.now().epochSecond
fun initFilesDir() { fun initFilesDir() {
File("./files").also { it.mkdir() } File("./files/images").also { it.mkdirs() }
} }
suspend fun main() { suspend fun main() {