Skip to content

Commit

Permalink
updated config to use json instead of yml
Browse files Browse the repository at this point in the history
  • Loading branch information
FireBall1725 committed Dec 12, 2023
1 parent 390cc61 commit 76b39b4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This contains secrets
app/config/botConfig.yml
app/config/botConfig.json

# Created by https://www.toptal.com/developers/gitignore/api/kotlin,osx,windows,linux,intellij,gradle
# Edit at https://www.toptal.com/developers/gitignore?templates=kotlin,osx,windows,linux,intellij,gradle
Expand Down
14 changes: 14 additions & 0 deletions app/config/botConfig-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"discord_token": "<discord bot token>",
"pterodactyl_token": "<pterodactyl api token>",
"pterodactyl_url": "<pterodactyl api url>",
"whitelist": {
"enabled": true,
"whitelist_channel_id": 1183054905913655399,
"whitelist_authorized_role_id": 1143743030541680664
},
"backup_download": {
"enabled": false
},
"is_dev_env": true
}
8 changes: 0 additions & 8 deletions app/config/botConfig-example.yml

This file was deleted.

25 changes: 7 additions & 18 deletions app/src/main/kotlin/ca/fireball1725/lcs/discordbot/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.google.gson.Gson
import dev.kord.common.annotation.KordPreview
import dev.kord.gateway.Intents
import dev.kord.x.emoji.Emojis
Expand All @@ -24,12 +25,13 @@ import me.jakejmattson.discordkt.dsl.ListenerException
import me.jakejmattson.discordkt.dsl.bot
import me.jakejmattson.discordkt.locale.Language
import java.awt.Color
import java.io.Reader
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path

private val configPath = FileSystems.getDefault().getPath("config", "botConfig.yml")
private val botConfig: BotConfig = loadConfigFromFile(configPath)
private val configPath = FileSystems.getDefault().getPath("config", "botConfig.json")
private val botConfig: BotConfig = loadBotConfig(configPath)

private val pterodactyl: Pterodactyl = Pterodactyl(botConfig.pterodactylToken, botConfig.pterodactylUrl)

Expand Down Expand Up @@ -145,22 +147,9 @@ suspend fun main(args: Array<String>) {
}
}

private fun loadConfigFromFile(path: Path): BotConfig {
val mapper = ObjectMapper(YAMLFactory())
mapper.registerModule(
KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, true)
.configure(KotlinFeature.StrictNullChecks, false)
.build(),
)

return Files.newBufferedReader(path).use {
mapper.readValue(it, BotConfig::class.java)
}
private fun loadBotConfig(path: Path): BotConfig {
val reader: Reader = Files.newBufferedReader(path)
return Gson().fromJson(reader, BotConfig::class.java)
}

fun botConfig(): BotConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ import com.google.gson.annotations.SerializedName
data class BotConfig(
@SerializedName("discord_token")
val discordToken: String,

@SerializedName("pterodactyl_token")
val pterodactylToken: String,

@SerializedName("pterodactyl_url")
val pterodactylUrl: String,

@SerializedName("is_dev_env")
val isDevelopmentEnvironment: Boolean,

@SerializedName("whitelist")
val whitelist: BotConfigConfigWhitelist? = BotConfigConfigWhitelist(),
val whitelist: BotConfigConfigWhitelist,

@SerializedName("backup_download")
val backupDownload: BotConfigConfigBackupDownload,
)

data class BotConfigConfigWhitelist(
@SerializedName("enabled")
val enabled: Boolean = false,
val enabled: Boolean,

@SerializedName("whitelist_channel_id")
val channelId: Long? = null,
val channelId: Long,

@SerializedName("whitelist_authorized_role_id")
val authorizedRoleId: Long? = null,
val authorizedRoleId: Long,
)

data class BotConfigConfigBackupDownload(
@SerializedName("allow_backup_downloads")
@SerializedName("enabled")
val enabled: Boolean,
)

0 comments on commit 76b39b4

Please sign in to comment.