feat: add qq music api search
This commit is contained in:
9 files changed
+194
-4
No files matched your search
@@ -11,12 +11,20 @@ import cn.rtast.fancybot.configManager
|
||||
import cn.rtast.fancybot.entity.music.Search
|
||||
import cn.rtast.fancybot.entity.music.SearchedResult
|
||||
import cn.rtast.fancybot.entity.music.SongUrl
|
||||
import cn.rtast.fancybot.entity.music.qq.CoverImage
|
||||
import cn.rtast.fancybot.entity.music.qq.QMSearchResult
|
||||
import cn.rtast.fancybot.entity.music.qq.QQMSearch
|
||||
import cn.rtast.fancybot.util.Http
|
||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||
import cn.rtast.fancybot.util.str.formatToMinutes
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.enums.MusicShareType
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.MessageChain
|
||||
import cn.rtast.rob.util.ob.NodeMessageChain
|
||||
import cn.rtast.rob.util.ob.OneBotListener
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URI
|
||||
import java.time.Instant
|
||||
|
||||
class MusicCommand : BaseCommand() {
|
||||
@@ -121,7 +129,7 @@ class MusicCommand : BaseCommand() {
|
||||
}
|
||||
|
||||
val keyword = args.joinToString(" ")
|
||||
val result = searchSong(keyword).result.songs.asSequence().first().id
|
||||
val result = searchSong(keyword).result.songs.first().id
|
||||
val msg = MessageChain.Builder()
|
||||
.addMusicShare(MusicShareType.Netease, result.toString())
|
||||
.build()
|
||||
@@ -151,4 +159,94 @@ class MusicPlayUrlCommand : BaseCommand() {
|
||||
.build()
|
||||
listener.sendGroupMessage(message.groupId, msg)
|
||||
}
|
||||
}
|
||||
|
||||
class QQMusicCommand : BaseCommand() {
|
||||
override val commandNames = listOf("qm", "/qqm", "qqm", "/qm", "q点歌")
|
||||
|
||||
private val qqMusicApiUrl = configManager.qqMusicApiUrl
|
||||
|
||||
private val recordMap = mutableMapOf<Long, List<QMSearchResult>>()
|
||||
|
||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||
if (args.isEmpty()) {
|
||||
message.reply("发送`/qm <关键词>`即可搜索qq音乐的音乐哦")
|
||||
return
|
||||
}
|
||||
this.recordMap.forEach { (key, results) ->
|
||||
val validResults = results.filter { result ->
|
||||
Instant.now().epochSecond - result.timestamp <= 30
|
||||
}
|
||||
if (validResults.isEmpty()) {
|
||||
recordMap.remove(key)
|
||||
} else {
|
||||
recordMap[key] = validResults
|
||||
}
|
||||
}
|
||||
val method = args.first()
|
||||
when (method) {
|
||||
"p" -> {
|
||||
try {
|
||||
val index = args.last().toInt()
|
||||
val result = recordMap[message.sender.userId]!![index]
|
||||
val msg = MessageChain.Builder()
|
||||
.addMusicShare(MusicShareType.QQ, result.id.toString())
|
||||
.build()
|
||||
listener.sendGroupMessage(message.groupId, msg)
|
||||
} catch (_: Exception) {
|
||||
message.reply("回复错误已清除本次搜索结果")
|
||||
}
|
||||
recordMap.remove(message.sender.userId)
|
||||
}
|
||||
|
||||
else -> {
|
||||
try {
|
||||
val keyword = args.joinToString(" ")
|
||||
val response = Http.get<QQMSearch>(
|
||||
"$qqMusicApiUrl/getSearchByKey",
|
||||
mapOf("key" to keyword)
|
||||
).response.data.song.list
|
||||
val tempResultList = mutableListOf<QMSearchResult>()
|
||||
response.forEach {
|
||||
val coverUrl = Http.get<CoverImage>(
|
||||
"$qqMusicApiUrl/getImageUrl",
|
||||
mapOf("id" to it.albumMid)
|
||||
)
|
||||
val tempSearchResult = QMSearchResult(
|
||||
it.songId, it.songName, it.singer.joinToString(", ") { join -> join.name },
|
||||
it.interval, it.songMid, Instant.now().epochSecond, coverUrl.response.data.imageUrl
|
||||
)
|
||||
tempResultList.add(tempSearchResult)
|
||||
}
|
||||
recordMap[message.sender.userId] = tempResultList
|
||||
val nodeMsg = NodeMessageChain.Builder()
|
||||
val headerMsg = MessageChain.Builder()
|
||||
.addText("发送 `qm p <序号>`即可返回对应的歌曲")
|
||||
.addNewLine()
|
||||
.addText("你有30秒的时间进行操作")
|
||||
.build()
|
||||
nodeMsg.addMessageChain(headerMsg, message.sender.userId)
|
||||
tempResultList.forEachIndexed { index, it ->
|
||||
try {
|
||||
val imageBase64 = URI(it.albumUrl).toURL().readBytes().encodeToBase64()
|
||||
val msg = MessageChain.Builder()
|
||||
.addImage(imageBase64, true)
|
||||
.addText("序号: $index")
|
||||
.addNewLine()
|
||||
.addText("歌曲名: ${it.name}")
|
||||
.addNewLine()
|
||||
.addText("歌手: ${it.singers}")
|
||||
.addNewLine()
|
||||
.addText("时长: ${it.interval.formatToMinutes()}")
|
||||
.build()
|
||||
nodeMsg.addMessageChain(msg, message.sender.userId)
|
||||
} catch (_: FileNotFoundException) {
|
||||
}
|
||||
}
|
||||
message.reply(nodeMsg.build())
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user