fix music command and add dockerfile
This commit is contained in:
7 files changed
+104
-25
No files matched your search
+17
@@ -0,0 +1,17 @@
|
|||||||
|
FROM gradle:8.9-jdk17 AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN gradle build --no-daemon
|
||||||
|
|
||||||
|
FROM openjdk:17-jdk-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder /app/build/libs/*.jar /app/app.jar
|
||||||
|
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
CMD ["java", "-jar", "/app/app.jar"]
|
||||||
+5
-1
@@ -27,7 +27,7 @@ tasks.compileJava {
|
|||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("cn.rtast:ROneBot:0.2.0")
|
implementation("cn.rtast:ROneBot:1.2.2")
|
||||||
implementation("com.google.code.gson:gson:2.11.0")
|
implementation("com.google.code.gson:gson:2.11.0")
|
||||||
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.14")
|
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.14")
|
||||||
}
|
}
|
||||||
@@ -37,6 +37,10 @@ tasks.build {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tasks.jar {
|
tasks.jar {
|
||||||
|
enabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.shadowJar {
|
||||||
manifest {
|
manifest {
|
||||||
attributes(
|
attributes(
|
||||||
"Main-Class" to "cn.rtast.fancybot.FancyBotKt",
|
"Main-Class" to "cn.rtast.fancybot.FancyBotKt",
|
||||||
|
|||||||
@@ -12,29 +12,43 @@ import cn.rtast.fancybot.commands.JrrpCommand
|
|||||||
import cn.rtast.fancybot.commands.MusicCommand
|
import cn.rtast.fancybot.commands.MusicCommand
|
||||||
import cn.rtast.rob.ROneBotFactory
|
import cn.rtast.rob.ROneBotFactory
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
import cn.rtast.rob.util.BaseCommand
|
|
||||||
import cn.rtast.rob.util.ob.OBMessage
|
import cn.rtast.rob.util.ob.OBMessage
|
||||||
|
import com.sun.net.httpserver.HttpServer
|
||||||
import org.java_websocket.WebSocket
|
import org.java_websocket.WebSocket
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
|
||||||
class FancyBot : OBMessage {
|
class FancyBot : OBMessage {
|
||||||
override fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
||||||
println(message.rawMessage)
|
println(message.rawMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val commands = listOf<BaseCommand>(
|
val commands = listOf(
|
||||||
EchoCommand(),
|
EchoCommand(),
|
||||||
JrrpCommand(),
|
JrrpCommand(),
|
||||||
MusicCommand()
|
MusicCommand()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun createFakeServer() {
|
||||||
|
val server = HttpServer.create(InetSocketAddress(8000), 0)
|
||||||
|
server.createContext("/") { exchange ->
|
||||||
|
val response = "Hello, Kotlin HTTP Server!"
|
||||||
|
exchange.sendResponseHeaders(200, response.toByteArray().size.toLong())
|
||||||
|
val os = exchange.responseBody
|
||||||
|
os.write(response.toByteArray())
|
||||||
|
os.close()
|
||||||
|
}
|
||||||
|
server.start()
|
||||||
|
println("Server is running on port 8000...")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val address = System.getenv("WS_ADDRESS")
|
val address = System.getenv("WS_ADDRESS")
|
||||||
val password = System.getenv("WS_PASSWORD")
|
val password = System.getenv("WS_PASSWORD")
|
||||||
val fancyBot = FancyBot()
|
val fancyBot = FancyBot()
|
||||||
val wsClient = ROneBotFactory.createClient(address, password, fancyBot)
|
val wsClient = ROneBotFactory.createClient(address, password, fancyBot)
|
||||||
val commandManager = wsClient.commandManager
|
val commandManager = wsClient.commandManager
|
||||||
commands.forEach {
|
commands.forEach { commandManager.register(it) }
|
||||||
commandManager.register(it)
|
createFakeServer()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ import cn.rtast.rob.util.ob.OBMessage
|
|||||||
class EchoCommand: BaseCommand() {
|
class EchoCommand: BaseCommand() {
|
||||||
override val commandName = "/echo"
|
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(" "))
|
listener.sendGroupMessage(message.groupId, args.joinToString(" "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ import kotlin.random.Random
|
|||||||
class JrrpCommand : BaseCommand() {
|
class JrrpCommand : BaseCommand() {
|
||||||
override val commandName = "/今日人品"
|
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 randomNumber = Random.nextInt(0, 101)
|
||||||
val scoreDesc = when (randomNumber) {
|
val scoreDesc = when (randomNumber) {
|
||||||
in 0..10 -> "人品不太好呢"
|
in 0..10 -> "人品不太好呢"
|
||||||
@@ -24,6 +24,6 @@ class JrrpCommand : BaseCommand() {
|
|||||||
in 90..100 -> "人品超级好呢!"
|
in 90..100 -> "人品超级好呢!"
|
||||||
else -> "人品还行哦"
|
else -> "人品还行哦"
|
||||||
}
|
}
|
||||||
listener.sendGroupMessage(message.groupId, scoreDesc)
|
listener.sendGroupMessage(message.groupId, "$scoreDesc($randomNumber)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,31 +8,74 @@
|
|||||||
package cn.rtast.fancybot.commands
|
package cn.rtast.fancybot.commands
|
||||||
|
|
||||||
import cn.rtast.fancybot.entity.Search
|
import cn.rtast.fancybot.entity.Search
|
||||||
|
import cn.rtast.fancybot.entity.SearchedResult
|
||||||
import cn.rtast.fancybot.entity.SongUrl
|
import cn.rtast.fancybot.entity.SongUrl
|
||||||
import cn.rtast.fancybot.util.Http
|
import cn.rtast.fancybot.util.Http
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.OBMessage
|
import cn.rtast.rob.util.ob.OBMessage
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
class MusicCommand : BaseCommand() {
|
class MusicCommand : BaseCommand() {
|
||||||
override val commandName = "/点歌"
|
override val commandName = "/music"
|
||||||
|
|
||||||
override fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
private val searchedResult = mutableMapOf<Long, List<SearchedResult>>()
|
||||||
val keyword = args.joinToString(" ")
|
|
||||||
Http.getAsync<Search>("https://music.api.yhdzz.cn/search?keywords=$keyword") {
|
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||||
val name = it!!.result.songs.first().name
|
searchedResult.forEach { (key, results) ->
|
||||||
val id = it.result.songs.first().id
|
val validResults = results.filter { result ->
|
||||||
val artists = it.result.songs.first().artists.joinToString(", ") { it.name }
|
Instant.now().epochSecond - result.timestamp <= 30
|
||||||
Http.getAsync<SongUrl>("https://music.api.yhdzz.cn/song/url?id=$id") { songUrl ->
|
}
|
||||||
|
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(
|
listener.sendGroupMessage(
|
||||||
message.groupId,
|
message.groupId,
|
||||||
"[CQ:music," +
|
"你上次的搜索结果还没用呢, 已经帮你把上次的搜索结果清除掉啦~"
|
||||||
"type=custom," +
|
|
||||||
"url=https://music.163.com/#/song?id=$id," +
|
|
||||||
"audio=${songUrl?.data?.first()?.url}," +
|
|
||||||
"title=《$name》-- $artists]\n"
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,4 +11,5 @@ data class SearchedResult(
|
|||||||
val id: Long,
|
val id: Long,
|
||||||
val name: String,
|
val name: String,
|
||||||
val artists: String,
|
val artists: String,
|
||||||
|
val timestamp: Long
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user