defineCommand
Command type, run context, and the identity helper for command definitions.
defineCommand(command)
function defineCommand<S extends CommandOptions>(command: Command<S>): Command<S>;Identity function — returns its argument unchanged. Exists purely for IDE autocomplete and structural validation; at runtime it's the same as the inline object.
import { defineCommand } from "@djs-commands/core";
const ping = defineCommand({
name: "ping",
description: "Replies with pong",
run: async ({ reply }) => {
await reply("pong");
},
});Command<S>
interface Command<S extends CommandOptions = Record<string, never>> {
name: string;
description: string;
options?: S;
run: CommandRun<S>;
ownerOnly?: boolean;
guildOnly?: boolean;
channels?: readonly string[];
permissions?: readonly PermissionsString[];
roles?: readonly string[];
validators?: readonly Validator[];
cooldown?: CooldownConfig;
legacy?: CommandLegacyConfig;
}The full shape of a command definition. See Concepts → Commands for what each field does.
AnyCommand
type AnyCommand = Command<any>;Erased-generic alias — used in heterogeneous arrays and registries where the option schema varies between commands.
CommandRun<S>
type CommandRun<S extends CommandOptions = Record<string, never>> =
(ctx: CommandRunContext<S>) => void | Promise<void>;The handler signature. Receives a CommandRunContext.
CommandRunContext<S>
type CommandRunContext<S extends CommandOptions = Record<string, never>> =
| SlashRunContext<S>
| LegacyRunContext<S>;
interface BaseRunContext<S> {
client: Client;
author: User;
guild: Guild | null;
member: GuildMember | null;
channel: TextBasedChannel | null;
channelId: string | null;
options: ResolveOptions<S>;
reply: (content: string | { content?: string; ephemeral?: boolean }) => Promise<unknown>;
}Discriminated union over invocation source.
SlashRunContext<S>
type SlashRunContext<S> = BaseRunContext<S> & {
type: "slash";
interaction: ChatInputCommandInteraction;
};The slash-command branch — interaction is the raw discord.js interaction.
LegacyRunContext<S>
type LegacyRunContext<S> = BaseRunContext<S> & {
type: "legacy";
message: Message;
};The legacy-prefix branch — message is the raw discord.js message.
run: async (ctx) => {
if (ctx.type === "slash") {
await ctx.interaction.deferReply({ ephemeral: true });
}
await ctx.reply("hi");
}CommandLegacyConfig
interface CommandLegacyConfig {
enabled?: boolean;
aliases?: readonly string[];
}Per-command legacy-mode opt-in. enabled defaults to true when the handler-level legacy switch is on. aliases are alternative invocation names.
defineCommand({
name: "ping",
description: "Replies with pong",
legacy: { enabled: true, aliases: ["p", "pong"] },
run: async ({ reply }) => { await reply("pong"); },
});Last updated on
