Skip to content

Commit

Permalink
Only store one hashmap for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Feb 17, 2024
1 parent 0f62baa commit 08a1b73
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/me/devoxin/flight/api/CommandClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CommandClient(
?: commands.values.firstOrNull { it.properties.aliases.contains(command) }
?: return dispatchSafely { it.onUnknownCommand(event, command, args) }

val subcommand = args.firstOrNull()?.lowercase().let { cmd.subcommands[it] ?: cmd.subcommandAliases[it] }
val subcommand = args.firstOrNull()?.lowercase().let { cmd.subcommands[it] }
val invoked = subcommand ?: cmd

if (subcommand != null) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/me/devoxin/flight/api/CommandFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class CommandFunction(
) : Executable(name, method, cog, arguments, contextParameter) {
val contextType: ContextType
val subcommands = hashMapOf<String, SubCommandFunction>()
val subcommandAliases = hashMapOf<String, SubCommandFunction>()

@Deprecated("Use #subcommands with a mapping/filter function.")
val subcommandAliases: Map<String, SubCommandFunction>
get() = subcommands.values.flatMap { it.properties.aliases.map { a -> a to it } }
.associateBy({ it.first }) { it.second }

init {
val jvmCtx = contextParameter.type
Expand All @@ -44,13 +48,13 @@ class CommandFunction(
subcommands[sc.name] = sc

for (trigger in sc.properties.aliases) {
val existing = subcommandAliases[trigger]
val existing = subcommands[trigger]

if (existing != null) {
throw IllegalStateException("The trigger '$trigger' for sub-command '${sc.name}' within command '$name' is already assigned to '${existing.name}'!")
}

subcommandAliases[trigger] = sc
subcommands[trigger] = sc
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CommandRegistry : HashMap<String, CommandFunction>() {
.setNSFW(command.properties.nsfw)

if (command.subcommands.isNotEmpty()) {
for (sc in command.subcommands.values) {
for (sc in command.subcommands.values.toSet()) {
val scData = SubcommandData(sc.name, sc.properties.description)

if (sc.arguments.isNotEmpty()) {
Expand Down

0 comments on commit 08a1b73

Please sign in to comment.