Class DiscordOAuth2Client

    • Method Detail

      • createFromCode

        public static DiscordOAuth2Client createFromCode​(RestClient restClient,
                                                         discord4j.discordjson.json.AuthorizationCodeGrantRequest request)
        Create an OAuth2 client by completing an authorization code grant flow. This is useful if you have a custom HTTP server that receives the code parameter. Build a AuthorizationCodeGrantRequest making sure to include the code parameter and the access token with refresh capabilities will be stored once the first request is made using one of the API methods in this class or withAuthorizedClient(DiscordWebRequest).

        For an example server implementation, check DiscordOAuth2Server, which uses this method.

        Parameters:
        restClient - a Discord REST API client for performing requests
        request - an object with all parameters required to complete an authorization code grant request
        Returns:
        a client that can work with a valid user token to perform API requests
      • createFromCredentials

        public static DiscordOAuth2Client createFromCredentials​(RestClient restClient,
                                                                discord4j.discordjson.json.ClientCredentialsGrantRequest request)
        Create an OAuth2 client by performing a client credentials flow. This is a quick and easy way as a bot developer to get an access token for testing purposes. Build a ClientCredentialsGrantRequest using your application parameters and intended Scope values, separated by spaces.
        Parameters:
        restClient - a Discord REST API client for performing requests
        request - an object with all parameters required to complete a client credentials grant request
        Returns:
        a client that can work with a valid token for your user to perform API requests
      • createFromToken

        public static DiscordOAuth2Client createFromToken​(RestClient restClient,
                                                          long clientId,
                                                          String clientSecret,
                                                          discord4j.discordjson.json.AccessTokenData data)
        Create an OAuth2 client with the raw AccessTokenData returned from OAuth2Service.exchangeAuthorizationCode(AuthorizationCodeGrantRequest). Useful if a token is requested directly by your HTTP server.
        Parameters:
        restClient - a Discord REST API client for performing requests
        clientId - your application's client ID
        clientSecret - your application's client secret
        data - the access token object
        Returns:
        a client that can work with a valid token to perform API requests
      • getAccessToken

        public AccessToken getAccessToken()
        Return the current access token for this client. Depending on the OAuth2 scopes used, you can extract information from this object to perform additional tasks. For instance, if you used Scope.BOT, AccessToken.getGuild() will be available; if you used Scope.WEBHOOK_INCOMING, AccessToken.getWebhook() will be available.
        Returns:
        the current access token authorized for this client. This is an immutable instance, so you may need to call this again if the token is refreshed.
        Throws:
        IllegalStateException - if this access token was invalidated, indicating you might require reauthorization
      • getObjectMapper

        public ObjectMapper getObjectMapper()
        Return the Jackson ObjectMapper tied to this instance for JSON handling purposes.
        Returns:
        an object that can provide JSON processing
      • getAuthorizationInfo

        public Mono<discord4j.discordjson.json.AuthorizationInfoData> getAuthorizationInfo()
        Returns info about the current authorization. Uses withAuthorizedClient(DiscordWebRequest) to retrieve and use the Bearer token tied to this client.
        Returns:
        a Mono with authorization details given by this token in this client, or an error Mono in case any request fails
      • getCurrentUser

        public Mono<discord4j.discordjson.json.UserData> getCurrentUser()
        Returns the user object of the requesting account. For OAuth2, this requires the Scope.IDENTIFY scope, which will return the object without an email, and optionally the Scope.EMAIL scope, which returns the object with an email.
        Returns:
        a Mono with user details if successful, otherwise an error Mono
      • getCurrentUserGuilds

        public Flux<discord4j.discordjson.json.UserGuildData> getCurrentUserGuilds​(Map<String,​Object> queryParams)
        Returns a list of partial guild objects the requesting account is a member of. Requires the Scope.GUILDS scope.
        Parameters:
        queryParams - optional query parameters for this endpoint, allowing pagination using before, after and limit (defaults to 200)
        Returns:
        a Flux with partial guild information for the user, otherwise an error Flux
      • getCurrentUserGuildMember

        public Mono<discord4j.discordjson.json.MemberData> getCurrentUserGuildMember​(long guildId)
        Returns a member object from the current user in the given guild. Request the Scope.GUILDS_MEMBERS_READ scope.
        Parameters:
        guildId - the guild to query the current user member object
        Returns:
        a Mono with member information, otherwise an error Mono
      • getUserConnections

        public Flux<discord4j.discordjson.json.ConnectionData> getUserConnections()
        Return a list of ConnectionData objects. Requires this client was authorized to use the Scope.CONNECTIONS scope. Uses withAuthorizedClient(DiscordWebRequest) to retrieve and use the Bearer token tied to this client.
        Returns:
        a Mono with user connections, or an error Mono in case any request fails
      • getApplicationCommandPermissions

        public Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData> getApplicationCommandPermissions​(long applicationId,
                                                                                                                        long guildId,
                                                                                                                        long commandId)
        Fetches permissions for a specific command for your application in a guild. Returns a guild application command permissions object.
        Parameters:
        applicationId - your application ID
        guildId - the guild ID
        commandId - the command ID
        Returns:
        a Mono with command permissions object for the requested guild, or an error Mono in case a request fails
      • modifyApplicationCommandPermissions

        public Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData> modifyApplicationCommandPermissions​(long applicationId,
                                                                                                                           long guildId,
                                                                                                                           long commandId,
                                                                                                                           discord4j.discordjson.json.ApplicationCommandPermissionsRequest request)
        Edits command permissions for a specific command for your application in a guild and returns a guild application command permissions object. You can add up to 100 permission overwrites for a command.
        Parameters:
        applicationId - your application ID
        guildId - the guild ID
        commandId - the command ID
        request - a request body containing the permissions to be set
        Returns:
        a Mono with command permissions object for the requested guild, or an error Mono in case a request fails
      • exchange

        public <T> Mono<T> exchange​(DiscordWebRequest request,
                                    Class<T> responseType)
        Execute a given DiscordWebRequest on behalf of a user and mapping the response to the given type, using the credentials stored under this client. The token fetching, refreshing (if required) and API request are run once this Mono is subscribed. For more control on the authorized request and response see withAuthorizedClient(DiscordWebRequest).
        Type Parameters:
        T - the response type
        Parameters:
        request - the compiled Discord REST API request to be run on behalf of a user
        responseType - the expected response type from the API
        Returns:
        a Mono with the mapped response if successful, otherwise an error Mono
      • withAuthorizedClient

        public Mono<DiscordWebRequest> withAuthorizedClient​(DiscordWebRequest request)
        Prepare a given DiscordWebRequest on behalf of a user, using the credentials stored under this client. The token fetching, refreshing (if required) and API request are run once this Mono is subscribed.
        Parameters:
        request - the compiled Discord REST API request to be run on behalf of a user
        Returns:
        a Mono of a request including required steps to include proper authorization