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.
InteractionEventProcessor
interface#
The 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
:
Here is an example of InteractionEventProcessor
implementation:
filter(InteractionCreateEvent)
allows to decide whether to keep or to drop the givenInteractionCreateEvent
. 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 emptyMono
. The returned locale will be accessible via theInteractionContext
(which implementsbotrino.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.