feat: fix bug and useing sqlite to store data
This commit is contained in:
13 files changed
+185
-89
No files matched your search
@@ -1,31 +0,0 @@
|
|||||||
name: Build FancyBot
|
|
||||||
on:
|
|
||||||
- push
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
java:
|
|
||||||
- 17
|
|
||||||
os:
|
|
||||||
- ubuntu-latest
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Validate gradle wrapper
|
|
||||||
uses: gradle/wrapper-validation-action@v1
|
|
||||||
- name: Setup jdk ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v1
|
|
||||||
with:
|
|
||||||
java-version: ${{ matrix.java }}
|
|
||||||
- name: Make Gradle wrapper executable
|
|
||||||
run: chmod +x ./gradlew
|
|
||||||
- name: Build
|
|
||||||
run: ./gradlew build
|
|
||||||
- name: Upload build artifacts
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: FancyBot
|
|
||||||
path: |
|
|
||||||
**/build/libs/*.jar
|
|
||||||
@@ -27,8 +27,12 @@ tasks.compileJava {
|
|||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
val exposedVersion = "0.53.0"
|
||||||
implementation(libs.rOneBot)
|
implementation(libs.rOneBot)
|
||||||
implementation(libs.okhttp)
|
implementation(libs.okhttp)
|
||||||
|
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
|
||||||
|
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
|
||||||
|
implementation("org.xerial:sqlite-jdbc:3.46.1.0")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.build {
|
tasks.build {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import cn.rtast.fancybot.commands.MyPointCommand
|
|||||||
import cn.rtast.fancybot.commands.SignCommand
|
import cn.rtast.fancybot.commands.SignCommand
|
||||||
import cn.rtast.fancybot.entity.enums.WSType
|
import cn.rtast.fancybot.entity.enums.WSType
|
||||||
import cn.rtast.fancybot.util.file.ConfigManager
|
import cn.rtast.fancybot.util.file.ConfigManager
|
||||||
|
import cn.rtast.fancybot.util.initDatabase
|
||||||
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.ob.OBMessage
|
import cn.rtast.rob.util.ob.OBMessage
|
||||||
@@ -25,6 +26,10 @@ class FancyBot : OBMessage {
|
|||||||
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
||||||
println(message.rawMessage)
|
println(message.rawMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun onWebsocketError(webSocket: WebSocket, ex: Exception) {
|
||||||
|
println(ex.printStackTrace())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val commands = listOf(
|
val commands = listOf(
|
||||||
@@ -39,7 +44,7 @@ val commands = listOf(
|
|||||||
|
|
||||||
val configManager = ConfigManager()
|
val configManager = ConfigManager()
|
||||||
|
|
||||||
fun main() {
|
suspend fun main() {
|
||||||
val fancyBot = FancyBot()
|
val fancyBot = FancyBot()
|
||||||
val workType = configManager.wsType
|
val workType = configManager.wsType
|
||||||
val accessToken = configManager.accessToken
|
val accessToken = configManager.accessToken
|
||||||
@@ -52,4 +57,5 @@ fun main() {
|
|||||||
}
|
}
|
||||||
val commandManager = rob.commandManager
|
val commandManager = rob.commandManager
|
||||||
commands.forEach { commandManager.register(it) }
|
commands.forEach { commandManager.register(it) }
|
||||||
|
initDatabase()
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.commands
|
package cn.rtast.fancybot.commands
|
||||||
|
|
||||||
import cn.rtast.fancybot.entity.fkxqs.FKXQS
|
import cn.rtast.fancybot.entity.FKXQS
|
||||||
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
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ class SignCommand : BaseCommand() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
val randomPoint = signManager.sign(message.sender.userId)
|
val randomPoint = signManager.sign(message.sender.userId)
|
||||||
listener.sendGroupMessage(message.groupId, "你获得了 $randomPoint 点分数!\n发送‘/我的分数’ 即可查看当前的分数")
|
listener.sendGroupMessage(message.groupId, "你获得了 $randomPoint 点分数!\n发送‘/我的点数’ 即可查看当前的点数")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyPointCommand: BaseCommand() {
|
class MyPointCommand: BaseCommand() {
|
||||||
override val commandNames = listOf("/我的分数", "/mp")
|
override val commandNames = listOf("/我的点数", "/mp")
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||||
val status = signManager.getStatus(message.sender.userId)
|
val status = signManager.getStatus(message.sender.userId)
|
||||||
@@ -36,7 +36,7 @@ class MyPointCommand: BaseCommand() {
|
|||||||
listener.sendGroupMessage(message.groupId, "你还没有签到过呢! 先发送‘/签到’再来查询吧~")
|
listener.sendGroupMessage(message.groupId, "你还没有签到过呢! 先发送‘/签到’再来查询吧~")
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
listener.sendGroupMessage(message.groupId, "你当前的分数为: ${status.points}")
|
listener.sendGroupMessage(message.groupId, "你当前的点数为: ${status.points}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package cn.rtast.fancybot.entity.fkxqs
|
package cn.rtast.fancybot.entity
|
||||||
|
|
||||||
data class FKXQS(
|
data class FKXQS(
|
||||||
val data: Data,
|
val data: Data,
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.entity
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.Table
|
||||||
|
|
||||||
|
object JrrpTable : Table("jrrp") {
|
||||||
|
val id = integer("id").autoIncrement()
|
||||||
|
val userId = long("userId").uniqueIndex()
|
||||||
|
val timestamp = long("timestamp")
|
||||||
|
val point = long("point")
|
||||||
|
|
||||||
|
override val primaryKey = PrimaryKey(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
data class JrrpRecord(
|
||||||
|
val id: Long,
|
||||||
|
val timestamp: Long,
|
||||||
|
var points: Long,
|
||||||
|
)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.entity
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.Table
|
||||||
|
|
||||||
|
object SignTable : Table("sign") {
|
||||||
|
val id = integer("id").autoIncrement()
|
||||||
|
val userId = long("userId").uniqueIndex()
|
||||||
|
val timestamp = long("timestamp")
|
||||||
|
val point = long("point")
|
||||||
|
|
||||||
|
override val primaryKey = PrimaryKey(id)
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/8/30
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.util
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.ROOT_PATH
|
||||||
|
import cn.rtast.fancybot.entity.JrrpTable
|
||||||
|
import cn.rtast.fancybot.entity.SignTable
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import org.jetbrains.exposed.sql.Database
|
||||||
|
import org.jetbrains.exposed.sql.SchemaUtils
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
|
|
||||||
|
suspend fun initDatabase() {
|
||||||
|
Database.connect("jdbc:sqlite:file:$ROOT_PATH/data.sqlite", "org.sqlite.JDBC")
|
||||||
|
suspendedTransaction {
|
||||||
|
SchemaUtils.createMissingTablesAndColumns(JrrpTable)
|
||||||
|
SchemaUtils.createMissingTablesAndColumns(SignTable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> suspendedTransaction(block: suspend () -> T): T =
|
||||||
|
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||||
@@ -8,13 +8,10 @@
|
|||||||
package cn.rtast.fancybot.util
|
package cn.rtast.fancybot.util
|
||||||
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.time.LocalDate
|
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
|
|
||||||
fun Long.isSameDay(other: Long): Boolean {
|
fun Long.isSameDay(otherTimestamp: Long, zoneId: ZoneId = ZoneId.systemDefault()): Boolean {
|
||||||
val currentDate = LocalDate.now()
|
val thisDate = Instant.ofEpochMilli(this).atZone(zoneId).toLocalDate()
|
||||||
val dateToCompare = Instant.ofEpochSecond(other)
|
val otherDate = Instant.ofEpochMilli(otherTimestamp).atZone(zoneId).toLocalDate()
|
||||||
.atZone(ZoneId.systemDefault())
|
return thisDate == otherDate
|
||||||
.toLocalDate()
|
|
||||||
return currentDate == dateToCompare
|
|
||||||
}
|
}
|
||||||
@@ -7,25 +7,57 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.util.file
|
package cn.rtast.fancybot.util.file
|
||||||
|
|
||||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
import cn.rtast.fancybot.entity.JrrpRecord
|
||||||
import cn.rtast.fancybot.util.JsonFileHandler
|
import cn.rtast.fancybot.entity.JrrpTable
|
||||||
|
import cn.rtast.fancybot.util.suspendedTransaction
|
||||||
import cn.rtast.fancybot.util.isSameDay
|
import cn.rtast.fancybot.util.isSameDay
|
||||||
|
import org.jetbrains.exposed.sql.insert
|
||||||
|
import org.jetbrains.exposed.sql.selectAll
|
||||||
|
import org.jetbrains.exposed.sql.update
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
class JrrpManager : JsonFileHandler<List<JrrpRecord>>("jrrp.json", listOf()) {
|
class JrrpManager {
|
||||||
|
|
||||||
fun isJrrped(id: Long): Boolean {
|
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||||
val allJrrp = this.readArray<List<JrrpRecord>>()
|
return suspendedTransaction {
|
||||||
val user = allJrrp.find { it.id == id } ?: return false
|
JrrpTable.selectAll().where { JrrpTable.userId eq id }.map {
|
||||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
JrrpRecord(
|
||||||
|
it[JrrpTable.userId],
|
||||||
|
it[JrrpTable.timestamp],
|
||||||
|
it[JrrpTable.point],
|
||||||
|
)
|
||||||
|
}.singleOrNull()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun jrrp(id: Long): Int {
|
suspend fun isJrrped(id: Long): Boolean {
|
||||||
val allJrrp = this.readArray<MutableList<JrrpRecord>>()
|
val record = getRecord(id)
|
||||||
allJrrp.removeAll { it.id == id }
|
return if (record != null) {
|
||||||
allJrrp.add(JrrpRecord(id, Instant.now().epochSecond, 0))
|
record.timestamp.isSameDay(Instant.now().epochSecond)
|
||||||
this.write(allJrrp)
|
} else {
|
||||||
return Random.nextInt(0, 101)
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun jrrp(id: Long): Long {
|
||||||
|
val randomPoint = Random.nextLong(0, 101)
|
||||||
|
if (getRecord(id) == null) {
|
||||||
|
suspendedTransaction {
|
||||||
|
JrrpTable.insert {
|
||||||
|
it[userId] = id
|
||||||
|
it[timestamp] = Instant.now().epochSecond
|
||||||
|
it[point] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val record = getRecord(id)
|
||||||
|
suspendedTransaction {
|
||||||
|
JrrpTable.update({ JrrpTable.userId eq id }) {
|
||||||
|
it[point] = record?.points!! + randomPoint
|
||||||
|
it[timestamp] = Instant.now().epochSecond
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return randomPoint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,33 +7,64 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.util.file
|
package cn.rtast.fancybot.util.file
|
||||||
|
|
||||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
import cn.rtast.fancybot.entity.JrrpRecord
|
||||||
import cn.rtast.fancybot.util.JsonFileHandler
|
import cn.rtast.fancybot.entity.SignTable
|
||||||
|
import cn.rtast.fancybot.entity.SignTable.point
|
||||||
|
import cn.rtast.fancybot.entity.SignTable.timestamp
|
||||||
|
import cn.rtast.fancybot.entity.SignTable.userId
|
||||||
|
import cn.rtast.fancybot.util.suspendedTransaction
|
||||||
import cn.rtast.fancybot.util.isSameDay
|
import cn.rtast.fancybot.util.isSameDay
|
||||||
|
import org.jetbrains.exposed.sql.insert
|
||||||
|
import org.jetbrains.exposed.sql.selectAll
|
||||||
|
import org.jetbrains.exposed.sql.update
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
class SignManager : JsonFileHandler<List<JrrpRecord>>("sign.json", listOf()) {
|
class SignManager {
|
||||||
|
|
||||||
fun isSigned(id: Long): Boolean {
|
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||||
val allSign = this.readArray<List<JrrpRecord>>()
|
return suspendedTransaction {
|
||||||
val user = allSign.find { it.id == id } ?: return false
|
SignTable.selectAll().where { userId eq id }.map {
|
||||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
JrrpRecord(
|
||||||
|
it[userId],
|
||||||
|
it[timestamp],
|
||||||
|
it[point],
|
||||||
|
)
|
||||||
|
}.singleOrNull()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sign(id: Long): Long {
|
suspend fun isSigned(id: Long): Boolean {
|
||||||
val allSign = this.readArray<MutableList<JrrpRecord>>()
|
val record = getRecord(id)
|
||||||
|
return if (record != null) {
|
||||||
|
record.timestamp.isSameDay(Instant.now().epochSecond)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun sign(id: Long): Long {
|
||||||
val randomPoint = Random.nextLong(0, 101)
|
val randomPoint = Random.nextLong(0, 101)
|
||||||
val current = allSign.find { it.id == id } ?: JrrpRecord(id, Instant.now().epochSecond, 0)
|
if (getRecord(id) == null) {
|
||||||
current.points = randomPoint + current.points
|
suspendedTransaction {
|
||||||
allSign.remove(current)
|
SignTable.insert {
|
||||||
allSign.add(current)
|
it[userId] = id
|
||||||
this.write(allSign)
|
it[timestamp] = Instant.now().epochSecond
|
||||||
return randomPoint + current.points
|
it[point] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val record = getRecord(id)
|
||||||
|
suspendedTransaction {
|
||||||
|
SignTable.update({ userId eq id }) {
|
||||||
|
it[point] = record?.points!! + randomPoint
|
||||||
|
it[timestamp] = Instant.now().epochSecond
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return randomPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getStatus(id: Long): JrrpRecord? {
|
suspend fun getStatus(id: Long): JrrpRecord? {
|
||||||
val allSign = this.readArray<List<JrrpRecord>>()
|
return getRecord(id)
|
||||||
return allSign.find { it.id == id }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user