fix music command and add dockerfile
This commit is contained in:
7 files changed
+104
-25
No files matched your search
@@ -14,7 +14,7 @@ import cn.rtast.rob.util.ob.OBMessage
|
||||
class EchoCommand: BaseCommand() {
|
||||
override val commandName = "/echo"
|
||||
|
||||
override fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
listener.sendGroupMessage(message.groupId, args.joinToString(" "))
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import kotlin.random.Random
|
||||
class JrrpCommand : BaseCommand() {
|
||||
override val commandName = "/今日人品"
|
||||
|
||||
override fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val randomNumber = Random.nextInt(0, 101)
|
||||
val scoreDesc = when (randomNumber) {
|
||||
in 0..10 -> "人品不太好呢"
|
||||
@@ -24,6 +24,6 @@ class JrrpCommand : BaseCommand() {
|
||||
in 90..100 -> "人品超级好呢!"
|
||||
else -> "人品还行哦"
|
||||
}
|
||||
listener.sendGroupMessage(message.groupId, scoreDesc)
|
||||
listener.sendGroupMessage(message.groupId, "$scoreDesc($randomNumber)")
|
||||
}
|
||||
}
|
||||
@@ -8,30 +8,73 @@
|
||||
package cn.rtast.fancybot.commands
|
||||
|
||||
import cn.rtast.fancybot.entity.Search
|
||||
import cn.rtast.fancybot.entity.SearchedResult
|
||||
import cn.rtast.fancybot.entity.SongUrl
|
||||
import cn.rtast.fancybot.util.Http
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
import java.time.Instant
|
||||
|
||||
class MusicCommand : BaseCommand() {
|
||||
override val commandName = "/点歌"
|
||||
override val commandName = "/music"
|
||||
|
||||
override fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val keyword = args.joinToString(" ")
|
||||
Http.getAsync<Search>("https://music.api.yhdzz.cn/search?keywords=$keyword") {
|
||||
val name = it!!.result.songs.first().name
|
||||
val id = it.result.songs.first().id
|
||||
val artists = it.result.songs.first().artists.joinToString(", ") { it.name }
|
||||
Http.getAsync<SongUrl>("https://music.api.yhdzz.cn/song/url?id=$id") { songUrl ->
|
||||
listener.sendGroupMessage(
|
||||
message.groupId,
|
||||
"[CQ:music," +
|
||||
"type=custom," +
|
||||
"url=https://music.163.com/#/song?id=$id," +
|
||||
"audio=${songUrl?.data?.first()?.url}," +
|
||||
"title=《$name》-- $artists]\n"
|
||||
private val searchedResult = mutableMapOf<Long, List<SearchedResult>>()
|
||||
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
searchedResult.forEach { (key, results) ->
|
||||
val validResults = results.filter { result ->
|
||||
Instant.now().epochSecond - result.timestamp <= 30
|
||||
}
|
||||
if (validResults.isEmpty()) {
|
||||
searchedResult.remove(key)
|
||||
} else {
|
||||
searchedResult[key] = validResults
|
||||
}
|
||||
}
|
||||
val method = args.first()
|
||||
when (method) {
|
||||
"s", "ss", "搜索" -> {
|
||||
if (searchedResult.containsKey(message.sender.userId)) {
|
||||
searchedResult.remove(message.sender.userId)
|
||||
listener.sendGroupMessage(
|
||||
message.groupId,
|
||||
"你上次的搜索结果还没用呢, 已经帮你把上次的搜索结果清除掉啦~"
|
||||
)
|
||||
}
|
||||
val keyword = args.drop(1).joinToString(" ")
|
||||
val result = Http.get<Search>(
|
||||
"https://music.api.yhdzz.cn/search",
|
||||
mapOf("keywords" to keyword),
|
||||
)
|
||||
val tempResults = mutableListOf<SearchedResult>()
|
||||
result.result.songs.asSequence().take(5).forEach {
|
||||
val artists = it.artists.joinToString(", ") { it.name }
|
||||
tempResults.add(SearchedResult(it.id, it.name, artists, Instant.now().epochSecond))
|
||||
}
|
||||
searchedResult[message.sender.userId] = tempResults
|
||||
val stringResult = StringBuilder("搜索结果如下:\n")
|
||||
tempResults.forEachIndexed { index, searchedResult ->
|
||||
stringResult.append("| $index | 《${searchedResult.name}》-- ${searchedResult.artists}\n")
|
||||
}
|
||||
stringResult.append("输入/music p <序号> 播放\n你有30秒的时间进行操作")
|
||||
listener.sendGroupMessage(message.groupId, stringResult.toString())
|
||||
}
|
||||
|
||||
"p", "pp", "播放" -> {
|
||||
try {
|
||||
val index = args.drop(1).last().toInt()
|
||||
val searchedResultGet = searchedResult[message.sender.userId]!!
|
||||
val finalResult = searchedResultGet[index]
|
||||
val url =
|
||||
Http.get<SongUrl>("https://music.api.yhdzz.cn/song/url?id=${finalResult.id}").data.first().url
|
||||
val cqCode = "[CQ:music,type=custom,url=https://music.163.com/#/song?id=${finalResult.id}," +
|
||||
"audio=$url,title=《${finalResult.name}》-- ${finalResult.artists}]"
|
||||
listener.sendGroupMessage(message.groupId, cqCode)
|
||||
} catch (_: Exception) {
|
||||
listener.sendGroupMessage(message.groupId, "输入有误请重新搜索本次搜索结果已清除")
|
||||
}
|
||||
searchedResult.remove(message.sender.userId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user