Class Interactions
To begin creating a command you can pass a definition to the
onGuildCommand(ApplicationCommandRequest, Snowflake, Function) or
onGlobalCommand(ApplicationCommandRequest, Function) methods. The derived function is a handler that will
be run when an interaction for that application command is received. Commands are created or updated by using
createCommands(RestClient).
Once an RestInteraction is received, you need to submit a response to Discord within 3 seconds. You can do
that
by calling one of the acknowledge or reply methods under it, that will allow you to work with a
followup response handler. Currently this allows you to delete or modify the initial response, while also adding new
messages as followup.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn alias for a Reactor Netty server route. -
Method Summary
Modifier and TypeMethodDescriptionbuildReactorNettyHandler(RestClient restClient) Create a Reactor NettyHttpServerhandler to be applied to a single route using a method likeHttpServer.route(Consumer).buildReactorNettyHandler(RestClient restClient, InteractionValidator interactionValidator) Create a Reactor NettyHttpServerhandler to be applied to a single route using a method likeHttpServer.route(Consumer).static Interactionscreate()Create a new builder to work with Discord Interactions feature.createCommands(RestClient restClient) Send a request upon subscription to create or update all application commands definitions stored in this object.static InteractionHandlerSpecStart building a new interaction handling function, in order to combine guild and direct message handling.direct(Function<DirectInteraction, InteractionHandler> handlerFunction) Create an interaction handler that only accepts direct message interactions, giving you access to methods specific toDirectInteractioninstances.findHandler(discord4j.discordjson.json.InteractionData interactionData) Find the first handler that matches the givenInteractionData.guild(Function<GuildInteraction, InteractionHandler> handlerFunction) Create an interaction handler that only accepts guild interactions, giving you access to methods specific toGuildInteractioninstances.onCommand(Snowflake id, Function<RestInteraction, InteractionHandler> action) Add an application command handler that will match a command by the given Snowflakeid.onCommand(String name, Function<RestInteraction, InteractionHandler> action) Add an application command handler that will match a command by the givenname.onGlobalCommand(discord4j.discordjson.json.ApplicationCommandRequest createRequest, Function<RestInteraction, InteractionHandler> action) Use an application command definition to also add a handler associated with it.onGuildCommand(discord4j.discordjson.json.ApplicationCommandRequest createRequest, Snowflake guildId, Function<GuildInteraction, InteractionHandler> action) Use an application command definition to also add a handler associated with it.
-
Method Details
-
create
Create a new builder to work with Discord Interactions feature.- Returns:
- a new Interactions object
-
onCommand
Add an application command handler that will match a command by the given Snowflakeid.- Parameters:
id- a command id to matchaction- an interaction handler- Returns:
- this object
-
onCommand
Add an application command handler that will match a command by the givenname.- Parameters:
name- a command name to matchaction- an interaction handler- Returns:
- this object
-
onGuildCommand
public Interactions onGuildCommand(discord4j.discordjson.json.ApplicationCommandRequest createRequest, Snowflake guildId, Function<GuildInteraction, InteractionHandler> action) Use an application command definition to also add a handler associated with it. If you callcreateCommands(RestClient)this command will be associated with the givenguildId.- Parameters:
createRequest- a command definitionguildId- a guild ID to supply when creating a commandaction- an interaction handler- Returns:
- this object
-
onGlobalCommand
public Interactions onGlobalCommand(discord4j.discordjson.json.ApplicationCommandRequest createRequest, Function<RestInteraction, InteractionHandler> action) Use an application command definition to also add a handler associated with it. If you callcreateCommands(RestClient)this command will be created globally.Global commands can be invoked through a guild or through DMs. You can customize each source behavior using
createHandler()builder. Leaving a source without a handler will cause an "interaction failed" built-in message as user's reply.- Parameters:
createRequest- a command definitionaction- an interaction handler- Returns:
- this object
-
createCommands
Send a request upon subscription to create or update all application commands definitions stored in this object.- Parameters:
restClient- the web client used to interact with Discord API- Returns:
- a
Monowhere, upon successful completion, emits nothing, indicating the command have been created or updated. If an error is received, it is emitted through theMono.
-
findHandler
public ApplicationCommandDefinition findHandler(discord4j.discordjson.json.InteractionData interactionData) Find the first handler that matches the givenInteractionData.- Parameters:
interactionData- an object containing all information related to a single interaction- Returns:
- the detected handler, or if found nothing, a handler that does nothing.
-
buildReactorNettyHandler
Create a Reactor NettyHttpServerhandler to be applied to a single route using a method likeHttpServer.route(Consumer). This route will accept interactions from Discord when working in endpoint mode.Currently, no signature validation signature is implemented yet and external measures like a proxy that can validate the incoming requests must be used.
- Parameters:
restClient- the web client used to interact with Discord API- Returns:
- a Reactor Netty server route to handle endpoint-based interactions
-
buildReactorNettyHandler
public Interactions.ReactorNettyServerHandler buildReactorNettyHandler(RestClient restClient, InteractionValidator interactionValidator) Create a Reactor NettyHttpServerhandler to be applied to a single route using a method likeHttpServer.route(Consumer). This route will accept interactions from Discord when working in endpoint mode.Currently, no signature validation signature is implemented yet in D4J but you may provide your own implementation of
InteractionValidator- Parameters:
restClient- the web client used to interact with Discord APIinteractionValidator- the validator used to validate incoming requests- Returns:
- a Reactor Netty server route to handle endpoint-based interactions
-
guild
public static Function<RestInteraction,InteractionHandler> guild(Function<GuildInteraction, InteractionHandler> handlerFunction) Create an interaction handler that only accepts guild interactions, giving you access to methods specific toGuildInteractioninstances. To create an interaction handler capable of also handling direct messages, seecreateHandler().- Parameters:
handlerFunction- a mapper to derive anInteractionHandlerfrom aGuildInteraction- Returns:
- an interaction handling function, to be used in methods like
onGlobalCommand(ApplicationCommandRequest, Function)
-
direct
public static Function<RestInteraction,InteractionHandler> direct(Function<DirectInteraction, InteractionHandler> handlerFunction) Create an interaction handler that only accepts direct message interactions, giving you access to methods specific toDirectInteractioninstances. To create an interaction handler capable of also handling guild messages, seecreateHandler().- Parameters:
handlerFunction- a mapper to derive anInteractionHandlerfrom aDirectInteraction- Returns:
- an interaction handling function, to be used in methods like
onGlobalCommand(ApplicationCommandRequest, Function)
-
createHandler
Start building a new interaction handling function, in order to combine guild and direct message handling. Finish the mapper function by callingInteractionHandlerSpec.build(). By default this handler does nothing and methods likeInteractionHandlerSpec.guild(Function)orInteractionHandlerSpec.direct(Function)need to be called to add behavior.- Returns:
- a new interaction handler builder
-