Skip to content

Commit 21bc172

Browse files
committed
Run declaration filters on top-level slash commands if no subcommands
Fixes subcommands being filtered by mistake because they were applied on the top level
1 parent cb67575 commit 21bc172

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/commands/application/autobuilder/MessageContextCommandAutoBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ internal class MessageContextCommandAutoBuilder(
7474
}
7575
}
7676

77+
context(_: SkipLogger)
7778
private fun processMessageCommand(manager: AbstractApplicationCommandManager, metadata: MessageContextFunctionMetadata) {
7879
val func = metadata.func
7980
val instance = metadata.instance
8081
val path = metadata.path
8182
val commandId = metadata.commandId
8283

84+
if (!checkDeclarationFilter(manager, metadata.func, path, metadata.commandId))
85+
return // Already logged
86+
8387
val annotation = metadata.annotation
8488
manager.messageCommand(path.name, func.castFunction()) {
8589
fillCommandBuilder(func)

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/commands/application/autobuilder/SlashCommandAutoBuilder.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ internal class SlashCommandAutoBuilder(
242242
val subcommandsMetadata = topLevelMetadata.subcommands
243243
val subcommandGroupsMetadata = topLevelMetadata.subcommandGroups
244244
val isTopLevelOnly = subcommandsMetadata.isEmpty() && subcommandGroupsMetadata.isEmpty()
245+
246+
// Check we don't have subcommands before filtering, else it would filter out all of them
247+
if (isTopLevelOnly && !checkDeclarationFilter(manager, metadata.func, path, metadata.commandId))
248+
return // Already logged
249+
245250
manager.slashCommand(name, if (isTopLevelOnly) metadata.func.castFunction() else null) {
246251
contexts = if (forceGuildCommands) {
247252
setOf(InteractionContextType.GUILD)

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/commands/application/autobuilder/UserContextCommandAutoBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ internal class UserContextCommandAutoBuilder(
7474
}
7575
}
7676

77+
context(_: SkipLogger)
7778
private fun processUserCommand(manager: AbstractApplicationCommandManager, metadata: UserContextFunctionMetadata) {
7879
val func = metadata.func
7980
val instance = metadata.instance
8081
val path = metadata.path
8182
val commandId = metadata.commandId
8283

84+
if (!checkDeclarationFilter(manager, metadata.func, path, metadata.commandId))
85+
return // Already logged
86+
8387
val annotation = metadata.annotation
8488
manager.userCommand(path.name, func.castFunction()) {
8589
fillCommandBuilder(func)

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/commands/autobuilder/AutoBuilderUtils.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ internal fun runFiltered(
5353
block: () -> Unit
5454
) {
5555
val path = applicationFunctionMetadata.path
56-
val commandId = applicationFunctionMetadata.commandId
5756
val func = applicationFunctionMetadata.func
5857

5958
// On global manager, do not register any command if forceGuildCommands is enabled,
@@ -70,9 +69,6 @@ internal fun runFiltered(
7069
if (requiredScope != scope) return
7170
}
7271

73-
if (!checkDeclarationFilter(manager, func, path, commandId))
74-
return // Already logged
75-
7672
val testState = checkTestCommand(manager, func, scope, manager.context)
7773
if (scope == CommandScope.GLOBAL && testState != TestState.NO_ANNOTATION)
7874
throwInternal("Test commands on a global scope should have thrown in ${::checkTestCommand.shortSignatureNoSrc}")
@@ -214,4 +210,4 @@ internal fun CommandAutoBuilder.requireServiceOptionOrOptional(func: KFunction<*
214210
"if this is a Discord option, use @${optionAnnotation.simpleNestedName}, check @${commandAnnotation.simpleNestedName} for more details\n" +
215211
serviceError.toDetailedString()
216212
)
217-
}
213+
}

test-bot/src/test/kotlin/dev/freya02/botcommands/bot/commands/slash/SlashDeclarationFilter.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class SlashDeclarationFilter : ApplicationCommand() {
3939
event.reply_("Works, guild members: ${event.guild.memberCount}", ephemeral = true).await()
4040
}
4141

42+
@DeclarationFilter(ImpossibleDeclarationFilter::class)
4243
@JDASlashCommand(name = "declaration_filter_subcommand", subcommand = "subcommand")
4344
@TopLevelSlashCommandData(scope = CommandScope.GUILD)
44-
suspend fun onSlashDeclarationFilterSubcommand(event: GuildSlashEvent) {
45-
event.reply_("Works", ephemeral = true).await()
45+
fun onSlashDeclarationFilterSubcommand(event: GuildSlashEvent) {
46+
throw AssertionError("Cannot run")
4647
}
4748

48-
@DeclarationFilter(ImpossibleDeclarationFilter::class)
4949
@JDASlashCommand(name = "declaration_filter_subcommand", group = "group", subcommand = "subcommand")
50-
fun onSlashDeclarationFilterSubcommandGroupSubcommand(event: GuildSlashEvent) {
51-
throw AssertionError("Cannot run")
50+
suspend fun onSlashDeclarationFilterSubcommandGroupSubcommand(event: GuildSlashEvent) {
51+
event.reply_("Works", ephemeral = true).await()
5252
}
5353
}

0 commit comments

Comments
 (0)