SWAGcommands
Guides

Events

Add your own Discord listeners beside the built-in command routing.

SWAGcommands registers the event listeners it needs for commands. You can add your own by passing events.dir and putting functions under folders named for discord.js event names.

src/events/
├── clientReady/
│   └── announce.ts
└── messageCreate/
    └── log.ts
src/events/messageCreate/log.ts
import type { Message } from "discord.js";

export default async function logMessage(message: Message) {
  if (message.author.bot) return;
  console.log(message.author.username, message.content);
}
await SWAG.create({
  client,
  events: { dir: "./dist/events" },
});

Event functions receive Discord's usual arguments, plus the SWAGcommands instance as the final argument. For example, a messageCreate function may accept (message, instance). The framework awaits callbacks and reports thrown errors as EventExecutionError through onError. Its built-in command routing still runs.

Filtered event folders

A nested folder can name a filter configured under its event:

src/events/messageCreate/isHuman/audit.ts
await SWAG.create({
  client,
  events: {
    dir: "./dist/events",
    messageCreate: {
      isHuman: (message: Message) => !message.author.bot,
    },
  },
});

For custom event files, the parent folder name is looked up under events[eventName]; the filter runs before the callback. Keep event modules and callbacks small. If a filter or callback throws, the framework reports it and stops processing later callbacks for that emitted event.

For startup work or repeated jobs rather than a Discord event, use features.

On this page