Persist command counter between restarts

This commit is contained in:
kageru 2019-08-03 09:03:20 +02:00
parent 1672b4c62a
commit 897457336d
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 31 additions and 4 deletions

View File

@ -1,9 +1,10 @@
package moe.kageru.kagebot
import moe.kageru.kagebot.persistence.Dao
import org.javacord.api.DiscordApi
import java.util.concurrent.atomic.AtomicInteger
object Globals {
lateinit var api: DiscordApi
val commandCounter: AtomicInteger = AtomicInteger(0)
val commandCounter: AtomicInteger = AtomicInteger(Dao.getCommandCounter())
}

View File

@ -22,8 +22,12 @@ object Kagebot {
return
}
for (command in Config.commands) {
// execute returns true if the command was actually executed (not denied due to permissions)
command.matches(readableMessageContent) && command.execute(this) && break
if (command.matches(readableMessageContent)) {
// 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 {
Log.info("Bot has been interrupted. Shutting down.")
Dao.setCommandCounter(Globals.commandCounter.get())
Dao.close()
api.disconnect()
})

View File

@ -6,11 +6,18 @@ import org.mapdb.Serializer
object Dao {
private val db = DBMaker.fileDB("kagebot.db").fileMmapEnable().closeOnJvmShutdown().make()
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>) {
prisoners[releaseTime] = roles.toLongArray()
}
fun setCommandCounter(count: Int) {
commands["total"] = count
}
fun getCommandCounter() = commands["total"] ?: 0
fun close() = db.close()
fun getAllTimeouts() = prisoners.keys

View File

@ -6,6 +6,7 @@ import io.kotlintest.specs.StringSpec
import io.mockk.every
import io.mockk.mockk
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.Globals
import moe.kageru.kagebot.Kagebot.process
import moe.kageru.kagebot.TestUtil
import moe.kageru.kagebot.TestUtil.embedToString
@ -23,7 +24,20 @@ import java.util.*
class CommandTest : StringSpec({
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(
"""
[[command]]