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 { @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() 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() 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) } }