Interface CommandContext


public interface CommandContext
Context provided to text command handlers during command execution.

Contains all necessary information about the command invocation, including the user, guild, channel, and parsed arguments.

Received as a parameter in @TextCommand annotated methods:

@TextCommand(name = "greet", description = "Greet someone")
public void greet(CommandContext context) {
    if (context.hasArgs()) {
        context.reply("Hello, " + context.getArg(0) + "!");
    } else {
        context.reply("Hello, " + context.getUser().getName() + "!");
    }
}

  • Method Summary

    Modifier and Type
    Method
    Description
    default String
    getArg(int index)
    Gets a specific argument by index.
    default int
    Gets the number of arguments.
    Gets the command arguments.
    Gets the full command input after the command name.
    default net.dv8tion.jda.api.entities.User
    Gets the author of the command (alias for getUser).
    net.dv8tion.jda.api.entities.channel.middleman.MessageChannel
    Gets the channel where the command was issued.
    Gets the command name.
    net.dv8tion.jda.api.events.message.MessageReceivedEvent
    Gets the original message event.
    net.dv8tion.jda.api.entities.Guild
    Gets the guild where the command was issued.
    net.dv8tion.jda.api.entities.Member
    Gets the member who issued the command (guild only).
    net.dv8tion.jda.api.entities.Message
    Gets the message that triggered the command.
    net.dv8tion.jda.api.entities.User
    Gets the user who issued the command.
    default boolean
    Checks if there are any arguments.
    boolean
    Checks if the command was issued in a guild.
    default void
    reply(String message)
    Reply to the command message.
    default void
    replyFormat(String format, Object... args)
    Reply with formatted string.
  • Method Details

    • getEvent

      net.dv8tion.jda.api.events.message.MessageReceivedEvent getEvent()
      Gets the original message event.
      Returns:
      the message event
    • getMessage

      net.dv8tion.jda.api.entities.Message getMessage()
      Gets the message that triggered the command.
      Returns:
      the message
    • getUser

      net.dv8tion.jda.api.entities.User getUser()
      Gets the user who issued the command.
      Returns:
      the user
    • getMember

      net.dv8tion.jda.api.entities.Member getMember()
      Gets the member who issued the command (guild only).
      Returns:
      the member or null if in DM
    • getGuild

      net.dv8tion.jda.api.entities.Guild getGuild()
      Gets the guild where the command was issued.
      Returns:
      the guild or null if in DM
    • getChannel

      net.dv8tion.jda.api.entities.channel.middleman.MessageChannel getChannel()
      Gets the channel where the command was issued.
      Returns:
      the message channel
    • getCommand

      String getCommand()
      Gets the command name.
      Returns:
      the command name
    • getArgs

      String[] getArgs()
      Gets the command arguments.
      Returns:
      the arguments array
    • getArgsString

      String getArgsString()
      Gets the full command input after the command name.
      Returns:
      the raw arguments string
    • isFromGuild

      boolean isFromGuild()
      Checks if the command was issued in a guild.
      Returns:
      true if in a guild, false if in DM
    • reply

      default void reply(String message)
      Reply to the command message.
      Parameters:
      message - the message to send
    • replyFormat

      default void replyFormat(String format, Object... args)
      Reply with formatted string.
      Parameters:
      format - the format string
      args - the format arguments
    • getAuthor

      default net.dv8tion.jda.api.entities.User getAuthor()
      Gets the author of the command (alias for getUser).
      Returns:
      the user who sent the command
    • getArg

      default String getArg(int index)
      Gets a specific argument by index.
      Parameters:
      index - the argument index (0-based)
      Returns:
      the argument or empty string if out of bounds
    • hasArgs

      default boolean hasArgs()
      Checks if there are any arguments.
      Returns:
      true if there are arguments
    • getArgCount

      default int getArgCount()
      Gets the number of arguments.
      Returns:
      the argument count