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 TypeMethodDescriptiondefault StringgetArg(int index) Gets a specific argument by index.default intGets the number of arguments.String[]getArgs()Gets the command arguments.Gets the full command input after the command name.default net.dv8tion.jda.api.entities.UserGets the author of the command (alias for getUser).net.dv8tion.jda.api.entities.channel.middleman.MessageChannelGets the channel where the command was issued.Gets the command name.net.dv8tion.jda.api.events.message.MessageReceivedEventgetEvent()Gets the original message event.net.dv8tion.jda.api.entities.GuildgetGuild()Gets the guild where the command was issued.net.dv8tion.jda.api.entities.MemberGets the member who issued the command (guild only).net.dv8tion.jda.api.entities.MessageGets the message that triggered the command.net.dv8tion.jda.api.entities.UsergetUser()Gets the user who issued the command.default booleanhasArgs()Checks if there are any arguments.booleanChecks if the command was issued in a guild.default voidReply to the command message.default voidreplyFormat(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
-
getArgs
-
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
Reply to the command message.- Parameters:
message- the message to send
-
replyFormat
-
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
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
-