Rewrite Config to be a ConfigParser object
This commit is contained in:
parent
23afb34a97
commit
c59c0fdcd6
|
@ -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<Command>
|
||||
lateinit var systemConfig: SystemConfig
|
||||
lateinit var features: Features
|
||||
var commandCounter: AtomicInteger = AtomicInteger(0)
|
||||
lateinit var localization: Localization
|
||||
val commandCounter: AtomicInteger = AtomicInteger(0)
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}”")
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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 <R> 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 <R> 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(
|
||||
|
|
|
@ -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<String>()
|
||||
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<String>()
|
||||
Kagebot.processMessage(mockMessage("!dm", replies = calls))
|
||||
calls shouldBe listOf(config.localization.permissionDenied)
|
||||
calls shouldBe listOf(Globals.localization.permissionDenied)
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
|
|
@ -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<String>()
|
||||
|
|
Loading…
Reference in New Issue
Block a user