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) 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:
  • Method Details

    • create

      public static Interactions 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 Snowflake id.
      Parameters:
      id - a command id to match
      action - 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 given name.
      Parameters:
      name - a command name to match
      action - 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 call createCommands(RestClient) this command will be associated with the given guildId.
      Parameters:
      createRequest - a command definition
      guildId - a guild ID to supply when creating a command
      action - 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 call createCommands(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 definition
      action - 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 the Mono.
    • findHandler

      public ApplicationCommandDefinition findHandler(discord4j.discordjson.json.InteractionData interactionData)
      Find the first handler that matches the given InteractionData.
      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 Netty HttpServer handler to be applied to a single route using a method like HttpServer.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 Netty HttpServer handler to be applied to a single route using a method like HttpServer.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 API
      interactionValidator - the validator used to validate incoming requests
      Returns:
      a Reactor Netty server route to handle endpoint-based interactions
    • guild

      Create an interaction handler that only accepts guild interactions, giving you access to methods specific to GuildInteraction instances. To create an interaction handler capable of also handling direct messages, see createHandler().
      Parameters:
      handlerFunction - a mapper to derive an InteractionHandler from a GuildInteraction
      Returns:
      an interaction handling function, to be used in methods like onGlobalCommand(ApplicationCommandRequest, Function)
    • direct

      Create an interaction handler that only accepts direct message interactions, giving you access to methods specific to DirectInteraction instances. To create an interaction handler capable of also handling guild messages, see createHandler().
      Parameters:
      handlerFunction - a mapper to derive an InteractionHandler from a DirectInteraction
      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 calling InteractionHandlerSpec.build(). By default this handler does nothing and methods like InteractionHandlerSpec.guild(Function) or InteractionHandlerSpec.direct(Function) need to be called to add behavior.
      Returns:
      a new interaction handler builder