2019-07-14 17:27:43 +02:00
package moe.kageru.kagebot.features
2019-07-17 22:04:06 +02:00
import moe.kageru.kagebot.MessageUtil.sendEmbed
2019-10-18 19:34:41 +02:00
import moe.kageru.kagebot.config.Config
2019-07-14 17:27:43 +02:00
import moe.kageru.kagebot.config.ConfigParser
import org.javacord.api.event.message.MessageCreateEvent
2019-10-18 21:56:31 +02:00
import java.lang.reflect.InvocationTargetException
2019-07-14 17:27:43 +02:00
2019-07-17 23:42:12 +02:00
class SetConfigFeature : MessageFeature {
2019-07-14 17:27:43 +02:00
@ExperimentalStdlibApi
2019-07-17 23:42:12 +02:00
override fun handle ( message : MessageCreateEvent ) {
2019-07-14 17:27:43 +02:00
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 ( )
try {
2019-10-18 19:34:41 +02:00
Config . localization = Config . localeSpec . string ( newConfig )
2019-10-18 21:56:31 +02:00
Config . featureConfig = Config . featureSpec . string ( newConfig )
2019-10-18 20:48:43 +02:00
Config . commandConfig = Config . commandSpec . string ( newConfig )
2019-07-14 21:07:19 +02:00
ConfigParser . configFile . writeText ( newConfig )
message . channel . sendMessage ( " Config reloaded. " )
2019-10-18 21:56:31 +02:00
} catch ( e : Exception ) {
2019-07-17 22:04:06 +02:00
message . channel . sendEmbed {
addField ( " Error " , " ``` ${e.message} ``` " )
}
2019-07-14 17:27:43 +02:00
}
}
}