|
| 1 | +package io.github.freya022.botcommands.internal.commands.application.autobuilder |
| 2 | + |
| 3 | +import io.github.freya022.botcommands.api.commands.application.ApplicationCommandFilter |
| 4 | +import io.github.freya022.botcommands.api.commands.application.CommandScope |
| 5 | +import io.github.freya022.botcommands.api.commands.application.annotations.DeclarationFilter |
| 6 | +import io.github.freya022.botcommands.api.commands.application.annotations.Test |
| 7 | +import io.github.freya022.botcommands.api.commands.application.builder.ApplicationCommandBuilder |
| 8 | +import io.github.freya022.botcommands.api.commands.application.provider.* |
| 9 | +import io.github.freya022.botcommands.api.commands.text.annotations.NSFW |
| 10 | +import io.github.freya022.botcommands.api.core.BContext |
| 11 | +import io.github.freya022.botcommands.api.core.config.BApplicationConfig |
| 12 | +import io.github.freya022.botcommands.api.core.objectLogger |
| 13 | +import io.github.freya022.botcommands.api.core.utils.findAllAnnotations |
| 14 | +import io.github.freya022.botcommands.api.core.utils.hasAnnotationRecursive |
| 15 | +import io.github.freya022.botcommands.api.core.utils.simpleNestedName |
| 16 | +import io.github.freya022.botcommands.internal.commands.SkipLogger |
| 17 | +import io.github.freya022.botcommands.internal.commands.application.autobuilder.metadata.ApplicationFunctionMetadata |
| 18 | +import io.github.freya022.botcommands.internal.commands.application.autobuilder.metadata.RootAnnotatedApplicationCommand |
| 19 | +import io.github.freya022.botcommands.internal.commands.autobuilder.CommandAutoBuilder |
| 20 | +import io.github.freya022.botcommands.internal.commands.autobuilder.forEachWithDelayedExceptions |
| 21 | +import io.github.freya022.botcommands.internal.utils.* |
| 22 | +import net.dv8tion.jda.api.interactions.commands.Command as JDACommand |
| 23 | +import kotlin.reflect.KFunction |
| 24 | + |
| 25 | +internal abstract class ApplicationCommandAutoBuilder<T : RootAnnotatedApplicationCommand>( |
| 26 | + applicationConfig: BApplicationConfig |
| 27 | +) : CommandAutoBuilder(), |
| 28 | + GlobalApplicationCommandProvider, |
| 29 | + GuildApplicationCommandProvider { |
| 30 | + |
| 31 | + private val logger = this.objectLogger() |
| 32 | + |
| 33 | + protected val forceGuildCommands: Boolean = applicationConfig.forceGuildCommands |
| 34 | + protected abstract val commandType: JDACommand.Type |
| 35 | + |
| 36 | + protected abstract val rootAnnotatedCommands: Collection<T> |
| 37 | + |
| 38 | + override fun declareGlobalApplicationCommands(manager: GlobalApplicationCommandManager) { |
| 39 | + // On global manager, do not register any command if forceGuildCommands is enabled, |
| 40 | + // as none of them would be global |
| 41 | + if (forceGuildCommands) |
| 42 | + return |
| 43 | + |
| 44 | + val skipLogger = SkipLogger(logger) |
| 45 | + rootAnnotatedCommands.forEachWithDelayedExceptions forEach@{ rootAnnotatedCommand -> |
| 46 | + val scope = rootAnnotatedCommand.scope |
| 47 | + if (scope != CommandScope.GLOBAL) return@forEach |
| 48 | + |
| 49 | + val testState = checkTestCommand(manager, rootAnnotatedCommand.func, scope, manager.context) |
| 50 | + if (testState != TestState.NO_ANNOTATION) |
| 51 | + throwInternal("Test commands on a global scope should have thrown in ${::checkTestCommand.shortSignatureNoSrc}") |
| 52 | + |
| 53 | + context(skipLogger) { declareTopLevel(manager, rootAnnotatedCommand) } |
| 54 | + } |
| 55 | + |
| 56 | + skipLogger.log(guild = null, commandType) |
| 57 | + } |
| 58 | + |
| 59 | + override fun declareGuildApplicationCommands(manager: GuildApplicationCommandManager) { |
| 60 | + val skipLogger = SkipLogger(logger) |
| 61 | + rootAnnotatedCommands.forEachWithDelayedExceptions forEach@{ rootAnnotatedCommand -> |
| 62 | + val scope = rootAnnotatedCommand.scope |
| 63 | + |
| 64 | + // If guild commands aren't forced, check the scope |
| 65 | + val canBeDeclared = forceGuildCommands || scope == CommandScope.GUILD |
| 66 | + if (!canBeDeclared) return@forEach |
| 67 | + |
| 68 | + val testState = checkTestCommand(manager, rootAnnotatedCommand.func, scope, manager.context) |
| 69 | + if (scope == CommandScope.GLOBAL && testState != TestState.NO_ANNOTATION) |
| 70 | + throwInternal("Test commands on a global scope should have thrown in ${::checkTestCommand.shortSignatureNoSrc}") |
| 71 | + |
| 72 | + if (testState == TestState.EXCLUDE) |
| 73 | + return@forEach skipLogger.skip(rootAnnotatedCommand.name, "Is a test command while this guild isn't a test guild") |
| 74 | + |
| 75 | + context(skipLogger) { declareTopLevel(manager, rootAnnotatedCommand) } |
| 76 | + } |
| 77 | + |
| 78 | + skipLogger.log(manager.guild, commandType) |
| 79 | + } |
| 80 | + |
| 81 | + private fun checkTestCommand(manager: AbstractApplicationCommandManager, func: KFunction<*>, scope: CommandScope, context: BContext): TestState { |
| 82 | + if (func.hasAnnotationRecursive<Test>()) { |
| 83 | + requireAt(scope == CommandScope.GUILD, func) { |
| 84 | + "Test commands must have their scope set to GUILD" |
| 85 | + } |
| 86 | + if (manager !is GuildApplicationCommandManager) throwInternal("GUILD scoped command was not registered with a guild command manager") |
| 87 | + |
| 88 | + //Returns whether the command can be registered |
| 89 | + return when (manager.guild.idLong) { |
| 90 | + in AnnotationUtils.getEffectiveTestGuildIds(context, func) -> TestState.INCLUDE |
| 91 | + else -> TestState.EXCLUDE |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return TestState.NO_ANNOTATION |
| 96 | + } |
| 97 | + |
| 98 | + context(logger: SkipLogger) |
| 99 | + protected abstract fun declareTopLevel(manager: AbstractApplicationCommandManager, rootCommand: T) |
| 100 | + |
| 101 | + context(logger: SkipLogger) |
| 102 | + internal fun checkDeclarationFilter( |
| 103 | + manager: AbstractApplicationCommandManager, |
| 104 | + metadata: ApplicationFunctionMetadata<*>, |
| 105 | + ): Boolean { |
| 106 | + val func = metadata.func |
| 107 | + val path = metadata.path |
| 108 | + val commandId = metadata.commandId |
| 109 | + |
| 110 | + func.findAllAnnotations<DeclarationFilter>().forEach { declarationFilter -> |
| 111 | + checkAt(manager is GuildApplicationCommandManager, func) { |
| 112 | + "${annotationRef<DeclarationFilter>()} can only be used on guild commands" |
| 113 | + } |
| 114 | + |
| 115 | + declarationFilter.filters.forEach { |
| 116 | + if (!serviceContainer.getService(it).filter(manager.guild, path, commandId)) { |
| 117 | + val commandIdStr = if (commandId != null) " (id ${commandId})" else "" |
| 118 | + logger.skip(path, "${it.simpleNestedName} rejected this command$commandIdStr") |
| 119 | + return false |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return true |
| 124 | + } |
| 125 | + |
| 126 | + protected fun ApplicationCommandBuilder<*>.fillApplicationCommandBuilder(func: KFunction<*>) { |
| 127 | + filters += AnnotationUtils.getFilters(context, func, ApplicationCommandFilter::class) |
| 128 | + |
| 129 | + if (func.hasAnnotationRecursive<NSFW>()) { |
| 130 | + throwArgument(func, "${annotationRef<NSFW>()} can only be used on text commands, use the #nsfw method on your annotation instead") |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private enum class TestState { |
| 135 | + INCLUDE, |
| 136 | + EXCLUDE, |
| 137 | + NO_ANNOTATION |
| 138 | + } |
| 139 | +} |
0 commit comments