Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api;
import net.dv8tion.jda.annotations.Incubating;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.attribute.IGuildChannelContainer;
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.emoji.ApplicationEmoji;
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.entities.sticker.*;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.hooks.IEventManager;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.DirectAudioController;
import net.dv8tion.jda.api.managers.Presence;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.restaction.*;
import net.dv8tion.jda.api.requests.restaction.pagination.EntitlementPaginationAction;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.Once;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.api.utils.cache.CacheView;
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import net.dv8tion.jda.internal.requests.CompletedRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EntityString;
import net.dv8tion.jda.internal.utils.Helpers;
import okhttp3.OkHttpClient;
import org.jetbrains.annotations.Unmodifiable;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.regex.Matcher;
/**
* The core of JDA. Acts as a registry system of JDA. All parts of the API can be accessed starting from this class.
*
* @see JDABuilder
*/
public interface JDA extends IGuildChannelContainer
{
/**
* Represents the connection status of JDA and its Main WebSocket.
*/
enum Status
{
/**JDA is currently setting up supporting systems like the AudioSystem.*/
INITIALIZING(true),
/**JDA has finished setting up supporting systems and is ready to log in.*/
INITIALIZED(true),
/**JDA is currently attempting to log in.*/
LOGGING_IN(true),
/**JDA is currently attempting to connect it's websocket to Discord.*/
CONNECTING_TO_WEBSOCKET(true),
/**JDA has successfully connected it's websocket to Discord and is sending authentication*/
IDENTIFYING_SESSION(true),
/**JDA has sent authentication to discord and is awaiting confirmation*/
AWAITING_LOGIN_CONFIRMATION(true),
/**JDA is populating internal objects.
* This process often takes the longest of all Statuses (besides CONNECTED)*/
LOADING_SUBSYSTEMS(true),
/**JDA has finished loading everything, is receiving information from Discord and is firing events.*/
CONNECTED(true),
/**JDA's main websocket has been disconnected. This DOES NOT mean JDA has shutdown permanently.
* This is an in-between status. Most likely ATTEMPTING_TO_RECONNECT or SHUTTING_DOWN/SHUTDOWN will soon follow.*/
DISCONNECTED,
/** JDA session has been added to {@link net.dv8tion.jda.api.utils.SessionController SessionController}
* and is awaiting to be dequeued for reconnecting.*/
RECONNECT_QUEUED,
/**When trying to reconnect to Discord JDA encountered an issue, most likely related to a lack of internet connection,
* and is waiting to try reconnecting again.*/
WAITING_TO_RECONNECT,
/**JDA has been disconnected from Discord and is currently trying to reestablish the connection.*/
ATTEMPTING_TO_RECONNECT,
/**JDA has received a shutdown request or has been disconnected from Discord and reconnect is disabled, thus,
* JDA is in the process of shutting down*/
SHUTTING_DOWN,
/**JDA has finished shutting down and this instance can no longer be used to communicate with the Discord servers.*/
SHUTDOWN,
/**While attempting to authenticate, Discord reported that the provided authentication information was invalid.*/
FAILED_TO_LOGIN;
private final boolean isInit;
Status(boolean isInit)
{
this.isInit = isInit;
}
Status()
{
this.isInit = false;
}
public boolean isInit()
{
return isInit;
}
}
/**
* Represents the information used to create this shard.
*/
class ShardInfo
{
/** Default sharding config with one shard */
public static final ShardInfo SINGLE = new ShardInfo(0, 1);
int shardId;
int shardTotal;
public ShardInfo(int shardId, int shardTotal)
{
this.shardId = shardId;
this.shardTotal = shardTotal;
}
/**
* Represents the id of the shard of the current instance.
* This value will be between 0 and ({@link #getShardTotal()} - 1).
*
* @return The id of the currently logged in shard.
*/
public int getShardId()
{
return shardId;
}
/**
* The total amount of shards based on the value provided during JDA instance creation using
* {@link JDABuilder#useSharding(int, int)}.
* This does not query Discord to determine the total number of shards.
* This does not represent the amount of logged in shards.
* It strictly represents the integer value provided to discord
* representing the total amount of shards that the developer indicated that it was going to use when
* initially starting JDA.
*
* @return The total of shards based on the total provided by the developer during JDA initialization.
*/
public int getShardTotal()
{
return shardTotal;
}
/**
* Provides a shortcut method for easily printing shard info.
* Format: "[# / #]"
* Where the first # is shardId and the second # is shardTotal.
*
* @return A String representing the information used to build this shard.
*/
@Nonnull
public String getShardString()
{
return "[" + shardId + " / " + shardTotal + "]";
}
@Nonnull
@Override
public String toString()
{
return new EntityString(this)
.addMetadata("currentShard", getShardString())
.addMetadata("totalShards", getShardTotal())
.toString();
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof ShardInfo))
return false;
ShardInfo oInfo = (ShardInfo) o;
return shardId == oInfo.getShardId() && shardTotal == oInfo.getShardTotal();
}
}
/**
* Gets the current {@link net.dv8tion.jda.api.JDA.Status Status} of the JDA instance.
*
* @return Current JDA status.
*/
@Nonnull
Status getStatus();
/**
* The {@link GatewayIntent GatewayIntents} for this JDA session.
*
* @return {@link EnumSet} of active gateway intents
*/
@Nonnull
EnumSet getGatewayIntents();
/**
* The {@link CacheFlag cache flags} that have been enabled for this JDA session.
*
* @return Copy of the EnumSet of cache flags for this session
*/
@Nonnull
EnumSet getCacheFlags();
/**
* Attempts to remove the user with the provided id from the cache.
* If you attempt to remove the {@link #getSelfUser() SelfUser} this will simply return {@code false}.
*
*
This should be used by an implementation of {@link net.dv8tion.jda.api.utils.MemberCachePolicy MemberCachePolicy}
* as an upstream request to remove a member.
*
* @param userId
* The target user id
*
* @return True, if the cache was changed
*/
boolean unloadUser(long userId);
/**
* The time in milliseconds that discord took to respond to our last heartbeat
* This roughly represents the WebSocket ping of this session
*
*
{@link net.dv8tion.jda.api.requests.RestAction RestAction} request times do not
* correlate to this value!
*
*
The {@link net.dv8tion.jda.api.events.GatewayPingEvent GatewayPingEvent} indicates an update to this value.
*
* @return time in milliseconds between heartbeat and the heartbeat ack response
*
* @see #getRestPing() Getting RestAction ping
*/
long getGatewayPing();
/**
* The time in milliseconds that discord took to respond to a REST request.
* This will request the current user from the API and calculate the time the response took.
*
*
*
*
* @param status
* The init status to wait for, once JDA has reached the specified
* stage of the startup cycle this method will return.
*
* @throws InterruptedException
* If this thread is interrupted while waiting
* @throws IllegalArgumentException
* If the provided status is null or not an init status ({@link Status#isInit()})
* @throws IllegalStateException
* If JDA is shutdown during this wait period
*
* @return The current JDA instance, for chaining convenience
*/
@Nonnull
default JDA awaitStatus(@Nonnull JDA.Status status) throws InterruptedException
{
//This is done to retain backwards compatible ABI as it would otherwise change the signature of the method
// which would require recompilation for all users (including extension libraries)
return awaitStatus(status, new JDA.Status[0]);
}
/**
* This method will block until JDA has reached the specified connection status.
*
*
*
*
* @param status
* The init status to wait for, once JDA has reached the specified
* stage of the startup cycle this method will return.
* @param failOn
* Optional failure states that will force a premature return
*
* @throws InterruptedException
* If this thread is interrupted while waiting
* @throws IllegalArgumentException
* If the provided status is null or not an init status ({@link Status#isInit()})
* @throws IllegalStateException
* If JDA is shutdown during this wait period
*
* @return The current JDA instance, for chaining convenience
*/
@Nonnull
JDA awaitStatus(@Nonnull JDA.Status status, @Nonnull JDA.Status... failOn) throws InterruptedException;
/**
* This method will block until JDA has reached the status {@link Status#CONNECTED}.
* This status means that JDA finished setting up its internal cache and is ready to be used.
*
* @throws InterruptedException
* If this thread is interrupted while waiting
* @throws IllegalStateException
* If JDA is shutdown during this wait period
*
* @return The current JDA instance, for chaining convenience
*/
@Nonnull
default JDA awaitReady() throws InterruptedException
{
return awaitStatus(Status.CONNECTED);
}
/**
* Blocks the current thread until {@link #getStatus()} returns {@link Status#SHUTDOWN}.
* This can be useful in certain situations like disabling class loading.
*
*
Note that shutdown time depends on the length of the rate-limit queue.
* You can use {@link #shutdownNow()} to cancel all pending requests and immediately shutdown.
*
*
Example
*
{@code
* jda.shutdown();
* // Allow at most 10 seconds for remaining requests to finish
* if (!jda.awaitShutdown(10, TimeUnit.SECONDS)) {
* jda.shutdownNow(); // Cancel all remaining requests
* jda.awaitShutdown(); // Wait until shutdown is complete (indefinitely)
* }
* }
*
*
This will not implicitly call {@code shutdown()}, you are responsible to ensure that the shutdown process has started.
*
* @param duration
* The maximum time to wait, or 0 to wait indefinitely
* @param unit
* The time unit for the duration
*
* @throws IllegalArgumentException
* If the provided unit is null
* @throws InterruptedException
* If the current thread is interrupted while waiting
*
* @return False, if the timeout has elapsed before the shutdown has completed, true otherwise.
*/
@CheckReturnValue
boolean awaitShutdown(long duration, @Nonnull TimeUnit unit) throws InterruptedException;
/**
* Blocks the current thread until {@link #getStatus()} returns {@link Status#SHUTDOWN}.
* This can be useful in certain situations like disabling class loading.
*
*
Note that shutdown time depends on the length of the rate-limit queue.
* You can use {@link #shutdownNow()} to cancel all pending requests and immediately shutdown.
*
*
Example
*
{@code
* jda.shutdown();
* // Allow at most 10 seconds for remaining requests to finish
* if (!jda.awaitShutdown(Duration.ofSeconds(10))) {
* jda.shutdownNow(); // Cancel all remaining requests
* jda.awaitShutdown(); // Wait until shutdown is complete (indefinitely)
* }
* }
*
*
This will not implicitly call {@code shutdown()}, you are responsible to ensure that the shutdown process has started.
*
* @param timeout
* The maximum time to wait, or {@link Duration#ZERO} to wait indefinitely
*
* @throws IllegalArgumentException
* If the provided timeout is null
* @throws InterruptedException
* If the current thread is interrupted while waiting
*
* @return False, if the timeout has elapsed before the shutdown has completed, true otherwise.
*/
@CheckReturnValue
default boolean awaitShutdown(@Nonnull Duration timeout) throws InterruptedException
{
Checks.notNull(timeout, "Timeout");
return awaitShutdown(timeout.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* Blocks the current thread until {@link #getStatus()} returns {@link Status#SHUTDOWN}.
* This can be useful in certain situations like disabling class loading.
*
*
This will wait indefinitely by default. Use {@link #awaitShutdown(Duration)} to set a timeout.
*
*
Note that shutdown time depends on the length of the rate-limit queue.
* You can use {@link #shutdownNow()} to cancel all pending requests and immediately shutdown.
*
*
Example
*
{@code
* jda.shutdown();
* // Allow at most 10 seconds for remaining requests to finish
* if (!jda.awaitShutdown(Duration.ofSeconds(10))) {
* jda.shutdownNow(); // Cancel all remaining requests
* jda.awaitShutdown(); // Wait until shutdown is complete (indefinitely)
* }
* }
*
*
This will not implicitly call {@code shutdown()}, you are responsible to ensure that the shutdown process has started.
*
* @throws IllegalArgumentException
* If the provided timeout is null
* @throws InterruptedException
* If the current thread is interrupted while waiting
*
* @return Always true
*/
default boolean awaitShutdown() throws InterruptedException
{
return awaitShutdown(0, TimeUnit.MILLISECONDS);
}
/**
* Cancels all currently scheduled {@link RestAction} requests.
* When a {@link RestAction} is cancelled, a {@link java.util.concurrent.CancellationException} will be provided
* to the failure callback. This means {@link RestAction#queue(Consumer, Consumer)} will invoke the second callback
* and {@link RestAction#complete()} will throw an exception.
*
*
This is only recommended as an extreme last measure to avoid backpressure.
* If you want to stop requests on shutdown you should use {@link #shutdownNow()} instead of this method.
*
* @return how many requests were cancelled
*
* @see RestAction#setCheck(BooleanSupplier)
*/
int cancelRequests();
/**
* {@link ScheduledExecutorService} used to handle rate-limits for {@link RestAction}
* executions. This is also used in other parts of JDA related to http requests.
*
* @return The {@link ScheduledExecutorService} used for http request handling
*
* @since 4.0.0
*/
@Nonnull
ScheduledExecutorService getRateLimitPool();
/**
* {@link ScheduledExecutorService} used to send WebSocket messages to discord.
* This involves initial setup of guilds as well as keeping the connection alive.
*
* @return The {@link ScheduledExecutorService} used for WebSocket transmissions
*
* @since 4.0.0
*/
@Nonnull
ScheduledExecutorService getGatewayPool();
/**
* {@link ExecutorService} used to handle {@link RestAction} callbacks
* and completions. This is also used for handling {@link net.dv8tion.jda.api.entities.Message.Attachment} downloads
* when needed.
* By default this uses the {@link ForkJoinPool#commonPool() CommonPool} of the runtime.
*
* @return The {@link ExecutorService} used for callbacks
*
* @since 4.0.0
*/
@Nonnull
ExecutorService getCallbackPool();
/**
* The {@link OkHttpClient} used for handling http requests from {@link RestAction RestActions}.
*
* @return The http client
*
* @since 4.0.0
*/
@Nonnull
OkHttpClient getHttpClient();
/**
* Direct access to audio (dis-)connect requests.
* This should not be used when normal audio operation is desired.
*
*
The correct way to open and close an audio connection is through the {@link Guild Guild's}
* {@link AudioManager}.
*
* @throws IllegalStateException
* If {@link GatewayIntent#GUILD_VOICE_STATES} is disabled
*
* @return The {@link DirectAudioController} for this JDA instance
*
* @since 4.0.0
*/
@Nonnull
DirectAudioController getDirectAudioController();
/**
* Changes the internal EventManager.
*
*
The default EventManager is {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener}.
* There is also an {@link net.dv8tion.jda.api.hooks.AnnotatedEventManager AnnotatedEventManager} available.
*
* @param manager
* The new EventManager to use
*/
void setEventManager(@Nullable IEventManager manager);
/**
* Adds all provided listeners to the event-listeners that will be used to handle events.
* This uses the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} by default.
* To switch to the {@link net.dv8tion.jda.api.hooks.AnnotatedEventManager AnnotatedEventManager}, use {@link #setEventManager(IEventManager)}.
*
*
Note: when using the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener must be instance of {@link net.dv8tion.jda.api.hooks.EventListener EventListener}!
*
* @param listeners
* The listener(s) which will react to events.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*/
void addEventListener(@Nonnull Object... listeners);
/**
* Removes all provided listeners from the event-listeners and no longer uses them to handle events.
*
* @param listeners
* The listener(s) to be removed.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*/
void removeEventListener(@Nonnull Object... listeners);
/**
* Immutable List of Objects that have been registered as EventListeners.
*
* @return List of currently registered Objects acting as EventListeners.
*/
@Nonnull
List