Skip to content

Commit

Permalink
Automatically fix common mistakes in config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-korsa committed Jul 16, 2022
1 parent 5eaa3e4 commit 1e8179b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.dominikkorsa.discordintegration.DiscordIntegration
import dev.dejvokep.boostedyaml.block.implementation.Section
import dev.dejvokep.boostedyaml.route.Route
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings
import java.util.regex.Pattern

class ConfigManager(plugin: DiscordIntegration) : CustomConfig(plugin, "config.yml") {
override fun setUpdateSettings(builder: UpdaterSettings.Builder) {
super.setUpdateSettings(builder)
setUpdate5Settings(builder)
}

Expand All @@ -28,6 +30,27 @@ class ConfigManager(plugin: DiscordIntegration) : CustomConfig(plugin, "config.y
}
}

private fun fixStringList(route: Route) {
if (config.isList(route)) return
config.set(route, listOf(config.getString(route)))
}

private fun fixChannelListURL(route: Route) {
config.set(route, config.getStringList(route).map {
val matcher = channelUrlPattern.matcher(it)
if (matcher.matches()) matcher.group(1) else it
})
}

override fun applyFixes() {
super.applyFixes()
fixStringList(Route.from("chat", "channels"))
fixStringList(Route.from("chat", "webhooks"))
fixStringList(Route.from("chat", "console-channels"))
fixChannelListURL(Route.from("chat", "channels"))
fixChannelListURL(Route.from("chat", "console-channels"))
}

class Chat(private val section: Section) {
open class Embed(private val section: Section) {
val enabled get() = section.requireBoolean("enabled")
Expand Down Expand Up @@ -76,4 +99,8 @@ class ConfigManager(plugin: DiscordIntegration) : CustomConfig(plugin, "config.y
val activity get() = Activity(config.getSection("activity"))
val linking get() = Linking(config.getSection("linking"))
val debug get() = Debug(config.getSection("debug"))

companion object {
private val channelUrlPattern = Pattern.compile("""^https://(?:ptb\.)?discord\.com/channels/\d+/(\d+)$""")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ open class CustomConfig(

protected open fun setUpdateSettings(builder: UpdaterSettings.Builder) {}

protected open fun applyFixes() {}

fun reload() {
config.reload()
applyFixes()
config.update()
config.save()
}
}
3 changes: 2 additions & 1 deletion plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Do not edit the file-version!
file-version: 5

discord-token: # YOUR DISCORD TOKEN HERE
# Put your Discord token here:
discord-token:
chat:
channels:
# - 0123456789
Expand Down

0 comments on commit 1e8179b

Please sign in to comment.