diff --git a/src/main/kotlin/moe/kageru/kagebot/Globals.kt b/src/main/kotlin/moe/kageru/kagebot/Globals.kt index fecccf2..612a65d 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Globals.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Globals.kt @@ -1,7 +1,7 @@ package moe.kageru.kagebot import moe.kageru.kagebot.command.Command -import moe.kageru.kagebot.config.Config +import moe.kageru.kagebot.config.Localization import moe.kageru.kagebot.config.SystemConfig import moe.kageru.kagebot.features.Features import org.javacord.api.DiscordApi @@ -11,9 +11,9 @@ import java.util.concurrent.atomic.AtomicInteger object Globals { lateinit var server: Server lateinit var api: DiscordApi - lateinit var config: Config lateinit var commands: List lateinit var systemConfig: SystemConfig lateinit var features: Features - var commandCounter: AtomicInteger = AtomicInteger(0) + lateinit var localization: Localization + val commandCounter: AtomicInteger = AtomicInteger(0) } diff --git a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt index d1cfd07..1eed6b1 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Kagebot.kt @@ -2,7 +2,7 @@ package moe.kageru.kagebot import moe.kageru.kagebot.Log.log import moe.kageru.kagebot.Util.checked -import moe.kageru.kagebot.config.Config +import moe.kageru.kagebot.config.ConfigParser import moe.kageru.kagebot.config.RawConfig import org.javacord.api.DiscordApiBuilder import org.javacord.api.event.message.MessageCreateEvent @@ -31,7 +31,7 @@ object Kagebot { } fun welcomeUser(event: ServerMemberJoinEvent) { - Globals.config.features.welcome!!.let { welcome -> + Globals.features.welcome!!.let { welcome -> val message = event.user.sendMessage(welcome.embed) // If the user disabled direct messages, try the fallback (if defined) if (!Util.wasSuccessful(message) && @@ -53,7 +53,7 @@ object Kagebot { fun init() { Globals.api = DiscordApiBuilder().setToken(getSecret()).login().join() try { - Globals.config = Config(RawConfig.read()) + ConfigParser.initialLoad(RawConfig.read()) } catch (e: IllegalArgumentException) { println("Config error:\n$e,\n${e.message},\n${e.stackTrace.joinToString("\n")}") exitProcess(1) @@ -64,7 +64,7 @@ object Kagebot { }) log.info("kagebot Mk II running") Globals.api.addMessageCreateListener { checked { processMessage(it) } } - Globals.config.features.welcome?.let { + Globals.features.welcome?.let { Globals.api.addServerMemberJoinListener { checked { welcomeUser(it) } } diff --git a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt index cfa36cd..d14dc0e 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt @@ -1,7 +1,6 @@ package moe.kageru.kagebot.command import moe.kageru.kagebot.Globals -import moe.kageru.kagebot.Globals.config import moe.kageru.kagebot.Log.log import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.Util.doIf @@ -41,8 +40,8 @@ class Command(cmd: RawCommand) { fun execute(message: MessageCreateEvent) { if (permissions?.isAllowed(message) == false) { - if (config.localization.permissionDenied.isNotBlank()) { - message.channel.sendMessage(config.localization.permissionDenied) + if (Globals.localization.permissionDenied.isNotBlank()) { + message.channel.sendMessage(Globals.localization.permissionDenied) } log.info("Denying command ${this.trigger} to user ${message.messageAuthor.discriminatedName} (ID: ${message.messageAuthor.id})") return diff --git a/src/main/kotlin/moe/kageru/kagebot/command/MessageActions.kt b/src/main/kotlin/moe/kageru/kagebot/command/MessageActions.kt index 11cc48b..7f058a0 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/MessageActions.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/MessageActions.kt @@ -1,6 +1,6 @@ package moe.kageru.kagebot.command -import moe.kageru.kagebot.Globals.config +import moe.kageru.kagebot.Globals import moe.kageru.kagebot.Log.log import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.config.RawMessageActions @@ -25,7 +25,7 @@ class MessageActions(rawActions: RawMessageActions) { message.messageAuthor.asUser().ifPresent { user -> user.sendMessage( MessageUtil.getEmbedBuilder() - .addField("Blacklisted", config.localization.messageDeleted) + .addField("Blacklisted", Globals.localization.messageDeleted) .addField("Original:", "“${message.readableMessageContent}”") ) } diff --git a/src/main/kotlin/moe/kageru/kagebot/command/MessageRedirect.kt b/src/main/kotlin/moe/kageru/kagebot/command/MessageRedirect.kt index ba059e3..e0d59a9 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/MessageRedirect.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/MessageRedirect.kt @@ -1,6 +1,6 @@ package moe.kageru.kagebot.command -import moe.kageru.kagebot.Globals.config +import moe.kageru.kagebot.Globals import moe.kageru.kagebot.Log.log import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.Util @@ -16,7 +16,7 @@ internal class MessageRedirect(rawRedirect: RawRedirect) { fun execute(message: MessageCreateEvent, command: Command) { val embed = MessageUtil.getEmbedBuilder() .addField( - config.localization.redirectedMessage, + Globals.localization.redirectedMessage, message.readableMessageContent.let { content -> when (command.matchType) { MatchType.PREFIX -> content.removePrefix(command.trigger).trim() diff --git a/src/main/kotlin/moe/kageru/kagebot/config/Config.kt b/src/main/kotlin/moe/kageru/kagebot/config/Config.kt index b6c95aa..55c0fff 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/Config.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/Config.kt @@ -6,25 +6,20 @@ import moe.kageru.kagebot.command.Command import moe.kageru.kagebot.features.Features import java.awt.Color -class Config(rawConfig: RawConfig) { - private val system: SystemConfig = rawConfig.system?.let(::SystemConfig) - ?: throw IllegalArgumentException("No [system] block in config.") - var localization: Localization = rawConfig.localization?.let(::Localization) - ?: throw IllegalArgumentException("No [localization] block in config.") - var features: Features - - init { - Globals.systemConfig = system - Globals.server = api.getServerById(system.serverId).orElseThrow { IllegalArgumentException("Invalid server configured.") } - Globals.features = rawConfig.features?.let(::Features) ?: Features(RawFeatures(null)) - // TODO: remove this - this.features = Globals.features - Globals.commands = rawConfig.commands?.map(::Command) ?: emptyList() - Globals.config = this +object ConfigParser { + fun initialLoad(rawConfig: RawConfig) { + val systemConfig = rawConfig.system?.let(::SystemConfig) + ?: throw IllegalArgumentException("No [system] block in config.") + Globals.server = api.getServerById(systemConfig.serverId).orElseThrow { IllegalArgumentException("Invalid server configured.") } + Globals.systemConfig = systemConfig + reloadLocalization(rawConfig) + reloadFeatures(rawConfig) + reloadCommands(rawConfig) } - fun reloadLocalization(rawLocalization: RawLocalization) { - this.localization = Localization(rawLocalization) + fun reloadLocalization(rawConfig: RawConfig) { + Globals.localization = rawConfig.localization?.let(::Localization) + ?: throw IllegalArgumentException("No [localization] block in config.") } fun reloadCommands(rawConfig: RawConfig) { @@ -32,8 +27,9 @@ class Config(rawConfig: RawConfig) { ?: throw IllegalArgumentException("No commands found in config.") } - fun reloadFeatures(rawFeatures: RawFeatures) { - this.features = Features(rawFeatures) + fun reloadFeatures(rawConfig: RawConfig) { + Globals.features = rawConfig.features?.let(::Features) + ?: Features(RawFeatures(null)) } } diff --git a/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt b/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt index 097b867..890fca4 100644 --- a/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/ConfigTest.kt @@ -2,13 +2,14 @@ package moe.kageru.kagebot import io.kotlintest.shouldBe import io.kotlintest.shouldNotBe -import io.kotlintest.specs.StringSpec +import io.kotlintest.specs.ShouldSpec -class ConfigTest : StringSpec({ +class ConfigTest : ShouldSpec({ TestUtil.prepareTestEnvironment() "should properly parse test config" { - Globals.config shouldNotBe null Globals.systemConfig shouldNotBe null + Globals.localization shouldNotBe null + Globals.features shouldNotBe null Globals.commands.size shouldBe 2 } }) diff --git a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt index ed0fbab..31d3298 100644 --- a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt +++ b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt @@ -7,7 +7,7 @@ import io.mockk.Runs import io.mockk.every import io.mockk.just import io.mockk.mockk -import moe.kageru.kagebot.config.Config +import moe.kageru.kagebot.config.ConfigParser import moe.kageru.kagebot.config.RawConfig import org.javacord.api.DiscordApi import org.javacord.api.entity.channel.ServerTextChannel @@ -15,7 +15,7 @@ import org.javacord.api.entity.message.embed.EmbedBuilder import org.javacord.api.entity.user.User import org.javacord.api.event.message.MessageCreateEvent import org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl -import java.util.Optional +import java.util.* object TestUtil { fun mockMessage( @@ -74,7 +74,7 @@ object TestUtil { }) } Globals.api = api - Globals.config = Config(RawConfig.read("testconfig.toml")) + ConfigParser.initialLoad(RawConfig.read("testconfig.toml")) } fun testMessageSuccess(content: String, result: String) { @@ -90,17 +90,17 @@ object TestUtil { fun withCommands(config: String, test: (() -> R)) { val oldCmds = Globals.commands val rawConfig = RawConfig.readFromString(config) - Globals.config.reloadCommands(rawConfig) + ConfigParser.reloadCommands(rawConfig) test() Globals.commands = oldCmds } fun withLocalization(config: String, test: (() -> R)) { - val oldLoc = Globals.config.localization + val oldLoc = Globals.localization val rawConfig = RawConfig.readFromString(config) - Globals.config.reloadLocalization(rawConfig.localization!!) + ConfigParser.reloadLocalization(rawConfig) test() - Globals.config.localization = oldLoc + Globals.localization = oldLoc } fun withReplyContents( diff --git a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt index 40ffa1c..be6902e 100644 --- a/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/command/CommandTest.kt @@ -6,7 +6,6 @@ import io.kotlintest.specs.StringSpec import io.mockk.every import io.mockk.mockk import moe.kageru.kagebot.Globals -import moe.kageru.kagebot.Globals.config import moe.kageru.kagebot.Kagebot import moe.kageru.kagebot.TestUtil import moe.kageru.kagebot.TestUtil.embedToString @@ -133,7 +132,7 @@ class CommandTest : StringSpec({ val replies = mutableListOf() val mockMessage = mockMessage("!restricted", replies = replies) Kagebot.processMessage(mockMessage) - replies shouldBe mutableListOf(config.localization.permissionDenied) + replies shouldBe mutableListOf(Globals.localization.permissionDenied) withLocalization( """ [localization] @@ -217,7 +216,7 @@ class CommandTest : StringSpec({ every { get().getRoles(any()) } returns emptyList() } Kagebot.processMessage(mockMessage) - calls shouldBe mutableListOf(config.localization.permissionDenied, "access granted") + calls shouldBe mutableListOf(Globals.localization.permissionDenied, "access granted") } } "should refuse DM only message in server channel" { @@ -232,7 +231,7 @@ class CommandTest : StringSpec({ ) { val calls = mutableListOf() Kagebot.processMessage(mockMessage("!dm", replies = calls)) - calls shouldBe listOf(config.localization.permissionDenied) + calls shouldBe listOf(Globals.localization.permissionDenied) } } /* diff --git a/src/test/kotlin/moe/kageru/kagebot/features/WelcomeFeatureTest.kt b/src/test/kotlin/moe/kageru/kagebot/features/WelcomeFeatureTest.kt index 6b0bdaf..d09d18b 100644 --- a/src/test/kotlin/moe/kageru/kagebot/features/WelcomeFeatureTest.kt +++ b/src/test/kotlin/moe/kageru/kagebot/features/WelcomeFeatureTest.kt @@ -23,7 +23,7 @@ class WelcomeFeatureTest : StringSpec({ } } ) - sentMessages shouldBe mutableListOf(Globals.config.features.welcome!!.embed) + sentMessages shouldBe mutableListOf(Globals.features.welcome!!.embed) } "should send welcome fallback if DMs are disabled" { val message = mutableListOf()