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