Package com.dianxin.tori.api.bot
Class JavaDiscordBot
java.lang.Object
com.dianxin.tori.api.bot.JavaDiscordBot
Represents a base class for creating Discord bots using JDA with an extended
lifecycle system, console command support, and customizable startup options.
Users of this API should extend this class and override the desired methods such as:
onEnable()- Called after the bot finishes starting.onDisable()- Called before the bot shuts down.registerConsoleCommands()- Register custom console commands.getIntents()- Provide requiredGatewayIntentvalues.getActivity()- Define the bot presence/activity.
The class also automatically manages:
- JDA initialization and shutdown
- Console command listener
- Presence and intents setup
- Basic logging flow
public class MyBot extends JavaDiscordBot {
public MyBot() {
super("YOUR_TOKEN", "MyBot");
}
@Override
public void onEnable() {
getLogger().info("MyBot is ready!");
}
@Override
protected EnumSet<GatewayIntent> getIntents() {
return IntentContext.getDefaultIntents();
}
@Override
protected Activity getActivity() {
return Activity.playing("Hello world!");
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected JDABuilderbuilder()Creates and configures the JDABuilder with the bot's intents, activity, and audio module config.protected ActivityDefines the bot's presence/activity shown on Discord.protected AudioModuleConfigProvides dave session if available.Generates a default bot invitation link with no pre-defined permissions.getBotInviteLink(Permission... permissions) Generates a bot invitation link pre-configured with specific permissions.protected abstract StringProvides the bot token required for authentication with Discord.Gets the data storage directory specific to this bot.@Nullable StringRetrieves an environment configuration value by its variable name.@NonNull StringgetEnvOrDefault(String variable, String defaultValue) Retrieves an environment configuration value, returning a fallback default if not found.@NonNull StringgetEnvOrThrow(String variable) Retrieves an environment configuration value by its variable name, throwing an exception if missing.@NonNull StringgetEnvOrThrow(String variable, Throwable throwable) Retrieves an environment configuration value, throwing a custom throwable if missing.protected EnumSet<GatewayIntent> Provides the list of gateway intents required for the bot.getJda()Gets the JDA instance representing the active bot connection.protected @NonNull org.slf4j.LoggerGets the logger instance dedicated to this bot class.@NonNull IBotMetagetMeta()Gets the metadata container associated with this bot.final @NonNull SchedulerGets the global task scheduler provided by the Tori environment.getSelf()Gets the self-user profile of the bot on Discord.protected voidCalled before the bot shuts down.protected voidonEnable()Called when the bot fully starts and is ready.voidGracefully shuts down the bot, updating its status to offline, callingonDisable(), and closing the JDA connection.protected voidDeprecated.voidstart()Starts the bot, initializes JDA, loads intents/activity, registers console commands, and begins console listening.
-
Constructor Details
-
JavaDiscordBot
public JavaDiscordBot()
-
-
Method Details
-
getBotToken
Provides the bot token required for authentication with Discord.- Returns:
- The bot token as a string.
-
builder
Creates and configures the JDABuilder with the bot's intents, activity, and audio module config.Subclasses can override this method to further customize the JDABuilder.
- Returns:
- The configured JDABuilder instance.
-
start
Starts the bot, initializes JDA, loads intents/activity, registers console commands, and begins console listening.This method blocks until the JDA session is ready.
- Throws:
InterruptedException- If the current thread is interrupted.
-
onEnable
protected void onEnable()Called when the bot fully starts and is ready. Override to initialize listeners, commands, database, etc. -
onDisable
protected void onDisable()Called before the bot shuts down. Override to close resources or save data. -
onShutdown
public void onShutdown()Gracefully shuts down the bot, updating its status to offline, callingonDisable(), and closing the JDA connection. -
getDataFolder
Gets the data storage directory specific to this bot.- Returns:
- A
Fileobject representing the bot's data folder. - Throws:
IllegalStateException- If called before the bot metadata is fully initialized (e.g., inside the constructor).
-
getIntents
Provides the list of gateway intents required for the bot.- Returns:
- An
EnumSetof intents ornullto use default JDA behavior.
-
getActivity
Defines the bot's presence/activity shown on Discord.- Returns:
- A
Activityinstance ornullfor no activity.
-
getAudioModuleConfig
Provides dave session if available.- Returns:
- A AudioModuleConfig.
-
registerConsoleCommands
Deprecated.Override this to register custom console commands usingConsoleCommandManager. -
getJda
Gets the JDA instance representing the active bot connection.- Returns:
- The active
JDAinstance.
-
getSelf
Gets the self-user profile of the bot on Discord.- Returns:
- The bot's
Userentity.
-
getBotInviteLink
Generates a default bot invitation link with no pre-defined permissions.- Returns:
- A string containing the bot invite URL.
-
getBotInviteLink
Generates a bot invitation link pre-configured with specific permissions.- Parameters:
permissions- An array ofPermissionflags to request upon invitation.- Returns:
- A string containing the bot invite URL with permissions.
-
getEnv
Retrieves an environment configuration value by its variable name.- Parameters:
variable- The name of the environment variable.- Returns:
- The string value of the environment variable, or
nullif not found.
-
getEnvOrThrow
Retrieves an environment configuration value by its variable name, throwing an exception if missing.- Parameters:
variable- The name of the environment variable.- Returns:
- The string value of the environment variable.
- Throws:
RuntimeException- If the variable is not defined.
-
getEnvOrDefault
@ReleasedSince("26.10.0") public @NonNull String getEnvOrDefault(String variable, String defaultValue) Retrieves an environment configuration value, returning a fallback default if not found.- Parameters:
variable- The name of the environment variable.defaultValue- The fallback value to return if the variable is absent.- Returns:
- The configuration value or the default value.
-
getEnvOrThrow
@ReleasedSince("26.10.0") public @NonNull String getEnvOrThrow(String variable, Throwable throwable) throws Throwable Retrieves an environment configuration value, throwing a custom throwable if missing.- Parameters:
variable- The name of the environment variable.throwable- The throwable to be thrown if the variable is absent.- Returns:
- The string value of the environment variable.
- Throws:
Throwable- If the variable is not found.
-
getMeta
Gets the metadata container associated with this bot.- Returns:
- The
IBotMetainstance containing bot information.
-
getLogger
protected @NonNull org.slf4j.Logger getLogger()Gets the logger instance dedicated to this bot class.- Returns:
- The SLF4J
Loggerinstance.
-
getScheduler
Gets the global task scheduler provided by the Tori environment.- Returns:
- The active
Schedulerinstance.
-