Component Interaction Filter
Prevents component execution by returning an error object to the component executor, before which you must acknowledge the interaction.
Filters run when a component is about to be executed, i.e., after the constraints/rate limits... were checked.
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.
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
Note: For the example's sake, I will reply directly on each failed condition, however, I recommend having a separate function/class to handle rejections, as to not duplicate code on each rejection case.
@BService
class MyComponentFilter(
private val botOwners: BotOwners,
) : ComponentInteractionFilter {
override val global: Boolean get() = true
override suspend fun checkSuspend(event: GenericComponentInteractionCreateEvent, handlerName: String?): String? {
if (event.channel.idLong == 932902082724380744 && event.user !in botOwners) {
event.reply_("Only owners are allowed to use components in <#932902082724380744>", ephemeral = true).await()
return "Not an owner"
}
return null
}
}
@BService
public class MyComponentFilter implements ComponentInteractionFilter {
private final BotOwners botOwners;
public MyComponentFilter(BotOwners botOwners) {
this.botOwners = botOwners;
}
@Override
public boolean getGlobal() {
return true;
}
@Nullable
@Override
public String check(@NotNull GenericComponentInteractionCreateEvent event, @Nullable String handlerName) {
if (event.getChannel().getIdLong() == 932902082724380744L && !botOwners.isOwner(event.getUser())) {
event.reply("Only owners are allowed to use components in <#932902082724380744>").setEphemeral(true).queue();
return "Not an owner";
}
return null;
}
}
See also
@InterfacedService
Properties
Functions
Checks if this component can be used, returns null
if this filter passes, or a reason for the rejection, used for logging purposes.
Checks if this component can be used, returns null
if this filter passes, or a reason for the rejection, used for logging purposes.