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 {
|
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("com.moandjiezana.toml:toml4j:0.7.2")
|
||||||
implementation(kotlin("stdlib-jdk8"))
|
implementation(kotlin("stdlib-jdk8"))
|
||||||
implementation("org.javacord:javacord:3.0.4")
|
implementation("org.javacord:javacord:3.0.4")
|
||||||
|
|
|
@ -44,7 +44,7 @@ object Kagebot {
|
||||||
val api = DiscordApiBuilder().setToken(secret).login().join()
|
val api = DiscordApiBuilder().setToken(secret).login().join()
|
||||||
Globals.api = api
|
Globals.api = api
|
||||||
try {
|
try {
|
||||||
ConfigParser.initialLoad(RawConfig.read())
|
ConfigParser.initialLoad(RawConfig.DEFAULT_CONFIG_PATH)
|
||||||
} 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)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package moe.kageru.kagebot
|
||||||
import moe.kageru.kagebot.Util.failed
|
import moe.kageru.kagebot.Util.failed
|
||||||
import moe.kageru.kagebot.config.Config
|
import moe.kageru.kagebot.config.Config
|
||||||
import moe.kageru.kagebot.Util.toPairs
|
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.Message
|
||||||
import org.javacord.api.entity.message.MessageAuthor
|
import org.javacord.api.entity.message.MessageAuthor
|
||||||
import org.javacord.api.entity.message.Messageable
|
import org.javacord.api.entity.message.Messageable
|
||||||
|
@ -22,7 +23,7 @@ object MessageUtil {
|
||||||
fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder {
|
fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder {
|
||||||
val builder = EmbedBuilder()
|
val builder = EmbedBuilder()
|
||||||
Config.server.icon.ifPresent { builder.setThumbnail(it) }
|
Config.server.icon.ifPresent { builder.setThumbnail(it) }
|
||||||
builder.setColor(Config.systemConfig.color)
|
builder.setColor(SystemSpec.color)
|
||||||
builder.op()
|
builder.op()
|
||||||
return builder
|
return builder
|
||||||
}
|
}
|
||||||
|
@ -33,9 +34,8 @@ object MessageUtil {
|
||||||
op()
|
op()
|
||||||
}
|
}
|
||||||
val sent = sendMessage(embed)
|
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.
|
* I tried LinkedHashMaps, but those don’t seem to work either.
|
||||||
*/
|
*/
|
||||||
fun listToEmbed(contents: List<String>): EmbedBuilder {
|
fun listToEmbed(contents: List<String>): EmbedBuilder {
|
||||||
if (contents.size % 2 == 1) {
|
check(contents.size % 2 != 1) { "Embed must have even number of content strings (title/content pairs)" }
|
||||||
throw IllegalStateException("Embed must have even number of content strings (title/content pairs)")
|
|
||||||
}
|
|
||||||
return withEmbed {
|
return withEmbed {
|
||||||
contents.toPairs().forEach { (heading, content) ->
|
contents.toPairs().forEach { (heading, content) ->
|
||||||
addField(heading, content)
|
addField(heading, content)
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package moe.kageru.kagebot.config
|
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.command.Command
|
||||||
import moe.kageru.kagebot.features.Features
|
import moe.kageru.kagebot.features.Features
|
||||||
import org.javacord.api.entity.server.Server
|
import org.javacord.api.entity.server.Server
|
||||||
|
|
||||||
object Config {
|
object Config {
|
||||||
|
val specs = Config { addSpec(SystemSpec) }.from.toml
|
||||||
|
lateinit var config: Config
|
||||||
lateinit var server: Server
|
lateinit var server: Server
|
||||||
lateinit var commands: List<Command>
|
lateinit var commands: List<Command>
|
||||||
lateinit var systemConfig: SystemConfig
|
|
||||||
lateinit var features: Features
|
lateinit var features: Features
|
||||||
lateinit var localization: Localization
|
lateinit var localization: Localization
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,20 @@ package moe.kageru.kagebot.config
|
||||||
|
|
||||||
import moe.kageru.kagebot.Globals
|
import moe.kageru.kagebot.Globals
|
||||||
import moe.kageru.kagebot.command.Command
|
import moe.kageru.kagebot.command.Command
|
||||||
|
import moe.kageru.kagebot.config.SystemSpec.serverId
|
||||||
import moe.kageru.kagebot.features.Features
|
import moe.kageru.kagebot.features.Features
|
||||||
import java.awt.Color
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
object ConfigParser {
|
object ConfigParser {
|
||||||
val configFile: File = File(RawConfig.DEFAULT_CONFIG_PATH)
|
val configFile: File = File(RawConfig.DEFAULT_CONFIG_PATH)
|
||||||
|
|
||||||
fun initialLoad(rawConfig: RawConfig) {
|
fun initialLoad(file: String) {
|
||||||
val systemConfig = rawConfig.system?.let(::SystemConfig)
|
val rawConfig = RawConfig.read(file)
|
||||||
?: throw IllegalArgumentException("No [system] block in config.")
|
val config = Config.specs.file(RawConfig.getFile(file))
|
||||||
Config.server = Globals.api.getServerById(systemConfig.serverId)
|
Config.config = config
|
||||||
|
|
||||||
|
Config.server = Globals.api.getServerById(config[serverId])
|
||||||
.orElseThrow { IllegalArgumentException("Invalid server configured.") }
|
.orElseThrow { IllegalArgumentException("Invalid server configured.") }
|
||||||
Config.systemConfig = systemConfig
|
|
||||||
reloadLocalization(rawConfig)
|
reloadLocalization(rawConfig)
|
||||||
reloadFeatures(rawConfig)
|
reloadFeatures(rawConfig)
|
||||||
reloadCommands(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(
|
class Localization(
|
||||||
val permissionDenied: String,
|
val permissionDenied: String,
|
||||||
val redirectedMessage: String,
|
val redirectedMessage: String,
|
||||||
|
|
|
@ -2,10 +2,12 @@ package moe.kageru.kagebot.config
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
import com.moandjiezana.toml.Toml
|
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
|
import java.io.File
|
||||||
|
|
||||||
class RawConfig(
|
class RawConfig(
|
||||||
val system: RawSystemConfig?,
|
|
||||||
val localization: RawLocalization?,
|
val localization: RawLocalization?,
|
||||||
@SerializedName("command")
|
@SerializedName("command")
|
||||||
val commands: List<RawCommand>?,
|
val commands: List<RawCommand>?,
|
||||||
|
@ -17,7 +19,7 @@ class RawConfig(
|
||||||
|
|
||||||
fun readFromString(tomlContent: String): RawConfig = Toml().read(tomlContent).to(RawConfig::class.java)
|
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)
|
val file = File(path)
|
||||||
if (file.isFile) {
|
if (file.isFile) {
|
||||||
return file
|
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(
|
class RawLocalization(
|
||||||
val permissionDenied: String?,
|
val permissionDenied: String?,
|
||||||
val redirectedMessage: String?,
|
val redirectedMessage: String?,
|
||||||
|
|
|
@ -6,13 +6,16 @@ import io.kotlintest.specs.ShouldSpec
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import moe.kageru.kagebot.config.Config
|
import moe.kageru.kagebot.config.Config
|
||||||
|
import moe.kageru.kagebot.config.SystemSpec
|
||||||
import moe.kageru.kagebot.features.SetConfigFeature
|
import moe.kageru.kagebot.features.SetConfigFeature
|
||||||
|
import java.awt.Color
|
||||||
|
|
||||||
@ExperimentalStdlibApi
|
@ExperimentalStdlibApi
|
||||||
class ConfigTest : ShouldSpec({
|
class ConfigTest : ShouldSpec({
|
||||||
TestUtil.prepareTestEnvironment()
|
TestUtil.prepareTestEnvironment()
|
||||||
"should properly parse test config" {
|
"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.localization shouldNotBe null
|
||||||
Config.features shouldNotBe null
|
Config.features shouldNotBe null
|
||||||
Config.commands.size shouldBe 3
|
Config.commands.size shouldBe 3
|
||||||
|
|
|
@ -103,7 +103,7 @@ object TestUtil {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Globals.api = api
|
Globals.api = api
|
||||||
ConfigParser.initialLoad(RawConfig.read("testconfig.toml"))
|
ConfigParser.initialLoad("testconfig.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testMessageSuccess(content: String, result: String) {
|
fun testMessageSuccess(content: String, result: String) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user