Class DiscordApiImpl

    • Constructor Detail

      • DiscordApiImpl

        public DiscordApiImpl​(String token,
                              ProxySelector proxySelector,
                              Proxy proxy,
                              Authenticator proxyAuthenticator,
                              boolean trustAllCertificates)
        Creates a new discord api instance that can be used for auto-ratelimited REST calls, but does not connect to the Discord WebSocket.
        Parameters:
        token - The token used to connect without any account type specific prefix.
        proxySelector - The proxy selector which should be used to determine the proxies that should be used to connect to the Discord REST API and websocket.
        proxy - The proxy which should be used to connect to the Discord REST API and websocket.
        proxyAuthenticator - The authenticator that should be used to authenticate against proxies that require it.
        trustAllCertificates - Whether to trust all SSL certificates.
      • DiscordApiImpl

        public DiscordApiImpl​(AccountType accountType,
                              String token,
                              int currentShard,
                              int totalShards,
                              boolean waitForServersOnStartup,
                              ProxySelector proxySelector,
                              Proxy proxy,
                              Authenticator proxyAuthenticator,
                              boolean trustAllCertificates,
                              CompletableFuture<DiscordApi> ready)
        Creates a new discord api instance.
        Parameters:
        accountType - The account type of the instance.
        token - The token used to connect without any account type specific prefix.
        currentShard - The current shard the bot should connect to.
        totalShards - The total amount of shards.
        waitForServersOnStartup - Whether Javacord should wait for all servers to become available on startup or not.
        proxySelector - The proxy selector which should be used to determine the proxies that should be used to connect to the Discord REST API and websocket.
        proxy - The proxy which should be used to connect to the Discord REST API and websocket.
        proxyAuthenticator - The authenticator that should be used to authenticate against proxies that require it.
        trustAllCertificates - Whether to trust all SSL certificates.
        ready - The future which will be completed when the connection to Discord was successful.
    • Method Detail

      • getHttpClient

        public okhttp3.OkHttpClient getHttpClient​()
        Gets the used http client for this api instance.
        Returns:
        The used http client.
      • getEventDispatcher

        public EventDispatcher getEventDispatcher​()
        Gets the event dispatcher which is used to dispatch events.
        Returns:
        The used event dispatcher.
      • getRatelimitManager

        public RatelimitManager getRatelimitManager​()
        Gets the ratelimit manager for this bot.
        Returns:
        The ratelimit manager for this bot.
      • getObjectMapper

        public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper​()
        Gets the object mapper used by this api instance.
        Returns:
        The object mapper used by this api instance.
      • purgeCache

        public void purgeCache​()
        Purges all cached entities. This method is only meant to be called after receiving a READY packet.
      • getAllServers

        public Collection<Server> getAllServers​()
        Gets a collection with all servers, including ready and not ready ones.
        Returns:
        A collection with all servers.
      • getPossiblyUnreadyServerById

        public Optional<Server> getPossiblyUnreadyServerById​(long id)
        Gets a server by it's id, including ready and not ready ones.
        Parameters:
        id - The of the server.
        Returns:
        The server with the given id.
      • addServerToCache

        public void addServerToCache​(ServerImpl server)
        Adds the given server to the cache.
        Parameters:
        server - The server to add.
      • removeServerFromCache

        public void removeServerFromCache​(long serverId)
        Removes the given server from the cache.
        Parameters:
        serverId - The id of the server to remove.
      • addUserToCache

        public void addUserToCache​(User user)
        Adds the given user to the cache.
        Parameters:
        user - The user to add.
      • addGroupChannelToCache

        public void addGroupChannelToCache​(GroupChannel channel)
        Adds a group channel to the cache.
        Parameters:
        channel - The channel to add.
      • removeGroupChannelFromCache

        public void removeGroupChannelFromCache​(long channelId)
        Removes a group channel from the cache.
        Parameters:
        channelId - The id of the channel to remove.
      • addUnavailableServerToCache

        public void addUnavailableServerToCache​(long serverId)
        Adds a server id to the list with unavailable servers.
        Parameters:
        serverId - The id of the server.
      • setYourself

        public void setYourself​(User yourself)
        Sets the user of the connected account.
        Parameters:
        yourself - The user of the connected account.
      • getTimeOffset

        public Long getTimeOffset​()
        Gets the time offset between the Discord time and our local time. Might be null if it hasn't been calculated yet.
        Returns:
        The time offset between the Discord time and our local time.
      • setTimeOffset

        public void setTimeOffset​(Long timeOffset)
        Sets the time offset between the Discord time and our local time.
        Parameters:
        timeOffset - The time offset to set.
      • getOrCreateUser

        public User getOrCreateUser​(com.fasterxml.jackson.databind.JsonNode data)
        Gets a user or creates a new one from the given data.
        Parameters:
        data - The json data of the user.
        Returns:
        The user.
      • getOrCreateKnownCustomEmoji

        public KnownCustomEmoji getOrCreateKnownCustomEmoji​(Server server,
                                                            com.fasterxml.jackson.databind.JsonNode data)
        Gets or creates a new known custom emoji object.
        Parameters:
        server - The server of the emoji.
        data - The data of the emoji.
        Returns:
        The emoji for the given json object.
      • getKnownCustomEmojiOrCreateCustomEmoji

        public CustomEmoji getKnownCustomEmojiOrCreateCustomEmoji​(com.fasterxml.jackson.databind.JsonNode data)
        Gets a known custom emoji or creates a new (unknown) custom emoji object.
        Parameters:
        data - The data of the emoji.
        Returns:
        The emoji for the given json object.
      • getKnownCustomEmojiOrCreateCustomEmoji

        public CustomEmoji getKnownCustomEmojiOrCreateCustomEmoji​(long id,
                                                                  String name,
                                                                  boolean animated)
        Gets a known custom emoji or creates a new (unknown) custom emoji object.
        Parameters:
        id - The id of the emoji.
        name - The name of the emoji.
        animated - Whether the emoji is animated or not.
        Returns:
        The emoji for the given json object.
      • removeCustomEmoji

        public void removeCustomEmoji​(KnownCustomEmoji emoji)
        Removes a known custom emoji object.
        Parameters:
        emoji - The emoji to remove.
      • getOrCreateMessage

        public Message getOrCreateMessage​(TextChannel channel,
                                          com.fasterxml.jackson.databind.JsonNode data)
        Gets or creates a new message object.
        Parameters:
        channel - The channel of the message.
        data - The data of the message.
        Returns:
        The message for the given json object.
      • addMessageToCache

        public void addMessageToCache​(Message message)
        Adds a message to the cache.
        Parameters:
        message - The message to add.
      • removeMessageFromCache

        public void removeMessageFromCache​(long messageId)
        Removes a message from the cache.
        Parameters:
        messageId - The id of the message to remove.
      • addObjectListener

        public <T extends ObjectAttachableListenerListenerManager<T> addObjectListener​(Class<?> objectClass,
                                                                                         long objectId,
                                                                                         Class<T> listenerClass,
                                                                                         T listener)
        Adds an object listener. Adding a listener multiple times to the same object will only add it once and return the same listener manager on each invocation. The order of invocation is according to first addition.
        Type Parameters:
        T - The type of the listener.
        Parameters:
        objectClass - The class of the object.
        objectId - The id of the object.
        listenerClass - The listener class.
        listener - The listener to add.
        Returns:
        The manager for the added listener.
      • removeObjectListener

        public <T extends ObjectAttachableListener> void removeObjectListener​(Class<?> objectClass,
                                                                              long objectId,
                                                                              Class<T> listenerClass,
                                                                              T listener)
        Removes an object listener.
        Type Parameters:
        T - The type of the listener.
        Parameters:
        objectClass - The class of the object.
        objectId - The id of the object.
        listenerClass - The listener class.
        listener - The listener to remove.
      • removeObjectListeners

        public void removeObjectListeners​(Class<?> objectClass,
                                          long objectId)
        Remove all listeners attached to an object.
        Parameters:
        objectClass - The class of the object.
        objectId - The id of the object.
      • getObjectListeners

        public <T extends ObjectAttachableListenerMap<T,List<Class<T>>> getObjectListeners​(Class<?> objectClass,
                                                                                             long objectId)
        Gets a map with all registered listeners that implement one or more ObjectAttachableListeners and their assigned listener classes they listen to.
        Type Parameters:
        T - The type of the listeners.
        Parameters:
        objectClass - The class of the object.
        objectId - The id of the object.
        Returns:
        A map with all registered listeners that implement one or more ObjectAttachableListeners and their assigned listener classes they listen to.
      • getObjectListeners

        public <T extends ObjectAttachableListenerList<T> getObjectListeners​(Class<?> objectClass,
                                                                               long objectId,
                                                                               Class<T> listenerClass)
        Gets all object listeners of the given class.
        Type Parameters:
        T - The type of the listener.
        Parameters:
        objectClass - The class of the object.
        objectId - The id of the object.
        listenerClass - The listener class.
        Returns:
        A list with all object listeners of the given type.
      • getWebSocketAdapter

        public DiscordWebSocketAdapter getWebSocketAdapter​()
        Gets the websocket adapter which is used to connect to Discord.
        Returns:
        The websocket adapter.
      • setMessageCacheSize

        public void setMessageCacheSize​(int capacity,
                                        int storageTimeInSeconds)
        Specified by:
        setMessageCacheSize in interface DiscordApi
      • getProxySelector

        public Optional<ProxySelector> getProxySelector​()
        The proxy selector which should be used to determine the proxies that should be used to connect to the Discord REST API and websocket.
        Returns:
        the proxy selector which should be used to determine the proxies that should be used to connect to the Discord REST API and websocket.
      • getProxy

        public Optional<Proxy> getProxy​()
        The proxy which should be used to connect to the Discord REST API and websocket.
        Returns:
        the proxy which should be used to connect to the Discord REST API and websocket.
      • getProxyAuthenticator

        public Optional<Authenticator> getProxyAuthenticator​()
        The authenticator that should be used to authenticate against proxies that require it.
        Returns:
        the authenticator that should be used to authenticate against proxies that require it.
      • isTrustAllCertificates

        public boolean isTrustAllCertificates​()
        Whether to trust all SSL certificates.
        Returns:
        whether to trust all SSL certificates.