Pull commands out of config object

This commit is contained in:
kageru 2019-07-07 14:19:13 +02:00
parent ac75239197
commit 4150187bbe
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 9 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package moe.kageru.kagebot
import moe.kageru.kagebot.command.Command
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.SystemConfig
import org.javacord.api.DiscordApi
@ -10,6 +11,7 @@ object Globals {
lateinit var server: Server
lateinit var api: DiscordApi
lateinit var config: Config
lateinit var commands: List<Command>
lateinit var systemConfig: SystemConfig
var commandCounter: AtomicInteger = AtomicInteger(0)
}

View File

@ -22,7 +22,7 @@ object Kagebot {
}
return
}
for (command in Globals.config.commands) {
for (command in Globals.commands) {
if (command.matches(event.messageContent)) {
command.execute(event)
break

View File

@ -11,13 +11,12 @@ class Config(rawConfig: RawConfig) {
?: throw IllegalArgumentException("No [system] block in config.")
var localization: Localization = rawConfig.localization?.let(::Localization)
?: throw IllegalArgumentException("No [localization] block in config.")
var commands: List<Command>
var features: Features
init {
Globals.systemConfig = system
Globals.server = api.getServerById(system.serverId).orElseThrow()
this.commands = rawConfig.commands?.map(::Command) ?: emptyList()
Globals.commands = rawConfig.commands?.map(::Command) ?: emptyList()
Globals.config = this
this.features = rawConfig.features?.let(::Features) ?: Features.NONE
}
@ -27,7 +26,7 @@ class Config(rawConfig: RawConfig) {
}
fun reloadCommands(rawConfig: RawConfig) {
this.commands = rawConfig.commands?.map(::Command)
Globals.commands = rawConfig.commands?.map(::Command)?.toMutableList()
?: throw IllegalArgumentException("No commands found in config.")
}

View File

@ -8,6 +8,7 @@ class ConfigTest : StringSpec({
TestUtil.prepareTestEnvironment()
"should properly parse test config" {
Globals.config shouldNotBe null
Globals.config.commands shouldBe emptyList()
Globals.systemConfig shouldNotBe null
Globals.commands shouldBe emptyList()
}
})

View File

@ -86,11 +86,11 @@ object TestUtil {
}
fun <R> withCommands(config: String, test: (() -> R)) {
val oldCmds = Globals.config.commands
val oldCmds = Globals.commands
val rawConfig = RawConfig.readFromString(config)
Globals.config.reloadCommands(rawConfig)
test()
Globals.config.commands = oldCmds
Globals.commands = oldCmds
}
fun <R> withLocalization(config: String, test: (() -> R)) {