|
1 | 1 | --- |
2 | 2 | title: Message commands |
3 | 3 | --- |
| 4 | + |
| 5 | +Message commands, which are generally considered 'Legacy', are a way |
| 6 | +to run commands on Discord bots using regular messages. Before the |
| 7 | +introduction of |
| 8 | +[Slash/Chat Input Commands](./01-chat-input-commands.mdx), this was |
| 9 | +the primary way commands were handled. They were usually triggered by |
| 10 | +using a prefix set by the developer of the bot (for example: `!ping`). |
| 11 | + |
| 12 | +:::warning |
| 13 | + |
| 14 | +While CommandKit allows developers to create and handle message |
| 15 | +commands, it's important to note that Discord generally discourages |
| 16 | +message commands in favor of the more modern |
| 17 | +[Chat Input Commands](./01-chat-input-commands.mdx). |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +Message commands, like all the other command types, live in your |
| 22 | +`src/app/commands` directory. To let CommandKit know that this is a |
| 23 | +message command, just export the `messageCommand` function. |
| 24 | + |
| 25 | +```ts title="src/app/commands/ping.ts" |
| 26 | +import type { CommandData, MessageCommand } from 'commandkit'; |
| 27 | + |
| 28 | +export const command: CommandData = { |
| 29 | + name: 'ping', |
| 30 | +}; |
| 31 | + |
| 32 | +export const messageCommand: MessageCommand = async (ctx) => { |
| 33 | + await ctx.message.reply('Pong!'); |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +:::tip |
| 38 | + |
| 39 | +By default, your bot's prefix will be `!`. However, with CommandKit |
| 40 | +you can set custom prefixes, or even guild-based prefixes. |
| 41 | +[Read more](./05-custom-message-commands-prefix.mdx) |
| 42 | + |
| 43 | +::: |
| 44 | + |
| 45 | +## Message command options (arguments) |
| 46 | + |
| 47 | +Since a message command - on a technical level - is just a string, the |
| 48 | +idea of message command options will be different compared to |
| 49 | +[chat input command](./01-chat-input-commands.mdx) options. Instead of |
| 50 | +getting parsed data, you'll essentially be dealing with string |
| 51 | +'arguments'. |
| 52 | + |
| 53 | +```ts title="src/app/commands/ping.ts" |
| 54 | +import type { CommandData, MessageCommand } from 'commandkit'; |
| 55 | + |
| 56 | +export const command: CommandData = { |
| 57 | + name: 'ping', |
| 58 | +}; |
| 59 | + |
| 60 | +export const messageCommand: MessageCommand = async (ctx) => { |
| 61 | + const args = ctx.message.args(); // string[] |
| 62 | +}; |
| 63 | +``` |
| 64 | + |
| 65 | +With the above command as an example, if a user on Discord runs |
| 66 | +`!ping arg1 arg2 arg3`, the value of `args` will equal |
| 67 | +`['arg1', 'arg2', 'arg3']`. |
0 commit comments