Skip to content

Commit

Permalink
Fix conversion commands (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 authored Dec 14, 2023
1 parent afe77a8 commit 656a956
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class MappingsExtension : Extension() {
MappingsConfig::class
)

private val namespaceCache = mutableMapOf<Snowflake, Map<String, String>>()

override suspend fun setup() {
// Fix issue where Linkie doesn't create its cache directory
val cacheDirectory = Path("./.linkie-cache")
Expand Down Expand Up @@ -529,16 +531,6 @@ class MappingsExtension : Extension() {

// region: Mapping conversions

var enabledNamespaces = mutableListOf<String>()

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)

Expand All @@ -560,6 +552,16 @@ class MappingsExtension : Extension() {
}
}

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

publicSlashCommand {
name = "convert"
description = "Convert mappings across namespaces"
Expand All @@ -573,6 +575,9 @@ class MappingsExtension : Extension() {
description = "Convert a class mapping"

action {
val config = guild?.config()
val enabledNamespaces = config?.namespaces ?: namespaceNames.toList()

convertMapping(
"class",
MappingsQuery::queryClasses,
Expand All @@ -592,6 +597,9 @@ class MappingsExtension : Extension() {
description = "Convert a field mapping"

action {
val config = guild?.config()
val enabledNamespaces = config?.namespaces ?: namespaceNames.toList()

convertMapping(
"field",
MappingsQuery::queryFields,
Expand All @@ -618,6 +626,9 @@ class MappingsExtension : Extension() {
description = "Convert a method mapping"

action {
val config = guild?.config()
val enabledNamespaces = config?.namespaces ?: namespaceNames.toList()

convertMapping(
"method",
MappingsQuery::queryMethods,
Expand All @@ -642,6 +653,9 @@ class MappingsExtension : Extension() {
description = "Get information about /convert and its subcommands"

action {
val config = guild?.config()
val enabledNamespaces = config?.namespaces ?: namespaceNames.toList()

val pages = mutableListOf<String>()
pages.add(
"Mapping conversions are available for any Minecraft version with multiple mapping sets.\n\n" +
Expand Down Expand Up @@ -785,8 +799,6 @@ class MappingsExtension : Extension() {

config.namespaces = currentNamespaces
configUnit.save(config)
// Set the namespaces for the conversion commands to update
enabledNamespaces = currentNamespaces

respond {
content = context.translate(
Expand All @@ -804,7 +816,7 @@ class MappingsExtension : Extension() {

// endregion

logger.info { "Mappings extension set up - namespaces: " + enabledNamespaces.joinToString(", ") }
logger.info { "Mappings extension set up - namespaces: " + namespaceNames.joinToString(", ") }
}

private suspend fun <A, B> MappingSlashCommand.queryMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import com.kotlindiscord.kord.extensions.modules.extra.mappings.utils.autocomple
import com.kotlindiscord.kord.extensions.modules.extra.mappings.utils.toNamespace
import com.kotlindiscord.kord.extensions.utils.suggestStringMap
import dev.kord.common.entity.Snowflake
import me.shedaniel.linkie.utils.tryToVersion

/**
* Arguments for class, field, and method conversion commands.
*/
@Suppress("UndocumentedPublicProperty")
class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> 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"
Expand All @@ -32,13 +33,15 @@ class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> Map<

autoComplete {
val guildId = command.data.guildId.value
val values = enabledNamespaces(guildId) ?: emptyMap()
val values = enabledNamespaces(guildId)
suggestStringMap(values)
}

@Suppress("UnnecessaryParentheses")
validate {
failIf("Must be a valid namespace") { value !in (enabledNamespaces(context.getGuild()!!.id) ?: emptyMap()) }
failIf("Must be a valid namespace") {
context.getGuild() != null && value !in enabledNamespaces(context.getGuild()!!.id)
}
}
}

Expand All @@ -48,13 +51,15 @@ class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> Map<

autoComplete {
val guildId = command.data.guildId.value
val values = enabledNamespaces(guildId) ?: emptyMap()
val values = enabledNamespaces(guildId)
suggestStringMap(values)
}

@Suppress("UnnecessaryParentheses")
validate {
failIf("Must be a valid namespace") { value !in (enabledNamespaces(context.getGuild()!!.id) ?: emptyMap()) }
failIf("Must be a valid namespace") {
context.getGuild() != null && value !in enabledNamespaces(context.getGuild()!!.id)
}
}
}

Expand All @@ -69,7 +74,8 @@ class MappingConversionArguments(enabledNamespaces: suspend (Snowflake?) -> Map<
if (inputNamespace == null || outputNamespace == null) {
emptyList()
} else {
inputNamespace.getAllSortedVersions().filter { it in outputNamespace.getAllSortedVersions() }
inputNamespace.getAllVersions().toSet().intersect(outputNamespace.getAllVersions().toSet())
.sortedByDescending { it.tryToVersion() }
}
}
}
Expand Down

0 comments on commit 656a956

Please sign in to comment.