Skip to content

Commit 7fa4a77

Browse files
committed
document prefix resolver for message commands
1 parent 2c4928c commit 7fa4a77

File tree

8 files changed

+61
-17
lines changed

8 files changed

+61
-17
lines changed

apps/website/docs/guide/02-commands/04-message-commands.mdx

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ message commands in favor of the more modern
1818

1919
:::
2020

21+
## Basic Setup
22+
2123
Message commands, like all the other command types, live in your
2224
`src/app/commands` directory. To let CommandKit know that this is a
23-
message command, just export the `message` function.
25+
message command, just export the `message` function from your command file.
2426

2527
```ts title="src/app/commands/ping.ts"
2628
import type { CommandData, MessageCommand } from 'commandkit';
@@ -34,14 +36,6 @@ export const message: MessageCommand = async (ctx) => {
3436
};
3537
```
3638

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-
4539
## Message command options (arguments)
4640

4741
Since a message command - on a technical level - is just a string, the
@@ -63,5 +57,56 @@ export const message: MessageCommand = async (ctx) => {
6357
```
6458

6559
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']`.
60+
`!ping john jack jane`, the value of `args` will equal
61+
`['john', 'jack', 'jane']`.
62+
63+
## Custom Prefixes
64+
65+
The simplest way to set a custom prefix is using the
66+
`setPrefixResolver` method on the `commandkit` instance:
67+
68+
```ts title="src/app.ts"
69+
import { commandkit } from 'commandkit';
70+
71+
commandkit.setPrefixResolver(async (message) => {
72+
return '?';
73+
});
74+
```
75+
76+
This sets a global prefix of `?` for all message commands. Your bot
77+
will now respond to commands like `?help` or `?ping`.
78+
79+
:::tip
80+
81+
You can return an array of strings to support multiple prefixes
82+
simultaneously:
83+
84+
```ts
85+
return ['?', '!', '>'];
86+
```
87+
88+
:::
89+
90+
### Guild-Specific Prefixes
91+
92+
For a more dynamic approach, you might want different prefixes for
93+
different guilds. Here's how to implement that:
94+
95+
```ts title="src/app.ts"
96+
import { commandkit } from 'commandkit';
97+
import { database } from '../database'; // This is a mock database
98+
99+
commandkit.setPrefixResolver(async (message) => {
100+
const guildSettings = await database.findUnique({
101+
where: { guildId },
102+
});
103+
104+
return guildSettings?.prefix ?? '!'; // Fallback to '!' if no prefix is found
105+
});
106+
```
107+
108+
:::warning
109+
110+
The `setPrefixResolver` method runs on every message, so it's important to make sure that it's as lightweight as possible. To help with performance, you may want to implement caching, which CommandKit also supports with the [`@commandkit/cache`](../05-official-plugins/03-commandkit-cache.mdx) official plugin.
111+
112+
:::
File renamed without changes.

apps/website/docs/guide/02-commands/05-custom-message-commands-prefix.mdx

Lines changed: 0 additions & 3 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

apps/website/docusaurus.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ const config: Config = {
3737
lastVersion: 'current',
3838
versions: {
3939
current: {
40-
label: '1.2',
40+
label: 'v1',
4141
// path: 'v1',
42+
badge: false,
4243
},
4344
'0.1.10': {
44-
label: '0.1.10',
45+
label: 'v0.1.10',
4546
path: 'v0',
47+
banner: 'unmaintained'
4648
},
4749
},
4850
sidebarPath: './sidebars.ts',
@@ -140,7 +142,7 @@ const config: Config = {
140142
position: 'right',
141143
versions: {
142144
current: { label: 'v1' },
143-
'0.1.10': { label: 'v0' },
145+
'0.1.10': { label: 'v0' },
144146
},
145147
},
146148
{

0 commit comments

Comments
 (0)