ComponentInteractionFilter

@InterfacedService(acceptMultiple = true)
interface ComponentInteractionFilter<T : Any> : Filter(source)

Prevents component execution by returning an error object to the component executor.

Filters run when a component is about to be executed, i.e., after the constraints/rate limits... were checked.

When the final filter returns an error object of type T, it will then be passed to the ComponentInteractionRejectionHandler.

Combining filters

Filters can be combined with and/or (static methods for Java users).

Note: Filters that are not accessible via dependency injection cannot be used. For example, if you wish to combine filters using and/or, you should make a service factory that combines both filters and exposes the new one as a service:

@BConfiguration
class AdminAndInVoiceChannelFilterProvider {
@BService
fun adminAndInVoiceChannelFilter(
adminFilter: AdminFilter,
inVoiceChannelFilter: InVoiceChannelFilter
): ComponentInteractionFilter<String> = adminFilter and inVoiceChannelFilter
}

Requirements

Execution order

The execution order of global filters is determined by the priority of the service, while component-specific filters use the insertion order.

Example - Rejecting component interactions from non-owners

@BService
class MyComponentFilter(private val botOwners: BotOwners) : ComponentInteractionFilter<String> {
override suspend fun checkSuspend(event: GenericComponentInteractionCreateEvent, handlerName: String?): String? {
if (event.channel.idLong == 932902082724380744 && event.user !in botOwners) {
return "Only owners are allowed to use components in <#932902082724380744>"
}
return null
}
}

@BService
public class MyComponentFilter implements ComponentInteractionFilter<String> {
private final BotOwners botOwners;

public MyComponentFilter(BotOwners botOwners) {
this.botOwners = botOwners;
}

@Nullable
@Override
public String check(@NotNull GenericComponentInteractionCreateEvent event, @Nullable String handlerName) {
if (event.getChannel().getIdLong() == 932902082724380744L && !botOwners.isOwner(event.getUser())) {
return "Only owners are allowed to use components in <#932902082724380744>";
}
return null;
}
}

Parameters

T

Type of the error object handled by ComponentInteractionRejectionHandler

See also

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Description of the filter, used for logging purposes (like when a filter rejects a command).

Link copied to clipboard
open val global: Boolean

Whether this filter is global or command-specific.

Functions

Link copied to clipboard
Link copied to clipboard
open fun check(event: GenericComponentInteractionCreateEvent, handlerName: String?): T?

Returns null if this filter should allow the component to be used, or returns your own object if not.

Link copied to clipboard
open suspend fun checkSuspend(event: GenericComponentInteractionCreateEvent, handlerName: String?): T?

Returns null if this filter should allow the component to be used, or returns your own object if not.

Link copied to clipboard