|
1 | 1 | ---
|
2 | 2 | title: Context menu commands
|
3 | 3 | ---
|
| 4 | + |
| 5 | +Context menu commands allows users on Discord to run commands by |
| 6 | +either referencing a message or a user. Context menu commands can be |
| 7 | +grouped to either 'Message' or 'User' context menu commands. |
| 8 | + |
| 9 | +Alongside chat input commands, you can use CommandKit to also register |
| 10 | +and handle context menu commands. |
| 11 | + |
| 12 | +Context menu commands live in the same directory as your chat input |
| 13 | +commands. To begin, create a command file in your `src/app/commands` |
| 14 | +directory. |
| 15 | + |
| 16 | +This guide will create 2 simple `report-message` and `report-user` |
| 17 | +commands. It's not an actual reporting system, but it should make |
| 18 | +understanding the nature of these context menu commands easier. |
| 19 | + |
| 20 | +## Message context menu command |
| 21 | + |
| 22 | +```ts title="src/app/commands/report-message.ts" |
| 23 | +import type { |
| 24 | + CommandData, |
| 25 | + MessageContextMenuCommand, |
| 26 | +} from 'commandkit'; |
| 27 | +import { MessageFlags } from 'discord.js'; |
| 28 | + |
| 29 | +export const command: CommandData = { |
| 30 | + name: 'report-message', |
| 31 | + description: 'Report a message to moderators.', |
| 32 | +}; |
| 33 | + |
| 34 | +export const messageContextMenu: MessageContextMenuCommand = async ({ |
| 35 | + interaction, |
| 36 | +}) => { |
| 37 | + const content = interaction.targetMessage.content; |
| 38 | + |
| 39 | + // you could add your message reporting logic here |
| 40 | + |
| 41 | + await interaction.reply({ |
| 42 | + content: `You have reported the following message: ${content}`, |
| 43 | + flags: MessageFlags.Ephemeral, |
| 44 | + }); |
| 45 | +}; |
| 46 | +``` |
| 47 | + |
| 48 | +By just exporting the `messageContextMenu` function from your command |
| 49 | +file, CommandKit will properly update your command type before |
| 50 | +registering it to the Discord API. You don't have to manually set a |
| 51 | +`type` in your `command` object, as you would with most command |
| 52 | +handlers. |
| 53 | + |
| 54 | +## User context menu command |
| 55 | + |
| 56 | +The logic for registering and handling user context menu commands is |
| 57 | +very similar to message context menu commands. |
| 58 | + |
| 59 | +```ts title="src/app/commands/report-user.ts" |
| 60 | +import type { CommandData, UserContextMenuCommand } from 'commandkit'; |
| 61 | +import { MessageFlags } from 'discord.js'; |
| 62 | + |
| 63 | +export const command: CommandData = { |
| 64 | + name: 'report-user', |
| 65 | + description: 'Report a user to moderators.', |
| 66 | +}; |
| 67 | + |
| 68 | +export const userContextMenu: UserContextMenuCommand = async ({ |
| 69 | + interaction, |
| 70 | +}) => { |
| 71 | + const userId = interaction.targetUser.id; |
| 72 | + |
| 73 | + // you could add your user reporting logic here |
| 74 | + |
| 75 | + await interaction.reply({ |
| 76 | + content: `You have reported a user with the ID: ${userId}`, |
| 77 | + flags: MessageFlags.Ephemeral, |
| 78 | + }); |
| 79 | +}; |
| 80 | +``` |
0 commit comments