Reload config at runtime via command + attachment
This implements #10, but I want to write tests before closing that.
This commit is contained in:
parent
8a1308f98d
commit
d0b3bb608b
|
@ -6,20 +6,23 @@ class Features(
|
|||
val welcome: WelcomeFeature?,
|
||||
debug: DebugFeature,
|
||||
help: HelpFeature,
|
||||
getConfig: GetConfigFeature
|
||||
getConfig: GetConfigFeature,
|
||||
setConfig: SetConfigFeature
|
||||
) {
|
||||
constructor(rawFeatures: RawFeatures) : this(
|
||||
rawFeatures.welcome?.let(::WelcomeFeature),
|
||||
DebugFeature(),
|
||||
HelpFeature(),
|
||||
GetConfigFeature()
|
||||
GetConfigFeature(),
|
||||
SetConfigFeature()
|
||||
)
|
||||
|
||||
private val featureMap = mapOf(
|
||||
"help" to help,
|
||||
"debug" to debug,
|
||||
"welcome" to welcome,
|
||||
"getConfig" to getConfig
|
||||
"getConfig" to getConfig,
|
||||
"setConfig" to setConfig
|
||||
)
|
||||
|
||||
fun findByString(feature: String) = featureMap[feature]
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package moe.kageru.kagebot.features
|
||||
|
||||
import moe.kageru.kagebot.Log
|
||||
import moe.kageru.kagebot.MessageUtil
|
||||
import moe.kageru.kagebot.config.ConfigParser
|
||||
import moe.kageru.kagebot.config.RawConfig
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
class SetConfigFeature : MessageFeature() {
|
||||
@ExperimentalStdlibApi
|
||||
override fun handleInternal(message: MessageCreateEvent) {
|
||||
if (message.messageAttachments.size != 1) {
|
||||
message.channel.sendMessage("Error: please attach the new config to your message.")
|
||||
return
|
||||
}
|
||||
val newConfig = message.messageAttachments[0].url.openStream().readAllBytes().decodeToString()
|
||||
val rawConfig = try {
|
||||
RawConfig.readFromString(newConfig)
|
||||
} catch (e: IllegalStateException) {
|
||||
MessageUtil.sendEmbed(
|
||||
message.channel,
|
||||
MessageUtil.getEmbedBuilder()
|
||||
.addField(
|
||||
"An unexpected error occured. This is probably caused by a malformed config file. Perhaps this can help:",
|
||||
"```$e: ${e.message}"
|
||||
)
|
||||
)
|
||||
Log.log.info("Could not parse new config: $e: ${e.message}")
|
||||
return
|
||||
}
|
||||
try {
|
||||
ConfigParser.reloadLocalization(rawConfig)
|
||||
ConfigParser.reloadFeatures(rawConfig)
|
||||
ConfigParser.reloadCommands(rawConfig)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
MessageUtil.sendEmbed(
|
||||
message.channel,
|
||||
MessageUtil.getEmbedBuilder()
|
||||
.addField("Error", "```${e.message}```")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -110,3 +110,7 @@ feature = "help"
|
|||
[[command]]
|
||||
trigger = "!getConfig"
|
||||
feature = "getConfig"
|
||||
|
||||
[[command]]
|
||||
trigger = "!setConfig"
|
||||
feature = "setConfig"
|
||||
|
|
Loading…
Reference in New Issue
Block a user