2019-06-08 11:07:52 +02:00
package moe.kageru.kagebot
import com.moandjiezana.toml.Toml
import java.io.File
class Config ( val system : System , val commands : Commands ) {
companion object {
val config : Config by lazy { read ( " config.toml " ) }
val secret = File ( " secret " ) . readText ( ) . replace ( " \n " , " " )
private fun read ( path : String ) : Config {
val rawConfig : Toml = Toml ( ) . read ( run {
val file = File ( path )
if ( file . isFile ) {
return @run file
}
println ( " Config not found, falling back to defaults... " )
File ( this :: class . java . classLoader . getResource ( path ) !! . toURI ( ) )
} )
2019-06-08 12:27:32 +02:00
val parsed = rawConfig . to ( Config :: class . java )
return Config (
System ( parsed . system ) ,
Commands ( parsed . commands . commands . map { Command ( it ) } )
)
2019-06-08 11:07:52 +02:00
}
}
}
2019-06-08 12:27:32 +02:00
data class System ( val serverId : String , val admins : List < String > ) {
/ * *
* Self constructor to explicitly repeat the nullability checks after TOML parsing .
* /
constructor ( system : System ) : this ( system . serverId , system . admins )
}
2019-06-08 11:07:52 +02:00
2019-06-08 12:27:32 +02:00
/ * *
* Wrapper around [ Command ] s for TOML deserialization .
* /
2019-06-08 11:07:52 +02:00
data class Commands ( val commands : List < Command > )