Skip to main content

Filtering and adapting events

Listeners are triggered when an interaction event is received via the Discord gateway. It is possible for you to intervene between the moment the event is received and the moment the listener is triggered. This allows you to drop some events to prevent any command from being executed in a certain context, or to determine which locale to apply according to the event received.

The InteractionEventProcessor interface

Create a class that implements InteractionEventProcessor. If you are using the Botrino framework, it will automatically be set into the interaction service. Otherwise, you need to set it via the builder when constructing your InteractionService:

final var interactionService = InteractionService.builder(config, gateway)
.setEventProcessor(new MyEventProcessor())
.build();

Here is an example of InteractionEventProcessor implementation:

package testbot1;

import botrino.interaction.InteractionEventProcessor;
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
import discord4j.core.object.entity.channel.GuildMessageChannel;
import reactor.core.publisher.Mono;

import java.util.Locale;

public final class MyEventProcessor implements InteractionEventProcessor {

@Override
public Mono<Boolean> filter(InteractionCreateEvent event) {
return Mono.just(!event.getInteraction().getMember()
.map(member -> member.getDisplayName().toLowerCase().contains("ignore me"))
.orElse(true));
}

@Override
public Mono<Locale> computeLocale(InteractionCreateEvent event) {
return event.getInteraction().getChannel()
.ofType(GuildMessageChannel.class)
.flatMap(guildChannel -> {
if (guildChannel.getName().endsWith("fr")) {
return Mono.just(Locale.FRENCH);
} else if (guildChannel.getName().endsWith("de")) {
return Mono.just(Locale.GERMAN);
} else {
return Mono.empty();
}
});
}
}
  • filter(InteractionCreateEvent) allows to decide whether to keep or to drop the given InteractionCreateEvent. The default implementation will allow all events. This method can be useful if you want to implement a blacklist system to ban some users from using your bot. In this example, we are filtering out users which username contains the string " ignore me".
  • computeLocale(InteractionCreateEvent) allows to adapt the locale according to the event received. In order to keep the default locale, you are expected to return an empty Mono. The returned locale will be accessible via the InteractionContext (which implements botrino.api.Translator, see Internationalization). You will typically store the locale in a database (either per guild or per user) and retrieve it here using the data given by the interaction event. In this example, we are interpreting the suffix of the channel name as the locale, with only French and German being supported. It will use the default locale defined in the configuration in all other cases.
Considerations when using the Botrino framework
  • The implementation class must have a no-arg constructor.
  • If more than one implementation of InteractionEventProcessor are found, it will result in an error as it is impossible to determine which one to use. If you don't want to remove the extra implementation(s), you can mark one of them with the @Primary annotation to lift the ambiguity. You may alternatively use the @Exclude annotation if you don't want one implementation to be picked up by Botrino.