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:
parent
911707a680
commit
b0d3475469
|
@ -4,8 +4,11 @@ import moe.kageru.kagebot.Globals
|
||||||
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 java.awt.Color
|
import java.awt.Color
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
object ConfigParser {
|
object ConfigParser {
|
||||||
|
val configFile: File = File(RawConfig.DEFAULT_CONFIG_PATH)
|
||||||
|
|
||||||
fun initialLoad(rawConfig: RawConfig) {
|
fun initialLoad(rawConfig: RawConfig) {
|
||||||
val systemConfig = rawConfig.system?.let(::SystemConfig)
|
val systemConfig = rawConfig.system?.let(::SystemConfig)
|
||||||
?: throw IllegalArgumentException("No [system] block in config.")
|
?: throw IllegalArgumentException("No [system] block in config.")
|
||||||
|
|
|
@ -13,7 +13,7 @@ class RawConfig(
|
||||||
val features: RawFeatures?
|
val features: RawFeatures?
|
||||||
) {
|
) {
|
||||||
companion object {
|
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)
|
fun readFromString(tomlContent: String): RawConfig = Toml().read(tomlContent).to(RawConfig::class.java)
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,25 @@ package moe.kageru.kagebot.features
|
||||||
|
|
||||||
import moe.kageru.kagebot.config.RawFeatures
|
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(
|
constructor(rawFeatures: RawFeatures) : this(
|
||||||
rawFeatures.welcome?.let(::WelcomeFeature),
|
rawFeatures.welcome?.let(::WelcomeFeature),
|
||||||
DebugFeature(),
|
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]
|
fun findByString(feature: String) = featureMap[feature]
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -106,3 +106,7 @@ feature = "welcome"
|
||||||
[[command]]
|
[[command]]
|
||||||
trigger = "!help"
|
trigger = "!help"
|
||||||
feature = "help"
|
feature = "help"
|
||||||
|
|
||||||
|
[[command]]
|
||||||
|
trigger = "!getConfig"
|
||||||
|
feature = "getConfig"
|
||||||
|
|
|
@ -16,6 +16,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.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object TestUtil {
|
object TestUtil {
|
||||||
|
@ -23,6 +24,7 @@ object TestUtil {
|
||||||
content: String,
|
content: String,
|
||||||
replies: MutableList<String> = mutableListOf(),
|
replies: MutableList<String> = mutableListOf(),
|
||||||
replyEmbeds: MutableList<EmbedBuilder> = mutableListOf(),
|
replyEmbeds: MutableList<EmbedBuilder> = mutableListOf(),
|
||||||
|
files: MutableList<File> = mutableListOf(),
|
||||||
isBot: Boolean = false
|
isBot: Boolean = false
|
||||||
): MessageCreateEvent {
|
): MessageCreateEvent {
|
||||||
return mockk {
|
return mockk {
|
||||||
|
@ -30,6 +32,7 @@ object TestUtil {
|
||||||
every { readableMessageContent } returns content
|
every { readableMessageContent } returns content
|
||||||
every { channel.sendMessage(capture(replies)) } returns mockk()
|
every { channel.sendMessage(capture(replies)) } returns mockk()
|
||||||
every { channel.sendMessage(capture(replyEmbeds)) } returns mockk()
|
every { channel.sendMessage(capture(replyEmbeds)) } returns mockk()
|
||||||
|
every { channel.sendMessage(capture(files)) } returns mockk()
|
||||||
every { message.canYouDelete() } returns true
|
every { message.canYouDelete() } returns true
|
||||||
every { isPrivateMessage } returns false
|
every { isPrivateMessage } returns false
|
||||||
// We can’t use a nested mock here because other fields of messageAuthor might
|
// We can’t use a nested mock here because other fields of messageAuthor might
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user