Pull commands out of config object
This commit is contained in:
parent
ac75239197
commit
4150187bbe
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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.")
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
})
|
|
@ -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)) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user