SWAGcommands
Commands

Message commands

Prefixes, aliases, arguments, and replies for text commands.

Message commands use the CommandType.LEGACY enum value. The name sounds historical, but it's the current API for commands people type into chat: !ping, !echo hello, and so on.

src/commands/echo.ts
import { CommandType } from "swagcommands";
import type { CommandObject } from "swagcommands";

const echo = {
  type: CommandType.LEGACY,
  aliases: ["say"],
  reply: true,
  minArgs: 1,
  expectedArgs: "<text>",
  callback: ({ text, user }) => `${user.username} said: ${text}`,
} satisfies CommandObject;

export default echo;

With the default prefix, !echo hello world and !say hello world both invoke it. reply: true calls message.reply(); otherwise SWAGcommands sends to the channel. The router lowercases the command name, splits the remaining text on whitespace, and passes it as args. text is the joined argument text. Quotes aren't special: !echo "hello world" is still split on whitespace.

Gateway intents matter

For message commands in a guild, create your Discord client with GuildMessages and MessageContent, and enable the Message Content privileged intent in your application's Discord settings. Guilds is useful for guild metadata. The built-in message router ignores bot-authored messages and only handles sendable channels.

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});

Prefixes

Set defaultPrefix in SWAG.create(); it defaults to !. Guild-specific prefixes live in a prefix store. DMs use the default prefix. Message commands don't need deployCommands() because Discord doesn't register them as application commands.

A little less manual checking

minArgs, maxArgs, and expectedArgs feed the built-in ArgumentCount precondition. A bad invocation gets a usage hint. maxArgs: -1 means unlimited. See Preconditions for guild, owner, and permission checks.

On this page