Rewrite config to use Konf (1): SystemConfig
This commit is contained in:
parent
87cb943712
commit
e31d46ceb5
|
@ -34,6 +34,8 @@ val test by tasks.getting(Test::class) {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.uchuhimo:konf-core:0.20.0")
|
||||
implementation("com.uchuhimo:konf-toml:0.20.0")
|
||||
implementation("com.moandjiezana.toml:toml4j:0.7.2")
|
||||
implementation(kotlin("stdlib-jdk8"))
|
||||
implementation("org.javacord:javacord:3.0.4")
|
||||
|
|
|
@ -44,7 +44,7 @@ object Kagebot {
|
|||
val api = DiscordApiBuilder().setToken(secret).login().join()
|
||||
Globals.api = api
|
||||
try {
|
||||
ConfigParser.initialLoad(RawConfig.read())
|
||||
ConfigParser.initialLoad(RawConfig.DEFAULT_CONFIG_PATH)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
println("Config error:\n$e,\n${e.message},\n${e.stackTrace.joinToString("\n")}")
|
||||
exitProcess(1)
|
||||
|
|
|
@ -3,6 +3,7 @@ package moe.kageru.kagebot
|
|||
import moe.kageru.kagebot.Util.failed
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.Util.toPairs
|
||||
import moe.kageru.kagebot.config.SystemSpec
|
||||
import org.javacord.api.entity.message.Message
|
||||
import org.javacord.api.entity.message.MessageAuthor
|
||||
import org.javacord.api.entity.message.Messageable
|
||||
|
@ -22,7 +23,7 @@ object MessageUtil {
|
|||
fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder {
|
||||
val builder = EmbedBuilder()
|
||||
Config.server.icon.ifPresent { builder.setThumbnail(it) }
|
||||
builder.setColor(Config.systemConfig.color)
|
||||
builder.setColor(SystemSpec.color)
|
||||
builder.op()
|
||||
return builder
|
||||
}
|
||||
|
@ -33,9 +34,8 @@ object MessageUtil {
|
|||
op()
|
||||
}
|
||||
val sent = sendMessage(embed)
|
||||
if (sent.failed()) {
|
||||
// for logging
|
||||
}
|
||||
// for logging
|
||||
sent.failed()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,9 +51,7 @@ object MessageUtil {
|
|||
* I tried LinkedHashMaps, but those don’t seem to work either.
|
||||
*/
|
||||
fun listToEmbed(contents: List<String>): EmbedBuilder {
|
||||
if (contents.size % 2 == 1) {
|
||||
throw IllegalStateException("Embed must have even number of content strings (title/content pairs)")
|
||||
}
|
||||
check(contents.size % 2 != 1) { "Embed must have even number of content strings (title/content pairs)" }
|
||||
return withEmbed {
|
||||
contents.toPairs().forEach { (heading, content) ->
|
||||
addField(heading, content)
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package moe.kageru.kagebot.config
|
||||
|
||||
import com.uchuhimo.konf.Config
|
||||
import com.uchuhimo.konf.source.toml
|
||||
import moe.kageru.kagebot.command.Command
|
||||
import moe.kageru.kagebot.features.Features
|
||||
import org.javacord.api.entity.server.Server
|
||||
|
||||
object Config {
|
||||
val specs = Config { addSpec(SystemSpec) }.from.toml
|
||||
lateinit var config: Config
|
||||
lateinit var server: Server
|
||||
lateinit var commands: List<Command>
|
||||
lateinit var systemConfig: SystemConfig
|
||||
lateinit var features: Features
|
||||
lateinit var localization: Localization
|
||||
}
|
||||
|
|
|
@ -2,19 +2,20 @@ package moe.kageru.kagebot.config
|
|||
|
||||
import moe.kageru.kagebot.Globals
|
||||
import moe.kageru.kagebot.command.Command
|
||||
import moe.kageru.kagebot.config.SystemSpec.serverId
|
||||
import moe.kageru.kagebot.features.Features
|
||||
import java.awt.Color
|
||||
import java.io.File
|
||||
|
||||
object ConfigParser {
|
||||
val configFile: File = File(RawConfig.DEFAULT_CONFIG_PATH)
|
||||
|
||||
fun initialLoad(rawConfig: RawConfig) {
|
||||
val systemConfig = rawConfig.system?.let(::SystemConfig)
|
||||
?: throw IllegalArgumentException("No [system] block in config.")
|
||||
Config.server = Globals.api.getServerById(systemConfig.serverId)
|
||||
fun initialLoad(file: String) {
|
||||
val rawConfig = RawConfig.read(file)
|
||||
val config = Config.specs.file(RawConfig.getFile(file))
|
||||
Config.config = config
|
||||
|
||||
Config.server = Globals.api.getServerById(config[serverId])
|
||||
.orElseThrow { IllegalArgumentException("Invalid server configured.") }
|
||||
Config.systemConfig = systemConfig
|
||||
reloadLocalization(rawConfig)
|
||||
reloadFeatures(rawConfig)
|
||||
reloadCommands(rawConfig)
|
||||
|
@ -36,13 +37,6 @@ object ConfigParser {
|
|||
}
|
||||
}
|
||||
|
||||
class SystemConfig(val serverId: String, val color: Color) {
|
||||
constructor(rawSystemConfig: RawSystemConfig) : this(
|
||||
rawSystemConfig.serverId ?: throw IllegalArgumentException("No [system.server] defined."),
|
||||
Color.decode(rawSystemConfig.color ?: "#1793d0")
|
||||
)
|
||||
}
|
||||
|
||||
class Localization(
|
||||
val permissionDenied: String,
|
||||
val redirectedMessage: String,
|
||||
|
|
|
@ -2,10 +2,12 @@ package moe.kageru.kagebot.config
|
|||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.moandjiezana.toml.Toml
|
||||
import com.uchuhimo.konf.ConfigSpec
|
||||
import moe.kageru.kagebot.config.Config.config
|
||||
import java.awt.Color
|
||||
import java.io.File
|
||||
|
||||
class RawConfig(
|
||||
val system: RawSystemConfig?,
|
||||
val localization: RawLocalization?,
|
||||
@SerializedName("command")
|
||||
val commands: List<RawCommand>?,
|
||||
|
@ -17,7 +19,7 @@ class RawConfig(
|
|||
|
||||
fun readFromString(tomlContent: String): RawConfig = Toml().read(tomlContent).to(RawConfig::class.java)
|
||||
|
||||
private fun getFile(path: String): File {
|
||||
fun getFile(path: String): File {
|
||||
val file = File(path)
|
||||
if (file.isFile) {
|
||||
return file
|
||||
|
@ -33,7 +35,12 @@ class RawConfig(
|
|||
}
|
||||
}
|
||||
|
||||
class RawSystemConfig(val serverId: String?, val color: String?)
|
||||
object SystemSpec : ConfigSpec() {
|
||||
private val rawColor by optional("#1793d0", name = "color")
|
||||
val serverId by required<String>()
|
||||
val color by kotlin.lazy { Color.decode(config[rawColor])!! }
|
||||
}
|
||||
|
||||
class RawLocalization(
|
||||
val permissionDenied: String?,
|
||||
val redirectedMessage: String?,
|
||||
|
|
|
@ -6,13 +6,16 @@ import io.kotlintest.specs.ShouldSpec
|
|||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.config.SystemSpec
|
||||
import moe.kageru.kagebot.features.SetConfigFeature
|
||||
import java.awt.Color
|
||||
|
||||
@ExperimentalStdlibApi
|
||||
class ConfigTest : ShouldSpec({
|
||||
TestUtil.prepareTestEnvironment()
|
||||
"should properly parse test config" {
|
||||
Config.systemConfig shouldNotBe null
|
||||
Config.config[SystemSpec.serverId] shouldNotBe null
|
||||
SystemSpec.color shouldBe Color.decode("#1793d0")
|
||||
Config.localization shouldNotBe null
|
||||
Config.features shouldNotBe null
|
||||
Config.commands.size shouldBe 3
|
||||
|
|
|
@ -103,7 +103,7 @@ object TestUtil {
|
|||
})
|
||||
}
|
||||
Globals.api = api
|
||||
ConfigParser.initialLoad(RawConfig.read("testconfig.toml"))
|
||||
ConfigParser.initialLoad("testconfig.toml")
|
||||
}
|
||||
|
||||
fun testMessageSuccess(content: String, result: String) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user