
- 移除 ImageUtil 相关代码,改用 bot API 发送图片- 添加 JSON 和 Channel 引用,用于构造发送消息的参数- 更新发送逻辑,使用 sendFile 和 send 方法发送图片
71 lines
2.5 KiB
Kotlin
71 lines
2.5 KiB
Kotlin
package info.alinadace.sakuramiki.service.randomphoto
|
|
|
|
import com.alibaba.fastjson.JSON
|
|
import com.mavis.service.AlistService
|
|
import info.alinadace.sakuramiki.annotation.BotFunction
|
|
import info.alinadace.sakuramiki.service.Service
|
|
import info.alinadace.sakuramiki.service.randomphoto.domain.FileList
|
|
import info.alinadace.sakuramiki.service.randomphoto.domain.SingleFile
|
|
import io.github.kloping.qqbot.api.v2.FriendMessageEvent
|
|
import io.github.kloping.qqbot.api.v2.GroupMessageEvent
|
|
import io.github.kloping.qqbot.api.v2.MessageV2Event
|
|
import io.github.kloping.qqbot.entities.ex.PlainText
|
|
import io.github.kloping.qqbot.entities.qqpd.Channel
|
|
import jakarta.annotation.Resource
|
|
|
|
/**
|
|
* @author Kane
|
|
* @since 2024/11/11 19:26
|
|
*/
|
|
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
|
|
class RandomPhotoService : Service<MessageV2Event> {
|
|
|
|
@Resource
|
|
lateinit var alistService: AlistService
|
|
|
|
/**
|
|
* 服务入口
|
|
*/
|
|
override fun entrance(event: MessageV2Event): Boolean {
|
|
val chain = event.message
|
|
if (chain.size == 1 && chain[0] is PlainText && chain[0].toString() == "#photo") {
|
|
return true
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* 服务行为
|
|
*/
|
|
override fun active(event: MessageV2Event) {
|
|
val fileList = alistService.getAlistFileList("/VRChat-Photo")
|
|
val files = FileList.fromJson(fileList)
|
|
if (files.code != 200L) {
|
|
event.send("文件列表获取失败")
|
|
}
|
|
val content = files.data.content.filter { x -> !x.isDir }
|
|
if (content.isEmpty()) {
|
|
event.send("暂无照片")
|
|
return
|
|
}
|
|
val randomFile = content.random()
|
|
val fileInfo = alistService.getAlistFileInfo("/VRChat-Photo/" + randomFile.name, "")
|
|
val file = SingleFile.fromJson(fileInfo)
|
|
if (file.code != 200L) {
|
|
event.send("文件获取失败")
|
|
return
|
|
}
|
|
val map = HashMap<String, Any>()
|
|
map["file_type"] = 1
|
|
map["url"] = file.data.rawURL
|
|
map["srv_send_msg"] = false
|
|
val sendFile = event.bot.userBaseV2.sendFile(event.sender.cid, JSON.toJSONString(map), Channel.SEND_MESSAGE_HEADERS)
|
|
val message = HashMap<String, Any>()
|
|
message["content"] = " "
|
|
message["msg_type"] = 7
|
|
message["media"] = sendFile.file_info
|
|
message["event_id"] = event.id
|
|
event.bot.userBaseV2.send(event.sender.cid, JSON.toJSONString(message), Channel.SEND_MESSAGE_HEADERS)
|
|
}
|
|
}
|