Class 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:
    Slash commands
    • Method Detail

      • create

        public static Interactions create()
        Create a new builder to work with Discord Interactions feature.
        Returns:
        a new Interactions 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