Skip to content

Commit

Permalink
Apply patch from @sschr15
Browse files Browse the repository at this point in the history
  • Loading branch information
NoComment1105 committed Oct 6, 2023
1 parent 4e176e2 commit 3e047a5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*
Expand Down Expand Up @@ -530,34 +531,44 @@ class MappingsExtension : Extension() {
// region: Mapping conversions

var enabledNamespaces = mutableListOf<String>()
val namespaces = mutableListOf<Namespace>()

val namespaceGetter: suspend (Snowflake?) -> Map<String, String>? = { guildId ->
if (guildId == null) {
null
} else {
GuildBehavior(guildId, kord).config().namespaces.associateBy { it.lowercase() }
}
}

val namespaceNames = mutableSetOf<String>()
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>(
{ MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) }
{ MappingConversionArguments(namespaceGetter) }
) {
name = "class"
description = "Convert a class mapping"
Expand All @@ -576,7 +587,7 @@ class MappingsExtension : Extension() {
}

publicSubCommand<MappingConversionArguments>(
{ MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) }
{ MappingConversionArguments(namespaceGetter) }
) {
name = "field"
description = "Convert a field mapping"
Expand All @@ -602,7 +613,7 @@ class MappingsExtension : Extension() {
}

publicSubCommand<MappingConversionArguments>(
{ MappingConversionArguments(enabledNamespaces.associateBy { it.lowercase() }) }
{ MappingConversionArguments(namespaceGetter) }
) {
name = "method"
description = "Convert a method mapping"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>) : Arguments() {
class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> Map<String, String>?) : 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"
Expand Down

0 comments on commit 3e047a5

Please sign in to comment.