Add getConfig feature to retrieve the current config file

This implements #11 and is preparation for config reloading at runtime
This commit is contained in:
kageru 2019-07-14 17:14:23 +02:00
parent 911707a680
commit b0d3475469
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
7 changed files with 62 additions and 4 deletions

View File

@ -4,8 +4,11 @@ import moe.kageru.kagebot.Globals
import moe.kageru.kagebot.command.Command
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.")

View File

@ -13,7 +13,7 @@ class RawConfig(
val features: RawFeatures?
) {
companion object {
private const val DEFAULT_CONFIG_PATH = "config.toml"
const val DEFAULT_CONFIG_PATH = "config.toml"
fun readFromString(tomlContent: String): RawConfig = Toml().read(tomlContent).to(RawConfig::class.java)

View File

@ -2,14 +2,25 @@ package moe.kageru.kagebot.features
import moe.kageru.kagebot.config.RawFeatures
class Features(val welcome: WelcomeFeature?, debug: DebugFeature, help: HelpFeature) {
class Features(
val welcome: WelcomeFeature?,
debug: DebugFeature,
help: HelpFeature,
getConfig: GetConfigFeature
) {
constructor(rawFeatures: RawFeatures) : this(
rawFeatures.welcome?.let(::WelcomeFeature),
DebugFeature(),
HelpFeature()
HelpFeature(),
GetConfigFeature()
)
private val featureMap = mapOf("help" to help, "debug" to debug, "welcome" to welcome)
private val featureMap = mapOf(
"help" to help,
"debug" to debug,
"welcome" to welcome,
"getConfig" to getConfig
)
fun findByString(feature: String) = featureMap[feature]
}

View File

@ -0,0 +1,13 @@
package moe.kageru.kagebot.features
import moe.kageru.kagebot.config.ConfigParser
import org.javacord.api.event.message.MessageCreateEvent
/**
* Simple message handler to send the current config file via message attachment.
*/
class GetConfigFeature : MessageFeature() {
override fun handleInternal(message: MessageCreateEvent) {
message.channel.sendMessage(ConfigParser.configFile)
}
}

View File

@ -106,3 +106,7 @@ feature = "welcome"
[[command]]
trigger = "!help"
feature = "help"
[[command]]
trigger = "!getConfig"
feature = "getConfig"

View File

@ -16,6 +16,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.io.File
import java.util.*
object TestUtil {
@ -23,6 +24,7 @@ object TestUtil {
content: String,
replies: MutableList<String> = mutableListOf(),
replyEmbeds: MutableList<EmbedBuilder> = mutableListOf(),
files: MutableList<File> = mutableListOf(),
isBot: Boolean = false
): MessageCreateEvent {
return mockk {
@ -30,6 +32,7 @@ object TestUtil {
every { readableMessageContent } returns content
every { channel.sendMessage(capture(replies)) } returns mockk()
every { channel.sendMessage(capture(replyEmbeds)) } returns mockk()
every { channel.sendMessage(capture(files)) } returns mockk()
every { message.canYouDelete() } returns true
every { isPrivateMessage } returns false
// We can’t use a nested mock here because other fields of messageAuthor might

View File

@ -0,0 +1,24 @@
package moe.kageru.kagebot.features
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import moe.kageru.kagebot.Kagebot
import moe.kageru.kagebot.TestUtil
import moe.kageru.kagebot.TestUtil.mockMessage
import moe.kageru.kagebot.TestUtil.withCommands
import java.io.File
class ConfigFeatureTest : ShouldSpec({
TestUtil.prepareTestEnvironment()
"getConfig should sent message with attachment" {
withCommands("""
[[command]]
trigger = "!getConfig"
feature = "getConfig"
""".trimIndent()) {
val calls = mutableListOf<File>()
Kagebot.processMessage(mockMessage("!getConfig", files = calls))
calls.size shouldBe 1
}
}
})