From 3e047a5e74a4c7b64c11a680383a2e80a5480e42 Mon Sep 17 00:00:00 2001 From: NoComment1105 Date: Fri, 6 Oct 2023 09:46:01 +0100 Subject: [PATCH] Apply patch from @sschr15 --- .../extra/mappings/MappingsExtension.kt | 57 +++++++++++-------- .../arguments/MappingConversionArguments.kt | 37 +++++++++--- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/MappingsExtension.kt b/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/MappingsExtension.kt index aa7c63d1b8..57251864a4 100644 --- a/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/MappingsExtension.kt +++ b/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/MappingsExtension.kt @@ -27,7 +27,6 @@ import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand import com.kotlindiscord.kord.extensions.modules.extra.mappings.arguments.* import com.kotlindiscord.kord.extensions.modules.extra.mappings.builders.ExtMappingsBuilder import com.kotlindiscord.kord.extensions.modules.extra.mappings.enums.Channels -import com.kotlindiscord.kord.extensions.modules.extra.mappings.exceptions.UnsupportedNamespaceException import com.kotlindiscord.kord.extensions.modules.extra.mappings.storage.MappingsConfig import com.kotlindiscord.kord.extensions.modules.extra.mappings.utils.* import com.kotlindiscord.kord.extensions.pagination.EXPAND_EMOJI @@ -40,11 +39,13 @@ import com.kotlindiscord.kord.extensions.storage.StorageType import com.kotlindiscord.kord.extensions.storage.StorageUnit import com.kotlindiscord.kord.extensions.types.respond import dev.kord.common.entity.Permission +import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.GuildBehavior import dev.kord.core.event.interaction.ChatInputCommandInteractionCreateEvent import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.* import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.withContext import me.shedaniel.linkie.* @@ -530,34 +531,44 @@ class MappingsExtension : Extension() { // region: Mapping conversions var enabledNamespaces = mutableListOf() - val namespaces = mutableListOf() + + val namespaceGetter: suspend (Snowflake?) -> Map? = { guildId -> + if (guildId == null) { + null + } else { + GuildBehavior(guildId, kord).config().namespaces.associateBy { it.lowercase() } + } + } + + val namespaceNames = mutableSetOf() + kord.guilds.flatMapMerge { it.config().namespaces.toList().asFlow() }.toSet(namespaceNames) + + val namespaces = namespaceNames.map { + when (it) { + "barn" -> BarnNamespace + "feather" -> FeatherNamespace + "hashed-mojang" -> MojangHashedNamespace + "legacy-yarn" -> LegacyYarnNamespace + "mcp" -> McpNamespaceReplacement + "mojang" -> MojangNamespace + "plasma" -> PlasmaNamespace + "quilt-mappings" -> QuiltMappingsNamespace + "srg-mojang" -> MojangSrgNamespace + "yarn" -> YarnNamespace + "yarrn" -> YarrnNamespace + + else -> error("Unknown namespace: $it") + } + } publicSlashCommand { name = "convert" description = "Convert mappings across namespaces" - val configNamespaces = GuildBehavior(guildId!!, kord).config().namespaces - - enabledNamespaces.addAll(configNamespaces) - configNamespaces.forEach { - when (it) { - "legacy-yarn" -> namespaces.add(LegacyYarnNamespace) - "mcp" -> namespaces.add(McpNamespaceReplacement) - "mojang" -> namespaces.add(MojangNamespace) - "hashed-mojang" -> namespaces.add(MojangHashedNamespace) - "plasma" -> namespaces.add(PlasmaNamespace) - "quilt-mappings" -> namespaces.add(QuiltMappingsNamespace) - "yarn" -> namespaces.add(YarnNamespace) - "yarrn" -> namespaces.add(YarrnNamespace) - - else -> throw UnsupportedNamespaceException(it) - } - } - Namespaces.init(LinkieConfig.DEFAULT.copy(namespaces = namespaces)) publicSubCommand( - { MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) } + { MappingConversionArguments(namespaceGetter) } ) { name = "class" description = "Convert a class mapping" @@ -576,7 +587,7 @@ class MappingsExtension : Extension() { } publicSubCommand( - { MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) } + { MappingConversionArguments(namespaceGetter) } ) { name = "field" description = "Convert a field mapping" @@ -602,7 +613,7 @@ class MappingsExtension : Extension() { } publicSubCommand( - { MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) } + { MappingConversionArguments(namespaceGetter) } ) { name = "method" description = "Convert a method mapping" diff --git a/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/arguments/MappingConversionArguments.kt b/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/arguments/MappingConversionArguments.kt index e1ca686119..323ea2d4f3 100644 --- a/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/arguments/MappingConversionArguments.kt +++ b/extra-modules/extra-mappings/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/extra/mappings/arguments/MappingConversionArguments.kt @@ -8,36 +8,55 @@ package com.kotlindiscord.kord.extensions.modules.extra.mappings.arguments import com.kotlindiscord.kord.extensions.commands.Arguments import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.optionalEnumChoice -import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString import com.kotlindiscord.kord.extensions.commands.converters.impl.string import com.kotlindiscord.kord.extensions.modules.extra.mappings.enums.Channels import com.kotlindiscord.kord.extensions.modules.extra.mappings.utils.autocompleteVersions import com.kotlindiscord.kord.extensions.modules.extra.mappings.utils.toNamespace +import com.kotlindiscord.kord.extensions.utils.suggestStringMap +import dev.kord.common.entity.Snowflake /** * Arguments for class, field, and method conversion commands. */ @Suppress("UndocumentedPublicProperty") -class MappingConversionArguments(enabledNamespaces: Map) : Arguments() { +class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> Map?) : Arguments() { val query by string { name = "query" description = "Name to query mappings for" } - val inputNamespace by stringChoice { + val inputNamespace by string { name = "input" description = "The namespace to convert from" - choices(enabledNamespaces) + autoComplete { + val guildId = command.data.guildId.value + val values = enabledNamespaces(guildId) ?: emptyMap() + suggestStringMap(values) + } + + @Suppress("UnnecessaryParentheses") + validate { + failIf("Must be a valid namespace") { value !in (enabledNamespaces(context.getGuild()!!.id) ?: emptyMap()) } + } } - val outputNamespace by stringChoice { - name = "output" - description = "The namespace to convert to" + val outputNamespace by string { + name = "output" + description = "The namespace to convert to" - choices(enabledNamespaces) - } + autoComplete { + val guildId = command.data.guildId.value + val values = enabledNamespaces(guildId) ?: emptyMap() + suggestStringMap(values) + } + + @Suppress("UnnecessaryParentheses") + validate { + failIf("Must be a valid namespace") { value !in (enabledNamespaces(context.getGuild()!!.id) ?: emptyMap()) } + } + } val version by optionalString { name = "version"