update
This commit is contained in:
11 files changed
+367
-7
No files matched your search
+16
@@ -0,0 +1,16 @@
|
||||
FROM nginx:alpine as nginx-server
|
||||
|
||||
RUN echo "server { listen 8000; location / { return 200 'OK'; add_header Content-Type text/plain; } }" \
|
||||
> /etc/nginx/conf.d/default.conf
|
||||
|
||||
FROM openjdk:17-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY build/libs/FancyBot-1.0-SNAPSHOT-all.jar /app/app.jar
|
||||
|
||||
COPY --from=nginx-server /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
CMD ["sh", "-c", "nginx && java -jar app.jar"]
|
||||
|
||||
EXPOSE 8000
|
||||
@@ -28,6 +28,8 @@ tasks.compileJava {
|
||||
|
||||
dependencies {
|
||||
implementation("cn.rtast:ROneBot:0.2.0")
|
||||
implementation("com.google.code.gson:gson:2.11.0")
|
||||
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.14")
|
||||
}
|
||||
|
||||
tasks.build {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
|
||||
val gson: Gson = GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.disableHtmlEscaping()
|
||||
.serializeNulls()
|
||||
.create()
|
||||
@@ -9,8 +9,10 @@ package cn.rtast.fancybot
|
||||
|
||||
import cn.rtast.fancybot.commands.EchoCommand
|
||||
import cn.rtast.fancybot.commands.JrrpCommand
|
||||
import cn.rtast.fancybot.commands.MusicCommand
|
||||
import cn.rtast.rob.ROneBotFactory
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
import org.java_websocket.WebSocket
|
||||
|
||||
@@ -20,12 +22,19 @@ class FancyBot : OBMessage {
|
||||
}
|
||||
}
|
||||
|
||||
val commands = listOf<BaseCommand>(
|
||||
EchoCommand(),
|
||||
JrrpCommand(),
|
||||
MusicCommand()
|
||||
)
|
||||
|
||||
fun main() {
|
||||
val address = System.getenv("WS_ADDRESS")
|
||||
val password = System.getenv("WS_PASSWORD")
|
||||
val fancyBot = FancyBot()
|
||||
val wsClient = ROneBotFactory.createClient("", "", fancyBot)
|
||||
val wsClient = ROneBotFactory.createClient(address, password, fancyBot)
|
||||
val commandManager = wsClient.commandManager
|
||||
commandManager.register(EchoCommand())
|
||||
commandManager.register(JrrpCommand())
|
||||
commands.forEach {
|
||||
commandManager.register(it)
|
||||
}
|
||||
}
|
||||
@@ -12,11 +12,18 @@ import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
import kotlin.random.Random
|
||||
|
||||
class JrrpCommand: BaseCommand() {
|
||||
override val commandName = "/jrrp"
|
||||
class JrrpCommand : BaseCommand() {
|
||||
override val commandName = "/今日人品"
|
||||
|
||||
override fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val score = Random.nextInt(0, 101)
|
||||
listener.sendGroupMessage(message.groupId, score.toString())
|
||||
val randomNumber = Random.nextInt(0, 101)
|
||||
val scoreDesc = when (randomNumber) {
|
||||
in 0..10 -> "人品不太好呢"
|
||||
in 30..50 -> "人品一般般"
|
||||
in 70..90 -> "人品还不错哦"
|
||||
in 90..100 -> "人品超级好呢!"
|
||||
else -> "人品还行哦"
|
||||
}
|
||||
listener.sendGroupMessage(message.groupId, scoreDesc)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.commands
|
||||
|
||||
import cn.rtast.fancybot.entity.Search
|
||||
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
|
||||
|
||||
class MusicCommand : BaseCommand() {
|
||||
override val commandName = "/点歌"
|
||||
|
||||
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"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
data class Search(
|
||||
val result: Result,
|
||||
) {
|
||||
data class Result(
|
||||
val songs: List<Song>
|
||||
)
|
||||
|
||||
data class Song(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val artists: List<Artist>
|
||||
)
|
||||
|
||||
data class Artist(
|
||||
val name: String
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
data class SearchedResult(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val artists: String,
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
data class SongUrl(
|
||||
val data: List<Data>
|
||||
) {
|
||||
data class Data(
|
||||
val url: String,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
|
||||
object Http {
|
||||
|
||||
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||
val jsonHeader = mapOf(
|
||||
"Content-Type" to "application/json; charset=utf-8",
|
||||
"Accept" to "application/json"
|
||||
)
|
||||
val okHttpClient = OkHttpClient()
|
||||
|
||||
fun buildParams(url: String, params: Map<String, Any>?): String {
|
||||
val paramsUrl = StringBuilder("$url?")
|
||||
params?.let {
|
||||
it.forEach { (key, value) ->
|
||||
paramsUrl.append("$key=$value&")
|
||||
}
|
||||
paramsUrl.dropLast(1)
|
||||
}
|
||||
return if (params != null) paramsUrl.toString() else url
|
||||
}
|
||||
|
||||
fun addHeaders(request: Request.Builder, headers: Map<String, String>?): Request.Builder {
|
||||
headers?.let {
|
||||
it.forEach { (key, value) ->
|
||||
request.addHeader(key, value)
|
||||
}
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
inline fun <reified T> executeRequest(request: Request): T {
|
||||
val result = okHttpClient.newCall(request).execute().body.string()
|
||||
return result.fromJson<T>()
|
||||
}
|
||||
|
||||
private fun executeRequest(request: Request): String {
|
||||
return okHttpClient.newCall(request).execute().body.string()
|
||||
}
|
||||
|
||||
fun executeRequestAsync(
|
||||
requestBuilder: Request.Builder,
|
||||
headers: Map<String, String>?,
|
||||
callback: (String) -> Unit
|
||||
) {
|
||||
this.addHeaders(requestBuilder, headers)
|
||||
okHttpClient.newCall(requestBuilder.build()).enqueue(object : Callback {
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
val responseBody = response.body.string()
|
||||
callback(responseBody)
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
callback(e.toString())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun get(
|
||||
url: String,
|
||||
params: Map<String, Any>? = null,
|
||||
headers: Map<String, String>? = null
|
||||
): String {
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.url(paramsUrl)
|
||||
.get()
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build())
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
inline fun <reified T> get(
|
||||
url: String,
|
||||
params: Map<String, Any>? = null,
|
||||
headers: Map<String, String>? = null
|
||||
): T {
|
||||
return get(url, params, headers).fromJson<T>()
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
inline fun <reified T> post(
|
||||
url: String,
|
||||
formBody: Map<String, String>? = null,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null
|
||||
): T {
|
||||
val body = FormBody.Builder()
|
||||
val paramsUrl = buildParams(url, params)
|
||||
formBody?.let {
|
||||
it.forEach { (key, value) ->
|
||||
body.add(key, value)
|
||||
}
|
||||
}
|
||||
val request = Request.Builder()
|
||||
.post(body.build())
|
||||
.url(paramsUrl)
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest<T>(headerRequest.build())
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
inline fun <reified T> post(
|
||||
url: String,
|
||||
jsonBody: String,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null
|
||||
): T {
|
||||
val result = post(url, jsonBody, headers, params)
|
||||
return result.fromJson<T>()
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun post(
|
||||
url: String,
|
||||
jsonBody: String,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null
|
||||
): String {
|
||||
val body = jsonBody.toRequestBody(jsonMediaType)
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.post(body)
|
||||
.url(paramsUrl)
|
||||
this.addHeaders(request, jsonHeader)
|
||||
val headerRequest = addHeaders(request, headers)
|
||||
return this.executeRequest(headerRequest.build())
|
||||
}
|
||||
|
||||
inline fun <reified T> getAsync(
|
||||
url: String,
|
||||
params: Map<String, Any>? = null,
|
||||
headers: Map<String, String>? = null,
|
||||
noinline callback: (T?) -> Unit
|
||||
) {
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.url(paramsUrl)
|
||||
.get()
|
||||
|
||||
executeRequestAsync(request, headers) { response ->
|
||||
try {
|
||||
callback(response.fromJson<T>())
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
callback(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> postAsync(
|
||||
url: String,
|
||||
jsonBody: String,
|
||||
headers: Map<String, String>? = null,
|
||||
params: Map<String, Any>? = null,
|
||||
noinline callback: (T?) -> Unit
|
||||
) {
|
||||
val body = jsonBody.toRequestBody(jsonMediaType)
|
||||
val paramsUrl = buildParams(url, params)
|
||||
val request = Request.Builder()
|
||||
.post(body)
|
||||
.url(paramsUrl)
|
||||
this.addHeaders(request, jsonHeader)
|
||||
|
||||
executeRequestAsync(request, headers) { response ->
|
||||
try {
|
||||
callback(response.fromJson<T>())
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
callback(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/27
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import cn.rtast.fancybot.gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
|
||||
fun Any.toJson(): String {
|
||||
return gson.toJson(this)
|
||||
}
|
||||
|
||||
inline fun <reified T> String.fromJson(): T {
|
||||
return gson.fromJson(this, T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T> String.fromArrayJson(): T {
|
||||
return gson.fromJson(this, object : TypeToken<T>() {}.type)
|
||||
}
|
||||
Reference in New Issue
Block a user