Interface CommandListener


public interface CommandListener
An implementable "Listener" that can be added to a CommandClient and used to handle events relating to Commands.
Author:
John Grosh (jagrosh)
  • Method Details

    • onCommand

      default void onCommand(CommandEvent event, Command command)
      Called when a Command is triggered by a CommandEvent.
      Parameters:
      event - The CommandEvent that triggered the Command
      command - The Command that was triggered
    • onSlashCommand

      default void onSlashCommand(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, SlashCommand command)
      Called when a SlashCommand is triggered by a SlashCommandEvent.
      Parameters:
      event - The SlashCommandEvent that triggered the Command
      command - The SlashCommand that was triggered
    • onCompletedCommand

      default void onCompletedCommand(CommandEvent event, Command command)
      Called when a Command is triggered by a CommandEvent after it's completed successfully.

      Note that a successfully completed command is one that has not encountered an error or exception. Calls that do face errors should be handled by CommandListener#onCommandException

      Parameters:
      event - The CommandEvent that triggered the Command
      command - The Command that was triggered
    • onCompletedSlashCommand

      default void onCompletedSlashCommand(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, SlashCommand command)
      Called when a SlashCommand is triggered by a SlashCommandEvent after it's completed successfully.

      Note that a successfully completed slash command is one that has not encountered an error or exception. Calls that do face errors should be handled by CommandListener#onSlashCommandException

      Parameters:
      event - The SlashCommandEvent that triggered the Command
      command - The SlashCommand that was triggered
    • onTerminatedCommand

      default void onTerminatedCommand(CommandEvent event, Command command)
      Called when a Command is triggered by a CommandEvent but is terminated before completion.
      Parameters:
      event - The CommandEvent that triggered the Command
      command - The Command that was triggered
    • onTerminatedSlashCommand

      default void onTerminatedSlashCommand(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, SlashCommand command)
      Called when a Command is triggered by a SlashCommandEvent but is terminated before completion.
      Parameters:
      event - The SlashCommandEvent that triggered the Command
      command - The SlashCommand that was triggered
    • onNonCommandMessage

      default void onNonCommandMessage(net.dv8tion.jda.api.events.message.MessageReceivedEvent event)
      Called when a MessageReceivedEvent is caught by the Client Listener's but doesn't correspond to a Command.

      In other words, this catches all non-command MessageReceivedEvents allowing you to handle them without implementation of another listener.

      Parameters:
      event - A MessageReceivedEvent that wasn't used to call a Command
    • onCommandException

      default void onCommandException(CommandEvent event, Command command, Throwable throwable)
      Called when a Command catches a Throwable during execution.

      This doesn't account for exceptions thrown during other pre-checks, and should not be treated as such!

      An example of this misconception is via a Category test:

       public class BadCommand extends Command {
      
            public BadCommand() {
                this.name = "bad";
                this.category = new Category("bad category", event -> {
                    // This will throw a NullPointerException if it's not from a Guild!
                    return event.getGuild().getIdLong() == 12345678910111213;
                });
            }
      
            @Override
            protected void execute(CommandEvent) {
                event.reply("This is a bad command!");
            }
      
       }
      The NullPointerException thrown will not be caught by this method!
      Parameters:
      event - The CommandEvent that triggered the Command
      command - The Command that was triggered
      throwable - The Throwable thrown during Command execution
    • onSlashCommandException

      default void onSlashCommandException(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, SlashCommand command, Throwable throwable)
      Called when a SlashCommand catches a Throwable during execution.

      This doesn't account for exceptions thrown during other pre-checks, and should not be treated as such! The NullPointerException thrown will not be caught by this method!

      Parameters:
      event - The CommandEvent that triggered the Command
      command - The Command that was triggered
      throwable - The Throwable thrown during Command execution