Persist command counter between restarts
This commit is contained in:
parent
1672b4c62a
commit
897457336d
|
@ -1,9 +1,10 @@
|
||||||
package moe.kageru.kagebot
|
package moe.kageru.kagebot
|
||||||
|
|
||||||
|
import moe.kageru.kagebot.persistence.Dao
|
||||||
import org.javacord.api.DiscordApi
|
import org.javacord.api.DiscordApi
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
|
||||||
object Globals {
|
object Globals {
|
||||||
lateinit var api: DiscordApi
|
lateinit var api: DiscordApi
|
||||||
val commandCounter: AtomicInteger = AtomicInteger(0)
|
val commandCounter: AtomicInteger = AtomicInteger(Dao.getCommandCounter())
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,12 @@ object Kagebot {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for (command in Config.commands) {
|
for (command in Config.commands) {
|
||||||
// execute returns true if the command was actually executed (not denied due to permissions)
|
if (command.matches(readableMessageContent)) {
|
||||||
command.matches(readableMessageContent) && command.execute(this) && break
|
// only break if we have the permissions to execute this command, else keep searching
|
||||||
|
if (command.execute(this)) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +51,7 @@ object Kagebot {
|
||||||
}
|
}
|
||||||
Runtime.getRuntime().addShutdownHook(Thread {
|
Runtime.getRuntime().addShutdownHook(Thread {
|
||||||
Log.info("Bot has been interrupted. Shutting down.")
|
Log.info("Bot has been interrupted. Shutting down.")
|
||||||
|
Dao.setCommandCounter(Globals.commandCounter.get())
|
||||||
Dao.close()
|
Dao.close()
|
||||||
api.disconnect()
|
api.disconnect()
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,11 +6,18 @@ import org.mapdb.Serializer
|
||||||
object Dao {
|
object Dao {
|
||||||
private val db = DBMaker.fileDB("kagebot.db").fileMmapEnable().closeOnJvmShutdown().make()
|
private val db = DBMaker.fileDB("kagebot.db").fileMmapEnable().closeOnJvmShutdown().make()
|
||||||
private val prisoners = db.hashMap("timeout", Serializer.LONG, Serializer.LONG_ARRAY).createOrOpen()
|
private val prisoners = db.hashMap("timeout", Serializer.LONG, Serializer.LONG_ARRAY).createOrOpen()
|
||||||
|
private val commands = db.hashMap("commands", Serializer.STRING, Serializer.INTEGER).createOrOpen()
|
||||||
|
|
||||||
fun saveTimeout(releaseTime: Long, roles: List<Long>) {
|
fun saveTimeout(releaseTime: Long, roles: List<Long>) {
|
||||||
prisoners[releaseTime] = roles.toLongArray()
|
prisoners[releaseTime] = roles.toLongArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setCommandCounter(count: Int) {
|
||||||
|
commands["total"] = count
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCommandCounter() = commands["total"] ?: 0
|
||||||
|
|
||||||
fun close() = db.close()
|
fun close() = db.close()
|
||||||
|
|
||||||
fun getAllTimeouts() = prisoners.keys
|
fun getAllTimeouts() = prisoners.keys
|
||||||
|
|
|
@ -6,6 +6,7 @@ import io.kotlintest.specs.StringSpec
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import moe.kageru.kagebot.config.Config
|
import moe.kageru.kagebot.config.Config
|
||||||
|
import moe.kageru.kagebot.Globals
|
||||||
import moe.kageru.kagebot.Kagebot.process
|
import moe.kageru.kagebot.Kagebot.process
|
||||||
import moe.kageru.kagebot.TestUtil
|
import moe.kageru.kagebot.TestUtil
|
||||||
import moe.kageru.kagebot.TestUtil.embedToString
|
import moe.kageru.kagebot.TestUtil.embedToString
|
||||||
|
@ -23,7 +24,20 @@ import java.util.*
|
||||||
|
|
||||||
class CommandTest : StringSpec({
|
class CommandTest : StringSpec({
|
||||||
prepareTestEnvironment()
|
prepareTestEnvironment()
|
||||||
"should match prefix command" {
|
"should increment command counter" {
|
||||||
|
withCommands(
|
||||||
|
"""
|
||||||
|
[[command]]
|
||||||
|
trigger = "!ping"
|
||||||
|
response = "pong"
|
||||||
|
""".trimIndent()
|
||||||
|
) {
|
||||||
|
val before = Globals.commandCounter.get()
|
||||||
|
testMessageSuccess("!ping", "pong")
|
||||||
|
Globals.commandCounter.get() shouldBe (before + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"should match prefix command" {
|
||||||
withCommands(
|
withCommands(
|
||||||
"""
|
"""
|
||||||
[[command]]
|
[[command]]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user