Skip to content

Commit

Permalink
Check interaction contexts when retrieving guild-only options
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed May 18, 2024
1 parent 3438b8c commit cb09a2a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ interface ITopLevelApplicationCommandBuilder {
*/
var nsfw: Boolean
}

internal val ITopLevelApplicationCommandBuilder.isGuildOnly: Boolean
get() = contexts.singleOrNull() == InteractionContextType.GUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.freya022.botcommands.internal.commands.application

import io.github.freya022.botcommands.api.core.reflect.throwUser
import io.github.freya022.botcommands.api.commands.application.slash.builder.mixins.isGuildOnly
import io.github.freya022.botcommands.api.core.reflect.requireUser
import io.github.freya022.botcommands.api.core.utils.simpleNestedName
import io.github.freya022.botcommands.api.parameters.ResolverData
import io.github.freya022.botcommands.api.parameters.ResolverRequest
import net.dv8tion.jda.api.interactions.InteractionContextType
import kotlin.reflect.KClass

internal class ApplicationCommandResolverData(
Expand All @@ -12,9 +14,12 @@ internal class ApplicationCommandResolverData(

internal fun ResolverRequest.checkGuildOnly(returnType: KClass<*>) {
(resolverData as? ApplicationCommandResolverData)?.let { data ->
//TODO[User apps] throw if there is no guild scope at all, regardless of nullability
if (!data.commandInfo.builder.topLevelBuilder.scope.isGuildOnly && parameter.isRequired) {
parameter.throwUser("Cannot get a required ${returnType.simpleNestedName} in a global command")
parameter.requireUser(InteractionContextType.GUILD in data.commandInfo.builder.topLevelBuilder.contexts) {
"Commands that cannot run in guilds can't have a ${returnType.simpleNestedName} option"
}

parameter.requireUser(data.commandInfo.builder.topLevelBuilder.isGuildOnly || !parameter.isRequired) {
"Commands executable outside of guilds cannot have a required ${returnType.simpleNestedName} option"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.freya022.botcommands.internal.core.reflection

import io.github.freya022.botcommands.api.commands.application.builder.ApplicationCommandBuilder
import io.github.freya022.botcommands.api.commands.application.slash.builder.mixins.ITopLevelApplicationCommandBuilder
import io.github.freya022.botcommands.api.commands.application.slash.builder.mixins.isGuildOnly
import io.github.freya022.botcommands.api.commands.builder.ExecutableCommandBuilder
import io.github.freya022.botcommands.api.commands.builder.IBuilderFunctionHolder
import io.github.freya022.botcommands.api.commands.text.builder.TextCommandVariationBuilder
Expand All @@ -18,7 +18,6 @@ import io.github.freya022.botcommands.internal.utils.requireUser
import io.github.freya022.botcommands.internal.utils.shortSignature
import io.github.freya022.botcommands.internal.utils.throwUser
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent
import net.dv8tion.jda.api.interactions.InteractionContextType
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.jvmErasure
Expand Down Expand Up @@ -61,9 +60,6 @@ internal inline fun <reified GUILD_T : GenericCommandInteractionEvent> MemberPar
}
}

private val ITopLevelApplicationCommandBuilder.isGuildOnly: Boolean
get() = contexts.singleOrNull() == InteractionContextType.GUILD

internal inline fun <reified T : Any> ClassPathFunction.toMemberParamFunction() =
MemberParamFunction(function, instanceSupplier = { instance }, T::class)

Expand Down

0 comments on commit cb09a2a

Please sign in to comment.