Package org.javacord.api
Class DiscordApiBuilder
- java.lang.Object
-
- org.javacord.api.DiscordApiBuilder
-
public class DiscordApiBuilder extends Object
This class is used to login to a Discord account.
-
-
Constructor Summary
Constructors Constructor Description DiscordApiBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AccountTypegetAccountType()Gets the account type.intgetCurrentShard()Gets the current shard.Optional<String>getToken()Gets the token that will be used to login.intgetTotalShards()Gets the total amount of shards.booleanisWaitingForServersOnStartup()Checks if Javacord should wait for all servers to become available on startup.CompletableFuture<DiscordApi>login()Login to the account with the given token.Collection<CompletableFuture<DiscordApi>>loginAllShards()Login all shards to the account with the given token.Collection<CompletableFuture<DiscordApi>>loginShards(int... shards)Login given shards to the account with the given token.Collection<CompletableFuture<DiscordApi>>loginShards(IntPredicate shardsCondition)Login shards adhering to the given predicate to the account with the given token.DiscordApiBuildersetAccountType(AccountType type)Sets the account type.DiscordApiBuildersetCurrentShard(int currentShard)Sets shard for server sharding.DiscordApiBuildersetProxy(Proxy proxy)Sets the proxy which should be used to connect to the Discord REST API and web socket.DiscordApiBuildersetProxyAuthenticator(Authenticator authenticator)Sets the authenticator that should be used to authenticate against proxies that require it.DiscordApiBuildersetProxySelector(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.CompletableFuture<DiscordApiBuilder>setRecommendedTotalShards()Retrieves the recommended shards count from the Discord API and sets it in this builder.DiscordApiBuildersetToken(String token)Sets the token which is required for the login process.DiscordApiBuildersetTotalShards(int totalShards)Sets total shards for server sharding.DiscordApiBuildersetTrustAllCertificates(boolean trustAllCertificates)Sets whether all SSL certificates should be trusted when connecting to the Discord API and web socket.DiscordApiBuildersetWaitForServersOnStartup(boolean waitForServersOnStartup)Sets if Javacord should wait for all servers to become available on startup.
-
-
-
Method Detail
-
login
public CompletableFuture<DiscordApi> login()
Login to the account with the given token.- Returns:
- A
CompletableFuturewhich contains the DiscordApi.
-
loginAllShards
public Collection<CompletableFuture<DiscordApi>> loginAllShards()
Login all shards to the account with the given token. It is invalid to callsetCurrentShard(int)with anything but0before calling this method.- Returns:
- A collection of
CompletableFutures which contain theDiscordApis for the shards.
-
loginShards
public Collection<CompletableFuture<DiscordApi>> loginShards(IntPredicate shardsCondition)
Login shards adhering to the given predicate to the account with the given token. It is invalid to callsetCurrentShard(int)with anything but0before calling this method.- Parameters:
shardsCondition- The predicate for identifying shards to connect, starting with0!- Returns:
- A collection of
CompletableFutures which contain theDiscordApis for the shards.
-
loginShards
public Collection<CompletableFuture<DiscordApi>> loginShards(int... shards)
Login given shards to the account with the given token. It is invalid to callsetCurrentShard(int)with anything but0before calling this method.- Parameters:
shards- The shards to connect, starting with0!- Returns:
- A collection of
CompletableFutures which contain theDiscordApis for the shards.
-
setProxySelector
public DiscordApiBuilder setProxySelector(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 usingsetProxy(Proxy)and no proxy selector is configured using this method,ProxySelector.getDefault()is used to select appropriate proxies. IfProxySelector.setDefault(ProxySelector)was not used to set a custom proxy selector, the default one evaluates the system propertieshttps.proxyHost,https.proxyPort,http.nonProxyHosts,socksProxyHost,socksProxyPortandsocksProxyVersionas documented on the Networking Properties page.Note: It is an error to configure 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(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 withsetProxySelector(ProxySelector)is used to select appropriate proxies. If both are not set,ProxySelector.getDefault()is used to select appropriate proxies. IfProxySelector.setDefault(ProxySelector)was not used to set a custom proxy selector, the default one evaluates the system propertieshttps.proxyHost,https.proxyPort,http.nonProxyHosts,socksProxyHost,socksProxyPortandsocksProxyVersionas documented on the Networking Properties page.Note: It is an error to configure 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 withAuthenticator.setDefault(java.net.Authenticator), if any, is used to get credentials forBasicauth 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 totrue. 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(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 Optional<String> getToken()
Gets the token that will be used to login.- Returns:
- The token.
- See Also:
setToken(String)
-
setAccountType
public DiscordApiBuilder setAccountType(AccountType type)
Sets the account type. By default the builder assumes that you want to login to a bot account. Please notice, that public client bots are not allowed by Discord!- Parameters:
type- The account type.- Returns:
- The current instance in order to chain call methods.
-
getAccountType
public AccountType getAccountType()
Gets the account type.- Returns:
- The account type.
- See Also:
setAccountType(AccountType)
-
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 to1.- 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 to1.- 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 with0!- 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 theDiscordApi.getServers()method will return an empty collection directly after logging in and fireServerBecomesAvailableEventevents once they become available. You can check the ids of unavailable servers using theDiscordApi.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)
-
setRecommendedTotalShards
public 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
-
-