Class Interactions
- java.lang.Object
-
- discord4j.rest.interaction.Interactions
-
@Experimental public class Interactions extends Object
An entry point to build and process Discord interactions. Provides methods to setup an application command handler that can optionally be used to create and update global or guild commands.To begin creating a command you can pass a definition to the
onGuildCommand(ApplicationCommandRequest, Snowflake, Function)
oronGlobalCommand(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 usingcreateCommands(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 theacknowledge
orreply
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:
- Slash commands
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Interactions.ReactorNettyServerHandler
An alias for a Reactor Netty server route.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Interactions.ReactorNettyServerHandler
buildReactorNettyHandler(RestClient restClient)
Create a Reactor NettyHttpServer
handler to be applied to a single route using a method likeHttpServer.route(Consumer)
.Interactions.ReactorNettyServerHandler
buildReactorNettyHandler(RestClient restClient, InteractionValidator interactionValidator)
Create a Reactor NettyHttpServer
handler to be applied to a single route using a method likeHttpServer.route(Consumer)
.static Interactions
create()
Create a new builder to work with Discord Interactions feature.Mono<Void>
createCommands(RestClient restClient)
Send a request upon subscription to create or update all application commands definitions stored in this object.static InteractionHandlerSpec
createHandler()
Start building a new interaction handling function, in order to combine guild and direct message handling.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 toDirectInteraction
instances.ApplicationCommandDefinition
findHandler(discord4j.discordjson.json.InteractionData interactionData)
Find the first handler that matches the givenInteractionData
.static Function<RestInteraction,InteractionHandler>
guild(Function<GuildInteraction,InteractionHandler> handlerFunction)
Create an interaction handler that only accepts guild interactions, giving you access to methods specific toGuildInteraction
instances.Interactions
onCommand(Snowflake id, Function<RestInteraction,InteractionHandler> action)
Add an application command handler that will match a command by the given Snowflakeid
.Interactions
onCommand(String name, Function<RestInteraction,InteractionHandler> action)
Add an application command handler that will match a command by the givenname
.Interactions
onGlobalCommand(discord4j.discordjson.json.ApplicationCommandRequest createRequest, Function<RestInteraction,InteractionHandler> action)
Use an application command definition to also add a handler associated with it.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.
-
-
-
Method Detail
-
create
public static Interactions create()
Create a new builder to work with Discord Interactions feature.- Returns:
- a new Interactions object
-
onCommand
public Interactions onCommand(Snowflake id, Function<RestInteraction,InteractionHandler> action)
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
public Interactions onCommand(String name, Function<RestInteraction,InteractionHandler> action)
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
public Mono<Void> createCommands(RestClient restClient)
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
Mono
where, 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
public Interactions.ReactorNettyServerHandler buildReactorNettyHandler(RestClient restClient)
Create a Reactor NettyHttpServer
handler 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 NettyHttpServer
handler 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 toGuildInteraction
instances. To create an interaction handler capable of also handling direct messages, seecreateHandler()
.- Parameters:
handlerFunction
- a mapper to derive anInteractionHandler
from 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 toDirectInteraction
instances. To create an interaction handler capable of also handling guild messages, seecreateHandler()
.- Parameters:
handlerFunction
- a mapper to derive anInteractionHandler
from aDirectInteraction
- Returns:
- an interaction handling function, to be used in methods like
onGlobalCommand(ApplicationCommandRequest, Function)
-
createHandler
public static InteractionHandlerSpec 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
-
-