Class JavaDiscordBot

java.lang.Object
com.dianxin.tori.api.bot.JavaDiscordBot

public abstract class JavaDiscordBot extends Object
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:

The class also automatically manages:

  • JDA initialization and shutdown
  • Console command listener
  • Presence and intents setup
  • Basic logging flow
Example usage:

 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected JDABuilder
    Creates and configures the JDABuilder with the bot's intents, activity, and audio module config.
    protected Activity
    Defines the bot's presence/activity shown on Discord.
    Provides 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 String
    Provides the bot token required for authentication with Discord.
    Gets the data storage directory specific to this bot.
    @Nullable String
    getEnv(String variable)
    Retrieves an environment configuration value by its variable name.
    @NonNull String
    getEnvOrDefault(String variable, String defaultValue)
    Retrieves an environment configuration value, returning a fallback default if not found.
    @NonNull String
    Retrieves an environment configuration value by its variable name, throwing an exception if missing.
    @NonNull String
    getEnvOrThrow(String variable, Throwable throwable)
    Retrieves an environment configuration value, throwing a custom throwable if missing.
    Provides the list of gateway intents required for the bot.
    Gets the JDA instance representing the active bot connection.
    protected @NonNull org.slf4j.Logger
    Gets the logger instance dedicated to this bot class.
    @NonNull IBotMeta
    Gets the metadata container associated with this bot.
    final @NonNull Scheduler
    Gets the global task scheduler provided by the Tori environment.
    Gets the self-user profile of the bot on Discord.
    protected void
    Called before the bot shuts down.
    protected void
    Called when the bot fully starts and is ready.
    void
    Gracefully shuts down the bot, updating its status to offline, calling onDisable(), and closing the JDA connection.
    protected void
    Deprecated.
    void
    Starts the bot, initializes JDA, loads intents/activity, registers console commands, and begins console listening.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JavaDiscordBot

      public JavaDiscordBot()
  • Method Details

    • getBotToken

      protected abstract String getBotToken()
      Provides the bot token required for authentication with Discord.
      Returns:
      The bot token as a string.
    • builder

      protected JDABuilder 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

      public void start() throws InterruptedException
      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, calling onDisable(), and closing the JDA connection.
    • getDataFolder

      public File getDataFolder()
      Gets the data storage directory specific to this bot.
      Returns:
      A File object representing the bot's data folder.
      Throws:
      IllegalStateException - If called before the bot metadata is fully initialized (e.g., inside the constructor).
    • getIntents

      protected EnumSet<GatewayIntent> getIntents()
      Provides the list of gateway intents required for the bot.
      Returns:
      An EnumSet of intents or null to use default JDA behavior.
    • getActivity

      protected Activity getActivity()
      Defines the bot's presence/activity shown on Discord.
      Returns:
      A Activity instance or null for no activity.
    • getAudioModuleConfig

      protected AudioModuleConfig getAudioModuleConfig()
      Provides dave session if available.
      Returns:
      A AudioModuleConfig.
    • registerConsoleCommands

      @Deprecated protected void registerConsoleCommands()
      Deprecated.
      Override this to register custom console commands using ConsoleCommandManager.
    • getJda

      public JDA getJda()
      Gets the JDA instance representing the active bot connection.
      Returns:
      The active JDA instance.
    • getSelf

      public User getSelf()
      Gets the self-user profile of the bot on Discord.
      Returns:
      The bot's User entity.
    • getBotInviteLink

      public String getBotInviteLink()
      Generates a default bot invitation link with no pre-defined permissions.
      Returns:
      A string containing the bot invite URL.
    • getBotInviteLink

      public String getBotInviteLink(Permission... permissions)
      Generates a bot invitation link pre-configured with specific permissions.
      Parameters:
      permissions - An array of Permission flags to request upon invitation.
      Returns:
      A string containing the bot invite URL with permissions.
    • getEnv

      @ReleasedSince("26.10.0") public @Nullable String getEnv(String variable)
      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 null if not found.
    • getEnvOrThrow

      @ReleasedSince("26.10.0") public @NonNull String getEnvOrThrow(String variable)
      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

      public @NonNull IBotMeta getMeta()
      Gets the metadata container associated with this bot.
      Returns:
      The IBotMeta instance containing bot information.
    • getLogger

      protected @NonNull org.slf4j.Logger getLogger()
      Gets the logger instance dedicated to this bot class.
      Returns:
      The SLF4J Logger instance.
    • getScheduler

      public final @NonNull Scheduler getScheduler()
      Gets the global task scheduler provided by the Tori environment.
      Returns:
      The active Scheduler instance.