feat: fix bug and useing sqlite to store data
This commit is contained in:
13 files changed
+185
-89
No files matched your search
@@ -16,6 +16,7 @@ import cn.rtast.fancybot.commands.MyPointCommand
|
||||
import cn.rtast.fancybot.commands.SignCommand
|
||||
import cn.rtast.fancybot.entity.enums.WSType
|
||||
import cn.rtast.fancybot.util.file.ConfigManager
|
||||
import cn.rtast.fancybot.util.initDatabase
|
||||
import cn.rtast.rob.ROneBotFactory
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.ob.OBMessage
|
||||
@@ -25,6 +26,10 @@ class FancyBot : OBMessage {
|
||||
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
|
||||
println(message.rawMessage)
|
||||
}
|
||||
|
||||
override suspend fun onWebsocketError(webSocket: WebSocket, ex: Exception) {
|
||||
println(ex.printStackTrace())
|
||||
}
|
||||
}
|
||||
|
||||
val commands = listOf(
|
||||
@@ -39,7 +44,7 @@ val commands = listOf(
|
||||
|
||||
val configManager = ConfigManager()
|
||||
|
||||
fun main() {
|
||||
suspend fun main() {
|
||||
val fancyBot = FancyBot()
|
||||
val workType = configManager.wsType
|
||||
val accessToken = configManager.accessToken
|
||||
@@ -52,4 +57,5 @@ fun main() {
|
||||
}
|
||||
val commandManager = rob.commandManager
|
||||
commands.forEach { commandManager.register(it) }
|
||||
initDatabase()
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
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.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.BaseCommand
|
||||
|
||||
@@ -23,12 +23,12 @@ class SignCommand : BaseCommand() {
|
||||
return
|
||||
}
|
||||
val randomPoint = signManager.sign(message.sender.userId)
|
||||
listener.sendGroupMessage(message.groupId, "你获得了 $randomPoint 点分数!\n发送‘/我的分数’ 即可查看当前的分数")
|
||||
listener.sendGroupMessage(message.groupId, "你获得了 $randomPoint 点分数!\n发送‘/我的点数’ 即可查看当前的点数")
|
||||
}
|
||||
}
|
||||
|
||||
class MyPointCommand: BaseCommand() {
|
||||
override val commandNames = listOf("/我的分数", "/mp")
|
||||
override val commandNames = listOf("/我的点数", "/mp")
|
||||
|
||||
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
|
||||
val status = signManager.getStatus(message.sender.userId)
|
||||
@@ -36,7 +36,7 @@ class MyPointCommand: BaseCommand() {
|
||||
listener.sendGroupMessage(message.groupId, "你还没有签到过呢! 先发送‘/签到’再来查询吧~")
|
||||
return
|
||||
} 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(
|
||||
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
|
||||
|
||||
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
|
||||
fun Long.isSameDay(otherTimestamp: Long, zoneId: ZoneId = ZoneId.systemDefault()): Boolean {
|
||||
val thisDate = Instant.ofEpochMilli(this).atZone(zoneId).toLocalDate()
|
||||
val otherDate = Instant.ofEpochMilli(otherTimestamp).atZone(zoneId).toLocalDate()
|
||||
return thisDate == otherDate
|
||||
}
|
||||
@@ -7,25 +7,57 @@
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
import cn.rtast.fancybot.entity.JrrpRecord
|
||||
import cn.rtast.fancybot.entity.JrrpTable
|
||||
import cn.rtast.fancybot.util.suspendedTransaction
|
||||
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 kotlin.random.Random
|
||||
|
||||
class JrrpManager : JsonFileHandler<List<JrrpRecord>>("jrrp.json", listOf()) {
|
||||
class JrrpManager {
|
||||
|
||||
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)
|
||||
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||
return suspendedTransaction {
|
||||
JrrpTable.selectAll().where { JrrpTable.userId eq id }.map {
|
||||
JrrpRecord(
|
||||
it[JrrpTable.userId],
|
||||
it[JrrpTable.timestamp],
|
||||
it[JrrpTable.point],
|
||||
)
|
||||
}.singleOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
suspend fun isJrrped(id: Long): Boolean {
|
||||
val record = getRecord(id)
|
||||
return if (record != null) {
|
||||
record.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
} else {
|
||||
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
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
import cn.rtast.fancybot.entity.JrrpRecord
|
||||
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 org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import java.time.Instant
|
||||
import kotlin.random.Random
|
||||
|
||||
class SignManager : JsonFileHandler<List<JrrpRecord>>("sign.json", listOf()) {
|
||||
class SignManager {
|
||||
|
||||
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)
|
||||
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||
return suspendedTransaction {
|
||||
SignTable.selectAll().where { userId eq id }.map {
|
||||
JrrpRecord(
|
||||
it[userId],
|
||||
it[timestamp],
|
||||
it[point],
|
||||
)
|
||||
}.singleOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
fun sign(id: Long): Long {
|
||||
val allSign = this.readArray<MutableList<JrrpRecord>>()
|
||||
suspend fun isSigned(id: Long): Boolean {
|
||||
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 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
|
||||
if (getRecord(id) == null) {
|
||||
suspendedTransaction {
|
||||
SignTable.insert {
|
||||
it[userId] = id
|
||||
it[timestamp] = Instant.now().epochSecond
|
||||
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? {
|
||||
val allSign = this.readArray<List<JrrpRecord>>()
|
||||
return allSign.find { it.id == id }
|
||||
suspend fun getStatus(id: Long): JrrpRecord? {
|
||||
return getRecord(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user