SWAGcommands
Commands

Context menus

Add commands to the user and message right-click menus.

Context menus start from a selected user or message instead of a typed slash name. Put each definition in contextMenusDir; the filename is the display name, including spaces and casing.

src/context-menus/User Information.ts
import { ApplicationCommandType } from "discord.js";
import type { UserContextMenuCommandObject } from "swagcommands";

const info = {
  type: ApplicationCommandType.User,
  deferReply: { ephemeral: true },
  callback: ({ targetUser }) => ({
    content: `${targetUser.username} (${targetUser.id})`,
  }),
} satisfies UserContextMenuCommandObject;

export default info;
src/context-menus/Report Message.ts
import { ApplicationCommandType } from "discord.js";
import type { MessageContextMenuCommandObject } from "swagcommands";

const report = {
  type: ApplicationCommandType.Message,
  guildOnly: true,
  callback: ({ targetMessage }) => ({
    content: `Saved message ${targetMessage.id} for review.`,
  }),
} satisfies MessageContextMenuCommandObject;

export default report;

A user command receives targetUser and possibly targetMember; a message command receives targetMessage. Both get the invoking user, interaction, client, instance, and guild context. Use deferReply for slow work just as you would for a slash command.

Context menus support guildOnly, ownerOnly, testOnly, permissions, preconditions, and init. They don't have message aliases or argument options. A user and message context menu may share a name because Discord distinguishes their types; two of the same type and name are rejected.

Deploy application commands after the client is ready to make the menus appear.