|
| 1 | +import { Tabs } from 'nextra/components'; |
| 2 | + |
1 | 3 | # Commands Setup |
| 4 | + |
| 5 | +CommandKit supports both slash commands and context menu commands. Since both have the same triggers (interactions) and similar command structure, they are both stored and handled from the same commands directory. |
| 6 | + |
| 7 | +### Slash Command |
| 8 | + |
| 9 | +Here's an example `/ping` slash command which replies with "Pong!" |
| 10 | + |
| 11 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 12 | + <Tabs.Tab> |
| 13 | + ```js filename="commands/misc/ping.js" copy |
| 14 | + module.exports = { |
| 15 | + data: { |
| 16 | + name: 'ping', |
| 17 | + description: 'Pong!', |
| 18 | + }, |
| 19 | + |
| 20 | + run: ({ interaction, client, handler }) => { |
| 21 | + interaction.reply(`:ping_pong: Pong! ${client.ws.ping}ms`); |
| 22 | + }, |
| 23 | + |
| 24 | + options: { |
| 25 | + devOnly: true, |
| 26 | + guildOnly: true, |
| 27 | + userPermissions: ['Administrator', 'AddReactions'], |
| 28 | + botPermissions: ['Administrator', 'AddReactions'], |
| 29 | + deleted: false, |
| 30 | + }, |
| 31 | + }; |
| 32 | + ``` |
| 33 | + </Tabs.Tab> |
| 34 | + <Tabs.Tab> |
| 35 | + ```js filename="commands/misc/ping.js" copy |
| 36 | + export const data = { |
| 37 | + name: 'ping', |
| 38 | + description: 'Pong!', |
| 39 | + }, |
| 40 | + |
| 41 | + export function run({ interaction, client, handler }) { |
| 42 | + interaction.reply(`:ping_pong: Pong! ${client.ws.ping}ms`); |
| 43 | + }, |
| 44 | + |
| 45 | + export const options = { |
| 46 | + devOnly: true, |
| 47 | + guildOnly: true, |
| 48 | + userPermissions: ['Administrator', 'AddReactions'], |
| 49 | + botPermissions: ['Administrator', 'AddReactions'], |
| 50 | + deleted: false, |
| 51 | + }, |
| 52 | + ``` |
| 53 | + </Tabs.Tab> |
| 54 | + <Tabs.Tab> |
| 55 | + ```ts filename="commands/misc/ping.ts" copy |
| 56 | + import type { CommandData, SlashCommandProps, CommandOptions } from 'commandkit'; |
| 57 | + |
| 58 | + export const data: CommandData = { |
| 59 | + name: 'ping', |
| 60 | + description: 'Pong!', |
| 61 | + }, |
| 62 | + |
| 63 | + export function run({ interaction, client, handler }: SlashCommandProps) { |
| 64 | + interaction.reply(`:ping_pong: Pong! ${client.ws.ping}ms`); |
| 65 | + }, |
| 66 | + |
| 67 | + export const options: CommandOptions = { |
| 68 | + devOnly: true, |
| 69 | + guildOnly: true, |
| 70 | + userPermissions: ['Administrator', 'AddReactions'], |
| 71 | + botPermissions: ['Administrator', 'AddReactions'], |
| 72 | + deleted: false, |
| 73 | + }, |
| 74 | + ``` |
| 75 | + </Tabs.Tab> |
| 76 | + |
| 77 | +</Tabs> |
| 78 | + |
| 79 | +### Context Menu Command |
| 80 | + |
| 81 | +Here's an example `content` command which replies with the content of the target message. |
| 82 | + |
| 83 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 84 | + <Tabs.Tab> |
| 85 | + ```js filename="commands/misc/content.js" copy |
| 86 | + const { CommandType } = require('commandkit'); |
| 87 | + |
| 88 | + module.exports = { |
| 89 | + data: { |
| 90 | + name: 'content', |
| 91 | + type: CommandType.Message, |
| 92 | + }, |
| 93 | + |
| 94 | + run: ({ interaction, client, handler }) => { |
| 95 | + interaction.reply(`The message is: ${interaction.targetMessage}`); |
| 96 | + }, |
| 97 | + |
| 98 | + options: { |
| 99 | + devOnly: true, |
| 100 | + guildOnly: true, |
| 101 | + userPermissions: ['Administrator', 'AddReactions'], |
| 102 | + botPermissions: ['Administrator', 'AddReactions'], |
| 103 | + deleted: false, |
| 104 | + }, |
| 105 | + }; |
| 106 | + ``` |
| 107 | + </Tabs.Tab> |
| 108 | + <Tabs.Tab> |
| 109 | + ```js filename="commands/misc/content.js" copy |
| 110 | + import { CommandType } from 'commandkit'; |
| 111 | + |
| 112 | + export const data = { |
| 113 | + name: 'content', |
| 114 | + type: CommandType.Message, |
| 115 | + }, |
| 116 | + |
| 117 | + export function run({ interaction, client, handler }) { |
| 118 | + interaction.reply(`The message is: ${interaction.targetMessage}`); |
| 119 | + }, |
| 120 | + |
| 121 | + export const options = { |
| 122 | + devOnly: true, |
| 123 | + guildOnly: true, |
| 124 | + userPermissions: ['Administrator', 'AddReactions'], |
| 125 | + botPermissions: ['Administrator', 'AddReactions'], |
| 126 | + deleted: false, |
| 127 | + }, |
| 128 | + ``` |
| 129 | + </Tabs.Tab> |
| 130 | + <Tabs.Tab> |
| 131 | + ```ts filename="commands/misc/content.ts" copy |
| 132 | + import { CommandType, type CommandData, type ContextMenuCommandProps, type CommandOptions } from 'commandkit'; |
| 133 | + |
| 134 | + export const data: CommandData = { |
| 135 | + name: 'content', |
| 136 | + type: CommandType.Message, |
| 137 | + }, |
| 138 | + |
| 139 | + export function run({ interaction, client, handler }: ContextMenuCommandProps) { |
| 140 | + interaction.reply(`The message is: ${interaction.targetMessage}`); |
| 141 | + }, |
| 142 | + |
| 143 | + export const options: CommandOptions = { |
| 144 | + devOnly: true, |
| 145 | + guildOnly: true, |
| 146 | + userPermissions: ['Administrator', 'AddReactions'], |
| 147 | + botPermissions: ['Administrator', 'AddReactions'], |
| 148 | + deleted: false, |
| 149 | + }, |
| 150 | + ``` |
| 151 | + </Tabs.Tab> |
| 152 | + |
| 153 | +</Tabs> |
| 154 | + |
| 155 | +## Properties and Methods |
| 156 | + |
| 157 | +### `data` |
| 158 | + |
| 159 | +- Type: [`CommandData`](/typedef/CommandData) | [`SlashCommandBuilder`](https://discord.js.org/docs/packages/builders/main/SlashCommandBuilder:Class) | [`ContextMenuCommandBuilder`](https://discord.js.org/docs/packages/builders/main/ContextMenuCommandBuilder:Class) |
| 160 | + |
| 161 | +This property contains the command's structural information, and is required to register and handle commands. |
| 162 | + |
| 163 | +### `run` |
| 164 | + |
| 165 | +- Type: `void` |
| 166 | + |
| 167 | +- Props Type: [`SlashCommandProps`](/typedef/SlashCommandProps) | [`ContextMenuCommandProps`](/typedef/ContextMenuCommandProps) |
| 168 | + |
| 169 | +This function will be called when the command is executed. |
| 170 | + |
| 171 | +### `options` (optional) |
| 172 | + |
| 173 | +- Type: [`CommandOptions`](/typedef/CommandOptions) |
| 174 | + |
| 175 | +This property contains the command's registration/handling behaviour. |
0 commit comments