Class DiscordApiBuilder

    • Constructor Detail

      • DiscordApiBuilder

        public DiscordApiBuilder()
    • Method Detail

      • login

        public java.util.concurrent.CompletableFuture<DiscordApi> login()
        Login to the account with the given token.
        Returns:
        A CompletableFuture which contains the DiscordApi.
      • loginAllShards

        public java.util.List<java.util.concurrent.CompletableFuture<DiscordApi>> loginAllShards()
        Login all shards to the account with the given token. It is invalid to call setCurrentShard(int) with anything but 0 before calling this method.
        Returns:
        A list of CompletableFutures which contain the DiscordApis for the shards.
      • loginShards

        public java.util.List<java.util.concurrent.CompletableFuture<DiscordApi>> loginShards​(java.util.function.IntPredicate shardsCondition)
        Login shards adhering to the given predicate to the account with the given token. It is invalid to call setCurrentShard(int) with anything but 0 before calling this method.
        Parameters:
        shardsCondition - The predicate for identifying shards to connect, starting with 0!
        Returns:
        A list of CompletableFutures which contain the DiscordApis for the shards.
      • loginShards

        public java.util.List<java.util.concurrent.CompletableFuture<DiscordApi>> loginShards​(int... shards)
        Login given shards to the account with the given token. It is invalid to call setCurrentShard(int) with anything but 0 before calling this method.
        Parameters:
        shards - The shards to connect, starting with 0!
        Returns:
        A list of CompletableFutures which contain the DiscordApis for the shards.
      • setGlobalRatelimiter

        public DiscordApiBuilder setGlobalRatelimiter​(Ratelimiter ratelimiter)
        Sets a ratelimiter that can be used to control global ratelimits.

        If you don't provide a ratelimiter by yourself, Javacord will use a default ratelimiter which is set to 5 requests per 112 ms, resulting in about 45 requests per second. The default ratelimiter will be shared by every bot with the same token in the same Java program.

        If you provide a custom ratelimiter, you have to make sure to use the exact same ratelimiter instance with all Javacord instances, or otherwise synchronize the global ratelimit across shards.

        An easy implementation is available with the LocalRatelimiter.

        Parameters:
        ratelimiter - The ratelimiter used to control global ratelimits.
        Returns:
        The current instance in order to chain call methods.
      • setGatewayIdentifyRatelimiter

        public DiscordApiBuilder setGatewayIdentifyRatelimiter​(Ratelimiter ratelimiter)
        Sets a ratelimiter that can be used to control the 5 seconds gateway identify ratelimit.

        By default, Javacord automatically provides a default LocalRatelimiter which is set to allow one gateway identify request per 5500ms and is shared with every bot with the same token in the same Java program.

        **DO NOT** set a custom gateway identify ratelimiter unless you have to synchronize the ratelimit across multiple Java programs (running on different JVMs, VMs, physical servers etc.) that run Javacord on the same bot token. The default ratelimiter will handle the ratelimit for you as long as your whole bot runs in the same Java program.

        Parameters:
        ratelimiter - The ratelimiter used to control the 5 seconds gateway identify ratelimit.
        Returns:
        The current instance in order to chain call methods.
      • setEventsDispatchable

        public DiscordApiBuilder setEventsDispatchable​(boolean dispatchEvents)
        Sets whether this API instance can dispatch events.
        Parameters:
        dispatchEvents - Whether events can be dispatched.
        Returns:
        The current instance in order to chain call methods.
      • setProxySelector

        public DiscordApiBuilder setProxySelector​(java.net.ProxySelector proxySelector)
        Sets the proxy selector which should be used to determine the proxies that should be used to connect to the Discord REST API and web socket. If no explicit proxy is configured using setProxy(Proxy) and no proxy selector is configured using this method, ProxySelector.getDefault() is used to select appropriate proxies. If ProxySelector.setDefault(ProxySelector) was not used to set a custom proxy selector, the default one evaluates the system properties https.proxyHost, https.proxyPort, http.nonProxyHosts, socksProxyHost, socksProxyPort and socksProxyVersion as documented on the Networking Properties page.

        Note: It is an error to configure both an explicit proxy via setProxy(Proxy) and a proxy selector using this method.

        Parameters:
        proxySelector - The proxy selector to set.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        setProxy(Proxy), setProxyAuthenticator(Authenticator), ProxySelector.getDefault(), ProxySelector.setDefault(ProxySelector), Networking Properties
      • setProxy

        public DiscordApiBuilder setProxy​(java.net.Proxy proxy)
        Sets the proxy which should be used to connect to the Discord REST API and web socket. If this is not set explicitly, the proxy selector configured with setProxySelector(ProxySelector) is used to select appropriate proxies. If both are not set, ProxySelector.getDefault() is used to select appropriate proxies. If ProxySelector.setDefault(ProxySelector) was not used to set a custom proxy selector, the default one evaluates the system properties https.proxyHost, https.proxyPort, http.nonProxyHosts, socksProxyHost, socksProxyPort and socksProxyVersion as documented on the Networking Properties page.

        Note: It is an error to configure both an explicit proxy using this method and a proxy selector using setProxySelector(ProxySelector).

        Parameters:
        proxy - The proxy to set.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        setProxyAuthenticator(Authenticator), setProxySelector(ProxySelector), ProxySelector.getDefault(), ProxySelector.setDefault(ProxySelector), Networking Properties
      • setProxyAuthenticator

        public DiscordApiBuilder setProxyAuthenticator​(Authenticator authenticator)
        Sets the authenticator that should be used to authenticate against proxies that require it. If this is not set explicitly, the authenticator configured with Authenticator.setDefault(java.net.Authenticator), if any, is used to get credentials for Basic auth if the proxy supports it. If you need to support a more sophisticated authentication algorithm or scheme, use this method to set an own authenticator.
        Parameters:
        authenticator - The proxy authenticator to set.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        setProxy(Proxy), setProxySelector(ProxySelector), Authenticator.setDefault(java.net.Authenticator)
      • setTrustAllCertificates

        public DiscordApiBuilder setTrustAllCertificates​(boolean trustAllCertificates)
        Sets whether all SSL certificates should be trusted when connecting to the Discord API and web socket. This might for example be necessary when connecting through a decrypting proxy. Be aware that this also increases the risk of man-in-the-middle attacks, which basically is, what a decrypting proxy does. Due to this risk, a warning is logged when connecting with this property set to true. If you do not care about this risk, you can suppress this warning using your logging configuration.
        Parameters:
        trustAllCertificates - Whether to trust all SSL certificates.
        Returns:
        The current instance in order to chain call methods.
      • setToken

        public DiscordApiBuilder setToken​(java.lang.String token)
        Sets the token which is required for the login process. A tutorial on how to get the token can be found in the Javacord wiki.
        Parameters:
        token - The token to set.
        Returns:
        The current instance in order to chain call methods.
      • getToken

        public java.util.Optional<java.lang.String> getToken()
        Gets the token that will be used to login.
        Returns:
        The token.
        See Also:
        setToken(String)
      • setTotalShards

        public DiscordApiBuilder setTotalShards​(int totalShards)
        Sets total shards for server sharding. Sharding allows you to split your bot into several independent instances. A shard only handles a subset of a bot's servers.
        Parameters:
        totalShards - The total amount of shards. Sharding will be disabled if set to 1.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        API docs
      • getTotalShards

        public int getTotalShards()
        Gets the total amount of shards. Sharding is disabled if set to 1.
        Returns:
        The total amount of shards.
        See Also:
        setTotalShards(int)
      • setCurrentShard

        public DiscordApiBuilder setCurrentShard​(int currentShard)
        Sets shard for server sharding. Sharding allows you to split your bot into several independent instances. A shard only handles a subset of a bot's servers.
        Parameters:
        currentShard - The shard of this connection starting with 0!
        Returns:
        The current instance in order to chain call methods.
        See Also:
        API docs
      • getCurrentShard

        public int getCurrentShard()
        Gets the current shard.
        Returns:
        The current shard.
        See Also:
        setCurrentShard(int)
      • setWaitForServersOnStartup

        public DiscordApiBuilder setWaitForServersOnStartup​(boolean waitForServersOnStartup)
        Sets if Javacord should wait for all servers to become available on startup. If this is disabled the DiscordApi.getServers() method will return an empty collection directly after logging in and fire ServerBecomesAvailableEvent events once they become available. You can check the ids of unavailable servers using the DiscordApi.getUnavailableServers() method.
        Parameters:
        waitForServersOnStartup - Whether Javacord should wait for all servers to become available on startup or not.
        Returns:
        The current instance in order to chain call methods.
      • isWaitingForServersOnStartup

        public boolean isWaitingForServersOnStartup()
        Checks if Javacord should wait for all servers to become available on startup.
        Returns:
        If Javacord should wait.
        See Also:
        setWaitForServersOnStartup(boolean)
      • setWaitForUsersOnStartup

        public DiscordApiBuilder setWaitForUsersOnStartup​(boolean waitForUsersOnStartup)
        Sets if Javacord should wait for all users to be cached. If this is set to true, Javacord will consider Servers with uncached users as unavailable.

        Requires the Intent.GUILD_MEMBERS intent to be set.

        Parameters:
        waitForUsersOnStartup - Whether Javacord should wait for all users to be cached or not.
        Returns:
        The current instance in order to chain call methods.
      • isWaitingForUsersOnStartup

        public boolean isWaitingForUsersOnStartup()
        Checks if Javacord should wait for all users to be cached.
        Returns:
        If Javacord should wait.
        See Also:
        setWaitForUsersOnStartup(boolean)
      • setShutdownHookRegistrationEnabled

        public DiscordApiBuilder setShutdownHookRegistrationEnabled​(boolean registerShutdownHook)
        Sets if Javacord should register a shutdown hook that disconnects the DiscordApi instance.

        By default, Javacord registers a shutdown hook using Runtime.addShutdownHook(Thread) that calls the DiscordApi.disconnect() method. Setting this flag to false will disable this behavior.

        Parameters:
        registerShutdownHook - Whether the shutdown hook should be registered or not.
        Returns:
        The current instance in order to chain call methods.
      • isShutdownHookRegistrationEnabled

        public boolean isShutdownHookRegistrationEnabled()
        Checks if newly created DiscordApi instances should register a shutdown hook to disconnect the instance.
        Returns:
        Whether the shutdown hook will be registered or not.
        See Also:
        setShutdownHookRegistrationEnabled(boolean)
      • setIntents

        public DiscordApiBuilder setIntents​(Intent... intents)
        Sets intent for the events which should be received.
        Parameters:
        intents - One or more intents from Intent.
        Returns:
        The current instance in order to chain call methods.
      • setAllIntents

        public DiscordApiBuilder setAllIntents()
        Sets all intents.
        Returns:
        The current instance in order to chain call methods.
      • setAllNonPrivilegedIntents

        public DiscordApiBuilder setAllNonPrivilegedIntents()
        Sets all non-privileged intents.

        This is the default behavior if no intents are set in the builder.

        Returns:
        The current instance in order to chain call methods.
      • setAllIntentsExcept

        public DiscordApiBuilder setAllIntentsExcept​(Intent... intentsToOmit)
        Sets all intents except the given intents.
        Parameters:
        intentsToOmit - One or more Intents which should be omitted.
        Returns:
        The current instance in order to chain call methods.
      • setAllNonPrivilegedIntentsExcept

        public DiscordApiBuilder setAllNonPrivilegedIntentsExcept​(Intent... intentsToOmit)
        Sets all non privileged intents except the given intents.
        Parameters:
        intentsToOmit - One or more Intents which should be omitted.
        Returns:
        The current instance in order to chain call methods.
      • setAllNonPrivilegedIntentsAnd

        public DiscordApiBuilder setAllNonPrivilegedIntentsAnd​(Intent... intentsToInclude)
        Sets all non-privileged intents and the given intents.
        Parameters:
        intentsToInclude - One or more Intents which should be included.
        Returns:
        The current instance in order to chain call methods.
      • addIntents

        public DiscordApiBuilder addIntents​(Intent... intents)
        Adds the given intents to the already set.
        Parameters:
        intents - The intents to add.
        Returns:
        The current instance in order to chain call methods.
      • setAllIntentsWhere

        public DiscordApiBuilder setAllIntentsWhere​(java.util.function.Predicate<Intent> condition)
        Sets the intents where the given predicate matches.
        Parameters:
        condition - Whether the intent should be added or not.
        Returns:
        The current instance in order to chain call methods.
      • setUserCacheEnabled

        public DiscordApiBuilder setUserCacheEnabled​(boolean enabled)
        Sets whether the user cache should be enabled.

        By default, the user cache is disabled.

        Parameters:
        enabled - Whether the user cache should be enabled.
        Returns:
        The current instance in order to chain call methods.
      • isUserCachedEnabled

        public boolean isUserCachedEnabled()
        Gets whether the user cache is enabled.
        Returns:
        Whether the user cache is enabled.
      • setRecommendedTotalShards

        public java.util.concurrent.CompletableFuture<DiscordApiBuilder> setRecommendedTotalShards()
        Retrieves the recommended shards count from the Discord API and sets it in this builder. Sharding allows you to split your bot into several independent instances. A shard only handles a subset of a bot's servers.
        Returns:
        A future with the current api builder.
        See Also:
        API docs
      • addListener

        public <T extends GloballyAttachableListenerDiscordApiBuilder addListener​(java.lang.Class<T> listenerClass,
                                                                                    java.util.function.Supplier<T> listenerSupplier)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Adds a GloballyAttachableListener to all created DiscordApi instances. The supplier is called for every created DiscordApi instance, so either the same or different instances can be registered. One example would be a method reference to a default constructor like MyListener::new. Adding a listener multiple times will only add it once. The order of invocation is according to first addition.
        Specified by:
        addListener in interface ChainableGloballyAttachableListenerManager
        Type Parameters:
        T - The type of the listener.
        Parameters:
        listenerClass - The listener class.
        listenerSupplier - The supplier of listeners to add.
        Returns:
        The current instance in order to chain call methods.
      • addListener

        public DiscordApiBuilder addListener​(java.util.function.Supplier<GloballyAttachableListener> listenerSupplier)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Adds a listener that implements one or more GloballyAttachableListeners to all created DiscordApi instances. The supplier is called for every created DiscordApi instance, so either the same or different instances can be registered. One example would be a method reference to a default constructor like MyListener::new. Adding a listener multiple times will only add it once. The order of invocation is according to first addition.
        Specified by:
        addListener in interface ChainableGloballyAttachableListenerManager
        Parameters:
        listenerSupplier - The supplier of listeners to add.
        Returns:
        The current instance in order to chain call methods.
      • addListener

        public <T extends GloballyAttachableListenerDiscordApiBuilder addListener​(java.lang.Class<T> listenerClass,
                                                                                    java.util.function.Function<DiscordApi,​T> listenerFunction)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Adds a GloballyAttachableListener to all created DiscordApi instances. The function is called for every created DiscordApi instance, so either the same or different instances can be registered. One example would be a method reference to a constructor with a DiscordApi parameter like MyListener::new. Adding a listener multiple times will only add it once. The order of invocation is according to first addition.
        Specified by:
        addListener in interface ChainableGloballyAttachableListenerManager
        Type Parameters:
        T - The type of the listener.
        Parameters:
        listenerClass - The listener class.
        listenerFunction - The function to provide listeners to add.
        Returns:
        The current instance in order to chain call methods.
      • addListener

        public DiscordApiBuilder addListener​(java.util.function.Function<DiscordApi,​GloballyAttachableListener> listenerFunction)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Adds a listener that implements one or more GloballyAttachableListeners to all created DiscordApi instances. The function is called for every created DiscordApi instance, so either the same or different instances can be registered. One example would be a method reference to a constructor with a DiscordApi parameter like MyListener::new. Adding a listener multiple times will only add it once. The order of invocation is according to first addition.
        Specified by:
        addListener in interface ChainableGloballyAttachableListenerManager
        Parameters:
        listenerFunction - The function to provide listeners to add.
        Returns:
        The current instance in order to chain call methods.
      • removeListener

        public <T extends GloballyAttachableListenerDiscordApiBuilder removeListener​(java.lang.Class<T> listenerClass,
                                                                                       T listener)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Removes a GloballyAttachableListener from the list of listeners to be added to DiscordApi instances. If the listener was already added to a DiscordApi instance, it will not get removed by calling this method. This method should normally only be used before calling one of the login methods.
        Specified by:
        removeListener in interface ChainableGloballyAttachableListenerManager
        Type Parameters:
        T - The type of the listener.
        Parameters:
        listenerClass - The listener class.
        listener - The listener to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeListenerSupplier

        public DiscordApiBuilder removeListenerSupplier​(java.util.function.Supplier<GloballyAttachableListener> listenerSupplier)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Removes a supplier of listeners that implements one or more GloballyAttachableListeners from the list of listeners to be added to DiscordApi instances. If the listener was already added to a DiscordApi instance, it will not get removed by calling this method. This method should normally only be used before calling one of the login methods.
        Specified by:
        removeListenerSupplier in interface ChainableGloballyAttachableListenerManager
        Parameters:
        listenerSupplier - The supplier of listeners to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeListenerSupplier

        public <T extends GloballyAttachableListenerDiscordApiBuilder removeListenerSupplier​(java.lang.Class<T> listenerClass,
                                                                                               java.util.function.Supplier<T> listenerSupplier)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Removes a supplier of GloballyAttachableListeners from the list of listeners to be added to DiscordApi instances. If the listener was already added to a DiscordApi instance, it will not get removed by calling this method. This method should normally only be used before calling one of the login methods.
        Specified by:
        removeListenerSupplier in interface ChainableGloballyAttachableListenerManager
        Type Parameters:
        T - The type of the listener.
        Parameters:
        listenerClass - The listener class.
        listenerSupplier - The supplier of listeners to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeListenerFunction

        public DiscordApiBuilder removeListenerFunction​(java.util.function.Function<DiscordApi,​GloballyAttachableListener> listenerFunction)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Removes a function that provides listeners that implements one or more GloballyAttachableListeners from the list of listeners to be added to DiscordApi instances. If the listener was already added to a DiscordApi instance, it will not get removed by calling this method. This method should normally only be used before calling one of the login methods.
        Specified by:
        removeListenerFunction in interface ChainableGloballyAttachableListenerManager
        Parameters:
        listenerFunction - The function to provide listeners to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeListenerFunction

        public <T extends GloballyAttachableListenerDiscordApiBuilder removeListenerFunction​(java.lang.Class<T> listenerClass,
                                                                                               java.util.function.Function<DiscordApi,​T> listenerFunction)
        Description copied from interface: ChainableGloballyAttachableListenerManager
        Removes a function that provides GloballyAttachableListeners from the list of listeners to be added to DiscordApi instances. If the listener was already added to a DiscordApi instance, it will not get removed by calling this method. This method should normally only be used before calling one of the login methods.
        Specified by:
        removeListenerFunction in interface ChainableGloballyAttachableListenerManager
        Type Parameters:
        T - The type of the listener.
        Parameters:
        listenerClass - The listener class.
        listenerFunction - The function to provide listeners to remove.
        Returns:
        The current instance in order to chain call methods.