Component Interaction Filter
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
Register your instance as a service with @BService.
Have exactly one instance of ComponentInteractionRejectionHandler.
Implement either check (Java) or checkSuspend (Kotlin).
(Optional) Set your filter as a component-specific filter by disabling global.
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
Type of the error object handled by ComponentInteractionRejectionHandler
See also
@InterfacedService
Properties
Functions
Returns null
if this filter should allow the component to be used, or returns your own object if not.
Returns null
if this filter should allow the component to be used, or returns your own object if not.