Skip to main content
Version: 1.x

Context

Context​

Represents the execution context for a command, providing access to Discord.js objects, command metadata, and various utility methods for command execution.

Signature
class Context<ExecutionMode extends CommandExecutionMode = CommandExecutionMode, Args extends Record<string, any> = Record<string, any>> {
public readonly interaction: ContextParameters<ExecutionMode>['interaction'];
public readonly message: ContextParameters<ExecutionMode>['message'];
public readonly guild!: Guild | null;
public readonly guildId!: string | null;
public readonly channel!: TextBasedChannel | null;
public readonly channelId!: string | null;
public readonly client: Client<true>;
public readonly command: LoadedCommand;
constructor(commandkit: CommandKit, config: ContextParameters<ExecutionMode, Args>)
store: void
commandName: string
invokedCommandName: string
options: CommandContextOptions<ExecutionMode>
forwarded: boolean
forwardCommand(command: C) => Promise<never>;
executionMode: ExecutionMode
isInteraction() => this is InteractionCommandContext;
isChatInputCommand() => this is ChatInputCommandContext;
isAutocomplete() => this is AutocompleteCommandContext;
isMessageContextMenu() => this is MessageContextMenuCommandContext;
isUserContextMenu() => this is UserContextMenuCommandContext;
isMessage() => this is Context<'message'>;
getCommandIdentifier() => string;
getGuildLocale() => Locale | null;
getUserLocale() => Locale | null;
getLocale(preferUser: = false) => Locale;
setLocale(locale: Locale | null) => void;
clone(config?: Partial<ContextParameters<ExecutionMode>>) => Context<ExecutionMode>;
isMiddleware() => this is MiddlewareContext<ExecutionMode>;
args() => string[];
}

interaction​

property
ContextParameters<ExecutionMode>['interaction']

The interaction that triggered the command.

message​

property
ContextParameters<ExecutionMode>['message']

The message that triggered the command.

guild​

property
Guild | null

The guild where the command was triggered.

guildId​

property
string | null

The guild ID where the command was triggered.

channel​

property
TextBasedChannel | null

The channel where the command was triggered.

channelId​

property
string | null

The channel id where the command was triggered.

client​

property
Client<true>

The client instance.

command​

The command that this context belongs to.

constructor​

method
(commandkit: CommandKit, config: ContextParameters<ExecutionMode, Args>) => Context

Creates a new command context.

store​

property

The shared key-value store for this context. This can be used to store data that needs to be shared between middlewares or commands. This store is shared across all contexts in the same command execution, including the cloned contexts and middleware contexts.

commandName​

property
string

Gets the name of the current command.

invokedCommandName​

property
string

Gets the invoked command name (could be an alias for message commands)

options​

property
CommandContextOptions<ExecutionMode>

Gets the command options based on the execution mode.

forwarded​

property
boolean

Whether this context was forwarded from another context. This happens when a command forwards its context to another command.

forwardCommand​

method
(command: C) => Promise<never>

Forwards the context to another command. The target handler will be the same as current handler.

executionMode​

property
ExecutionMode

The execution mode of the command.

isInteraction​

method

Whether the command was triggered by an interaction.

isChatInputCommand​

method

Whether the command was triggered by a slash command interaction.

isAutocomplete​

method

Whether the command was triggered by an autocomplete interaction.

isMessageContextMenu​

Whether the command was triggered by a message context menu interaction.

isUserContextMenu​

method

Whether the command was triggered by a user context menu interaction.

isMessage​

method
() => this is Context<'message'>

Whether the command was triggered by a message.

getCommandIdentifier​

method
() => string

Returns the command identifier.

getGuildLocale​

method
() => Locale | null

Returns the locale of the guild where this command was triggered.

getUserLocale​

method
() => Locale | null

Returns the locale of the user who triggered this command.

getLocale​

method
(preferUser: = false) => Locale

Returns the locale for this command.

setLocale​

method
(locale: Locale | null) => void

Sets the locale for this command.

clone​

method
(config?: Partial<ContextParameters<ExecutionMode>>) => Context<ExecutionMode>

Creates a clone of this context

isMiddleware​

method
() => this is MiddlewareContext<ExecutionMode>

Checks if this context is a middleware context.

args​

method
() => string[]

Gets the command arguments (only available for message commands).