feat: add some feature
This commit is contained in:
21 files changed
+333
-24
No files matched your search
@@ -7,6 +7,7 @@
|
||||
|
||||
package cn.rtast.fancybot
|
||||
|
||||
import cn.rtast.fancybot.entity.Config
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
|
||||
@@ -15,3 +16,9 @@ val gson: Gson = GsonBuilder()
|
||||
.disableHtmlEscaping()
|
||||
.serializeNulls()
|
||||
.create()
|
||||
|
||||
const val ROOT_PATH = "./data"
|
||||
|
||||
val DEFAULT_CONFIG = Config(
|
||||
cnmApi = "https://ncm.rtast.cn"
|
||||
)
|
||||
@@ -10,6 +10,9 @@ 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.fancybot.commands.MyPointCommand
|
||||
import cn.rtast.fancybot.commands.SignCommand
|
||||
import cn.rtast.fancybot.util.file.ConfigManager
|
||||
import cn.rtast.rob.ROneBotFactory
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
@@ -21,13 +24,11 @@ class FancyBot : OBMessage {
|
||||
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
||||
println(message.rawMessage)
|
||||
}
|
||||
}
|
||||
|
||||
val commands = listOf(
|
||||
EchoCommand(),
|
||||
JrrpCommand(),
|
||||
MusicCommand()
|
||||
)
|
||||
override suspend fun onWebsocketError(webSocket: WebSocket, ex: Exception) {
|
||||
println(ex.message)
|
||||
}
|
||||
}
|
||||
|
||||
fun createFakeServer() {
|
||||
val server = HttpServer.create(InetSocketAddress(8000), 0)
|
||||
@@ -42,6 +43,15 @@ fun createFakeServer() {
|
||||
println("Server is running on port 8000...")
|
||||
}
|
||||
|
||||
val commands = listOf(
|
||||
EchoCommand(),
|
||||
JrrpCommand(),
|
||||
MusicCommand(),
|
||||
SignCommand(),
|
||||
MyPointCommand()
|
||||
)
|
||||
|
||||
val configManager = ConfigManager()
|
||||
|
||||
fun main() {
|
||||
val address = System.getenv("WS_ADDRESS")
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.cmd
|
||||
|
||||
import cn.rtast.fancybot.DEFAULT_CONFIG
|
||||
import cn.rtast.fancybot.util.toJson
|
||||
import java.io.File
|
||||
|
||||
fun main() {
|
||||
val file = File("src/main/resources/config.json")
|
||||
file.writeText(DEFAULT_CONFIG.toJson())
|
||||
println("Generated default config")
|
||||
}
|
||||
@@ -7,23 +7,29 @@
|
||||
|
||||
package cn.rtast.fancybot.commands
|
||||
|
||||
import cn.rtast.fancybot.util.file.JrrpManager
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
import kotlin.random.Random
|
||||
|
||||
class JrrpCommand : BaseCommand() {
|
||||
override val commandName = "/今日人品"
|
||||
override val commandName = "/jrrp"
|
||||
|
||||
private val jrrpManager = JrrpManager()
|
||||
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val randomNumber = Random.nextInt(0, 101)
|
||||
val scoreDesc = when (randomNumber) {
|
||||
if (jrrpManager.isJrrped(message.sender.userId)) {
|
||||
listener.sendGroupMessage(message.groupId, "你今天已经今日人品过啦, 明天再来吧~")
|
||||
return
|
||||
}
|
||||
val randomPoint = jrrpManager.jrrp(message.sender.userId)
|
||||
val scoreDesc = when (randomPoint) {
|
||||
in 0..10 -> "人品不太好呢"
|
||||
in 30..50 -> "人品一般般"
|
||||
in 70..90 -> "人品还不错哦"
|
||||
in 90..100 -> "人品超级好呢!"
|
||||
else -> "人品还行哦"
|
||||
}
|
||||
listener.sendGroupMessage(message.groupId, "$scoreDesc($randomNumber)")
|
||||
listener.sendGroupMessage(message.groupId, "$scoreDesc($randomPoint)")
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,10 @@
|
||||
|
||||
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.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.util.Http
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
@@ -44,7 +45,7 @@ class MusicCommand : BaseCommand() {
|
||||
}
|
||||
val keyword = args.drop(1).joinToString(" ")
|
||||
val result = Http.get<Search>(
|
||||
"https://music.api.yhdzz.cn/search",
|
||||
"${configManager.ncmAPI}/search",
|
||||
mapOf("keywords" to keyword),
|
||||
)
|
||||
val tempResults = mutableListOf<SearchedResult>()
|
||||
@@ -67,7 +68,10 @@ class MusicCommand : BaseCommand() {
|
||||
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
|
||||
Http.get<SongUrl>(
|
||||
"${configManager.ncmAPI}/song/url",
|
||||
mapOf("id" to 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)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.commands
|
||||
|
||||
import cn.rtast.fancybot.util.file.SignManager
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
|
||||
private val signManager = SignManager()
|
||||
|
||||
class SignCommand : BaseCommand() {
|
||||
override val commandName = "/签到"
|
||||
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
if (signManager.isSigned(message.sender.userId)) {
|
||||
listener.sendGroupMessage(message.groupId, "你今天已经签到, 明天再来吧~")
|
||||
return
|
||||
}
|
||||
val randomPoint = signManager.sign(message.sender.userId)
|
||||
listener.sendGroupMessage(message.groupId, "你获得了 $randomPoint 点分数!\n发送‘/我的分数’ 即可查看当前的分数")
|
||||
}
|
||||
}
|
||||
|
||||
class MyPointCommand: BaseCommand() {
|
||||
override val commandName = "/我的分数"
|
||||
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val status = signManager.getStatus(message.sender.userId)
|
||||
if (status == null) {
|
||||
listener.sendGroupMessage(message.groupId, "你还没有签到过呢! 先发送‘/签到’再来查询吧~")
|
||||
return
|
||||
} else {
|
||||
listener.sendGroupMessage(message.groupId, "你当前的分数为: ${status.points}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
|
||||
data class Config(
|
||||
val cnmApi: String,
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity.jrrp
|
||||
|
||||
data class JrrpRecord(
|
||||
val id: Long,
|
||||
val timestamp: Long,
|
||||
var points: Long,
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity.jrrp
|
||||
|
||||
class SignRecord {
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
package cn.rtast.fancybot.entity.music
|
||||
|
||||
data class Search(
|
||||
val result: Result,
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
package cn.rtast.fancybot.entity.music
|
||||
|
||||
data class SearchedResult(
|
||||
val id: Long,
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity
|
||||
package cn.rtast.fancybot.entity.music
|
||||
|
||||
data class SongUrl(
|
||||
val data: List<Data>
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
fun Long.isSameDay(other: Long): Boolean {
|
||||
val currentDate = LocalDate.now()
|
||||
val dateToCompare = Instant.ofEpochSecond(other)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate()
|
||||
return currentDate == dateToCompare
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import cn.rtast.fancybot.ROOT_PATH
|
||||
import cn.rtast.fancybot.gson
|
||||
import java.io.File
|
||||
|
||||
abstract class JsonFileHandler<T> {
|
||||
|
||||
val file: File
|
||||
|
||||
constructor(filename: String) {
|
||||
this.file = File(ROOT_PATH, filename)
|
||||
initFile(null)
|
||||
}
|
||||
|
||||
constructor(filename: String, defaultConfig: T) {
|
||||
this.file = File(ROOT_PATH, filename)
|
||||
initFile(defaultConfig)
|
||||
}
|
||||
|
||||
private fun initFile(defaultConfig: T?) {
|
||||
val configPath = File(ROOT_PATH)
|
||||
configPath.mkdirs()
|
||||
|
||||
if (this.file.createNewFile()) {
|
||||
if (defaultConfig != null) {
|
||||
this.write(defaultConfig)
|
||||
} else {
|
||||
val resourceConfig = this::class.java.classLoader
|
||||
.getResourceAsStream(file.name)
|
||||
?.reader()?.readText()!!
|
||||
this.write(resourceConfig)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun write(data: String) {
|
||||
this.file.writeText(data)
|
||||
}
|
||||
|
||||
fun write(data: T) {
|
||||
this.write(gson.toJson(data))
|
||||
}
|
||||
|
||||
inline fun <reified T> read(): T {
|
||||
val content = this.file.readText()
|
||||
return content.fromJson()
|
||||
}
|
||||
|
||||
inline fun <reified T> readArray(): T {
|
||||
val content = this.file.readText()
|
||||
return content.fromArrayJson<T>()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.Config
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
|
||||
class ConfigManager : JsonFileHandler<Config>("config.json") {
|
||||
|
||||
val ncmAPI get() = this.read<Config>().cnmApi
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import java.time.Instant
|
||||
import kotlin.random.Random
|
||||
|
||||
class JrrpManager : JsonFileHandler<List<JrrpRecord>>("jrrp.json", listOf()) {
|
||||
|
||||
fun isJrrped(id: Long): Boolean {
|
||||
val allJrrp = this.readArray<List<JrrpRecord>>()
|
||||
val user = allJrrp.find { it.id == id } ?: return false
|
||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
}
|
||||
|
||||
fun jrrp(id: Long): Int {
|
||||
val allJrrp = this.readArray<MutableList<JrrpRecord>>()
|
||||
allJrrp.removeAll { it.id == id }
|
||||
allJrrp.add(JrrpRecord(id, Instant.now().epochSecond, 0))
|
||||
this.write(allJrrp)
|
||||
return Random.nextInt(0, 101)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import java.time.Instant
|
||||
import kotlin.random.Random
|
||||
|
||||
class SignManager : JsonFileHandler<List<JrrpRecord>>("sign.json", listOf()) {
|
||||
|
||||
fun isSigned(id: Long): Boolean {
|
||||
val allSign = this.readArray<List<JrrpRecord>>()
|
||||
val user = allSign.find { it.id == id } ?: return false
|
||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
}
|
||||
|
||||
fun sign(id: Long): Long {
|
||||
val allSign = this.readArray<MutableList<JrrpRecord>>()
|
||||
val randomPoint = Random.nextLong(0, 101)
|
||||
val current = allSign.find { it.id == id } ?: JrrpRecord(id, Instant.now().epochSecond, 0)
|
||||
current.points = randomPoint + current.points
|
||||
allSign.remove(current)
|
||||
allSign.add(current)
|
||||
this.write(allSign)
|
||||
return randomPoint + current.points
|
||||
}
|
||||
|
||||
fun getStatus(id: Long): JrrpRecord? {
|
||||
val allSign = this.readArray<List<JrrpRecord>>()
|
||||
return allSign.find { it.id == id }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user