-
Notifications
You must be signed in to change notification settings - Fork 0
Started config thingies #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Wikijito7
wants to merge
13
commits into
master
Choose a base branch
from
feature/52-commands-to-set-and-reload-configuration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
958aa06
Started config thingies
Wikijito7 540b3e6
Merge branch 'master' into feature/52-commands-to-set-and-reload-conf…
Wikijito7 9e19b16
fixed lint errors and added skills
Wikijito7 a61a52e
fixed tests
Wikijito7 2daf02e
feat: replace ktlint with detekt for code quality checks
Wikijito7 1ea4ae4
fix: resolve detekt warnings and improve code quality
Wikijito7 7581260
fix: resolve detekt MaxLineLength violations and add suppressions wit…
Wikijito7 5ed5648
fix: resolve detekt issues and disable InvalidPackageDeclaration
Wikijito7 7a44999
Merge branch 'feature/52-commands-to-set-and-reload-configuration' in…
Wikijito7 ef4c938
fixed tab error
Wikijito7 d22467a
fix: use gradle/actions@v4 instead of @master
Wikijito7 8361b46
fix: remove unused imports in ConfigGroupCommandTest and ConfigSetCom…
Wikijito7 a71bfbf
fix: resolve compilation errors and sonar issues
Wikijito7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package es.wokis.commands.config | ||
|
|
||
| import dev.kord.core.Kord | ||
| import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior | ||
| import dev.kord.core.behavior.interaction.response.respond | ||
| import dev.kord.core.entity.interaction.AutoCompleteInteraction | ||
| import dev.kord.core.entity.interaction.ChatInputCommandInteraction | ||
| import dev.kord.core.entity.interaction.SubCommand as KordSubCommand | ||
| import es.wokis.commands.Autocomplete | ||
| import es.wokis.commands.CommandName | ||
| import es.wokis.commands.Component | ||
| import es.wokis.commands.GroupCommand | ||
| import es.wokis.localization.LocalizationKeys | ||
| import es.wokis.services.localization.LocalizationService | ||
| import es.wokis.utils.orDefaultLocale | ||
|
|
||
| class ConfigGroupCommand( | ||
| private val configReloadCommand: ConfigReloadCommand, | ||
| private val configSetCommand: ConfigSetCommand, | ||
| private val configGetCommand: ConfigGetCommand, | ||
| private val localizationService: LocalizationService | ||
| ) : GroupCommand, Component, Autocomplete { | ||
|
|
||
| override suspend fun onRegisterCommand(kord: Kord) { | ||
| kord.createGlobalChatInputCommand(CommandName.Config.commandName, localizationService.getString(LocalizationKeys.CONFIG_COMMAND_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_COMMAND_DESCRIPTION) | ||
| configReloadCommand.onRegisterCommand(this) | ||
| configSetCommand.onRegisterCommand(this) | ||
| configGetCommand.onRegisterCommand(this) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onExecute( | ||
| interaction: ChatInputCommandInteraction, | ||
| response: DeferredPublicMessageInteractionResponseBehavior | ||
| ) { | ||
| val commandName = (interaction.command as? KordSubCommand)?.name | ||
| commandName?.let { | ||
| when (commandName) { | ||
| CommandName.Config.Reload.commandName -> configReloadCommand.onExecute(interaction, response) | ||
| CommandName.Config.Set.commandName -> configSetCommand.onExecute(interaction, response) | ||
| CommandName.Config.Get.commandName -> configGetCommand.onExecute(interaction, response) | ||
| } | ||
| } ?: response.respond { | ||
| val locale = interaction.guildLocale.orDefaultLocale() | ||
| content = localizationService.getString(LocalizationKeys.ERROR_UNEXPECTED, locale) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onInteract(interaction: dev.kord.core.entity.interaction.ComponentInteraction) { | ||
| } | ||
|
|
||
| override suspend fun onAutoComplete(interaction: AutoCompleteInteraction) { | ||
| val subCommandName = (interaction.command as? KordSubCommand)?.name | ||
| when (subCommandName) { | ||
| CommandName.Config.Get.commandName -> configGetCommand.onAutoComplete(interaction) | ||
| } | ||
| } | ||
| } |
93 changes: 93 additions & 0 deletions
93
src/main/kotlin/commands/config/subcommands/get/ConfigGetCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package es.wokis.commands.config | ||
|
|
||
| import dev.kord.common.entity.Choice | ||
| import dev.kord.common.entity.optional.Optional | ||
| import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior | ||
| import dev.kord.core.behavior.interaction.response.respond | ||
| import dev.kord.core.behavior.interaction.suggest | ||
| import dev.kord.core.entity.interaction.AutoCompleteInteraction | ||
| import dev.kord.core.entity.interaction.ChatInputCommandInteraction | ||
| import dev.kord.rest.builder.interaction.GlobalChatInputCreateBuilder | ||
| import dev.kord.rest.builder.interaction.string | ||
| import dev.kord.rest.builder.interaction.subCommand | ||
| import es.wokis.commands.Autocomplete | ||
| import es.wokis.commands.CommandName | ||
| import es.wokis.commands.SubCommand | ||
| import es.wokis.localization.LocalizationKeys | ||
| import es.wokis.services.config.ConfigService | ||
| import es.wokis.services.localization.LocalizationService | ||
| import es.wokis.utils.orDefaultLocale | ||
|
|
||
| private const val ARGUMENT_SECTION = "section" | ||
|
|
||
| class ConfigGetCommand( | ||
| private val configService: ConfigService, | ||
| private val localizationService: LocalizationService | ||
| ) : SubCommand, Autocomplete { | ||
|
|
||
| private val validSections = listOf("database", "youtube", "deezer", "spotify", "tidal", "kokoro") | ||
|
|
||
| override suspend fun onRegisterCommand(builder: GlobalChatInputCreateBuilder) { | ||
| builder.apply { | ||
| subCommand(CommandName.Config.Get.commandName, localizationService.getString(LocalizationKeys.CONFIG_GET_COMMAND_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_GET_SECTION_DESCRIPTION) | ||
| string(ARGUMENT_SECTION, localizationService.getString(LocalizationKeys.CONFIG_GET_SECTION_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_GET_SECTION_DESCRIPTION) | ||
| required = true | ||
| autocomplete = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onExecute( | ||
| interaction: ChatInputCommandInteraction, | ||
| response: DeferredPublicMessageInteractionResponseBehavior | ||
| ) { | ||
| val locale = interaction.guildLocale.orDefaultLocale() | ||
| val section = interaction.command.strings[ARGUMENT_SECTION] | ||
|
|
||
| if (section == null || section !in validSections) { | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.CONFIG_INVALID_SECTION, locale) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| val config = configService.config | ||
| val sectionData = when (section) { | ||
| "database" -> config.database | ||
| "youtube" -> config.youtube | ||
| "deezer" -> config.deezer | ||
| "spotify" -> config.spotify | ||
| "tidal" -> config.tidal | ||
| "kokoro" -> config.kokoro | ||
| else -> null | ||
| } | ||
|
|
||
| response.respond { | ||
| content = localizationService.getStringFormat( | ||
| LocalizationKeys.CONFIG_GET_DISPLAY, | ||
| locale, | ||
| arrayOf(section, sectionData.toString()) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onAutoComplete(interaction: AutoCompleteInteraction) { | ||
| val input = interaction.command.strings[ARGUMENT_SECTION].orEmpty().lowercase() | ||
|
|
||
| val suggestions = validSections | ||
| .filter { it.lowercase().contains(input) } | ||
| .take(25) | ||
| .map { section -> | ||
| Choice.StringChoice( | ||
| name = section, | ||
| nameLocalizations = Optional.Missing(), | ||
| value = section | ||
| ) | ||
| } | ||
|
|
||
| interaction.suggest(suggestions) | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/commands/config/subcommands/reload/ConfigReloadCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package es.wokis.commands.config | ||
|
|
||
| import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior | ||
| import dev.kord.core.behavior.interaction.response.respond | ||
| import dev.kord.core.entity.interaction.ChatInputCommandInteraction | ||
| import dev.kord.rest.builder.interaction.GlobalChatInputCreateBuilder | ||
| import dev.kord.rest.builder.interaction.subCommand | ||
| import es.wokis.commands.CommandName | ||
| import es.wokis.commands.SubCommand | ||
| import es.wokis.localization.LocalizationKeys | ||
| import es.wokis.services.config.ConfigService | ||
| import es.wokis.services.localization.LocalizationService | ||
| import es.wokis.utils.Log | ||
| import es.wokis.utils.orDefaultLocale | ||
|
|
||
| class ConfigReloadCommand( | ||
| private val configService: ConfigService, | ||
| private val localizationService: LocalizationService | ||
| ) : SubCommand { | ||
|
|
||
| override suspend fun onRegisterCommand(builder: GlobalChatInputCreateBuilder) { | ||
| builder.apply { | ||
| subCommand(CommandName.Config.Reload.commandName, localizationService.getString(LocalizationKeys.CONFIG_RELOAD_COMMAND_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_RELOAD_COMMAND_DESCRIPTION) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onExecute( | ||
| interaction: ChatInputCommandInteraction, | ||
| response: DeferredPublicMessageInteractionResponseBehavior | ||
| ) { | ||
| val locale = interaction.guildLocale.orDefaultLocale() | ||
| try { | ||
| configService.reload() | ||
| Log.info("Configuration reloaded via command by user ${interaction.user.id}") | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.CONFIG_RELOAD_SUCCESS, locale) | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.error("Failed to reload config via command", e) | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.ERROR_UNEXPECTED, locale) | ||
| } | ||
| } | ||
| } | ||
| } |
147 changes: 147 additions & 0 deletions
147
src/main/kotlin/commands/config/subcommands/set/ConfigSetCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package es.wokis.commands.config | ||
|
|
||
| import dev.kord.common.entity.Permissions | ||
| import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior | ||
| import dev.kord.core.behavior.interaction.response.respond | ||
| import dev.kord.core.entity.interaction.ChatInputCommandInteraction | ||
| import dev.kord.rest.builder.interaction.GlobalChatInputCreateBuilder | ||
| import dev.kord.rest.builder.interaction.string | ||
| import dev.kord.rest.builder.interaction.subCommand | ||
| import es.wokis.commands.CommandName | ||
| import es.wokis.commands.SubCommand | ||
| import es.wokis.localization.LocalizationKeys | ||
| import es.wokis.services.config.ConfigService | ||
| import es.wokis.services.localization.LocalizationService | ||
| import es.wokis.utils.Log | ||
| import es.wokis.utils.orDefaultLocale | ||
| import java.io.File | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
|
|
||
| private const val ARGUMENT_SECTION = "section" | ||
| private const val ARGUMENT_KEY = "key" | ||
| private const val ARGUMENT_VALUE = "value" | ||
| private const val CONFIG_PATH = "./data/config.json" | ||
|
|
||
| class ConfigSetCommand( | ||
| private val configService: ConfigService, | ||
| private val localizationService: LocalizationService | ||
| ) : SubCommand { | ||
|
|
||
| private val validSections = mapOf( | ||
| "database" to listOf("enabled", "username", "password", "database"), | ||
| "youtube" to listOf("enabled", "oauth2_token", "po_token", "visitor_data", "remote_cipher_url", "remote_cipher_password"), | ||
| "deezer" to listOf("enabled", "master_decryption_key", "arl_token"), | ||
| "spotify" to listOf("enabled", "client_id", "client_secret", "custom_endpoint"), | ||
| "tidal" to listOf("enabled", "country_code", "token"), | ||
| "kokoro" to listOf("enabled", "base_url", "default_voice", "default_speed", "default_lang_code") | ||
| ) | ||
|
|
||
| override suspend fun onRegisterCommand(builder: GlobalChatInputCreateBuilder) { | ||
| builder.apply { | ||
| subCommand(CommandName.Config.Set.commandName, localizationService.getString(LocalizationKeys.CONFIG_SET_COMMAND_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_SET_COMMAND_DESCRIPTION) | ||
| string(ARGUMENT_SECTION, localizationService.getString(LocalizationKeys.CONFIG_SET_SECTION_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_SET_SECTION_DESCRIPTION) | ||
| required = true | ||
| } | ||
| string(ARGUMENT_KEY, localizationService.getString(LocalizationKeys.CONFIG_SET_KEY_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_SET_KEY_DESCRIPTION) | ||
| required = true | ||
| } | ||
| string(ARGUMENT_VALUE, localizationService.getString(LocalizationKeys.CONFIG_SET_VALUE_DESCRIPTION)) { | ||
| descriptionLocalizations = localizationService.getLocalizations(LocalizationKeys.CONFIG_SET_VALUE_DESCRIPTION) | ||
| required = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun onExecute( | ||
| interaction: ChatInputCommandInteraction, | ||
| response: DeferredPublicMessageInteractionResponseBehavior | ||
| ) { | ||
| val locale = interaction.guildLocale.orDefaultLocale() | ||
| val section = interaction.command.strings[ARGUMENT_SECTION] | ||
| val key = interaction.command.strings[ARGUMENT_KEY] | ||
| val value = interaction.command.strings[ARGUMENT_VALUE] | ||
|
|
||
| if (section == null || section !in validSections) { | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.CONFIG_INVALID_SECTION, locale) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (key == null || key !in validSections[section]!!) { | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.CONFIG_INVALID_KEY, locale) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (value.isNullOrEmpty()) { | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.ERROR_NO_CONTENT_PROVIDED, locale) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (section == "discord_bot_token" || (section == "database" && key == "password")) { | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.CONFIG_CANNOT_MODIFY_TOKEN, locale) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| updateConfigValue(section, key, value!!) | ||
| configService.reload() | ||
| Log.info("Config updated via command: $section.$key = $value by user ${interaction.user.id}") | ||
| response.respond { | ||
| content = localizationService.getStringFormat( | ||
| LocalizationKeys.CONFIG_SET_SUCCESS, | ||
| locale, | ||
| arrayOf("$section.$key", value) | ||
| ) | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.error("Failed to update config via command", e) | ||
| response.respond { | ||
| content = localizationService.getString(LocalizationKeys.ERROR_UNEXPECTED, locale) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun updateConfigValue(section: String, key: String, value: String) { | ||
| val configFile = File(CONFIG_PATH) | ||
| val json = Json { prettyPrint = true } | ||
| val configJson = json.parseToJsonElement(configFile.readText()) as JsonObject | ||
| val sectionJson = configJson[section] as? JsonObject | ||
| ?: throw IllegalStateException("Section $section not found in config") | ||
|
|
||
| val updatedSectionJson = JsonObject( | ||
| sectionJson.toMutableMap().apply { | ||
| val newValue = when { | ||
| value.lowercase() == "true" -> JsonPrimitive(true) | ||
| value.lowercase() == "false" -> JsonPrimitive(false) | ||
| value.toDoubleOrNull() != null -> JsonPrimitive(value.toDouble()) | ||
| else -> JsonPrimitive(value) | ||
| } | ||
| put(key, newValue) | ||
| } | ||
| ) | ||
|
|
||
| val updatedConfigJson = JsonObject( | ||
| configJson.toMutableMap().apply { | ||
| put(section, updatedSectionJson) | ||
| } | ||
| ) | ||
|
|
||
| configFile.writeText(json.encodeToString( | ||
| JsonObject.serializer(), | ||
| updatedConfigJson | ||
| )) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.