Skip to content

Commit cfb3f9f

Browse files
committed
docs: update content
1 parent 19e16e9 commit cfb3f9f

File tree

4 files changed

+123
-73
lines changed

4 files changed

+123
-73
lines changed

apps/website/docs/guide/01-getting-started/02-setup-commandkit.mdx

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,41 @@ description: Setup a new CommandKit project manually, or using the create-comman
55

66
You can quickly setup a new CommandKit project using `create-commandkit` — a command-line utility used for creating new discord.js applications with CommandKit. To get started, run the following command:
77

8-
```bash npm2yarn
8+
```sh npm2yarn
99
npx create-commandkit@latest
1010
```
1111

1212
This will start the CLI in the current directory which will help you quickly setup a base CommandKit project.
1313

14-
## Development version
14+
## Project structure
15+
16+
By using the CLI to create a base project, you should get a project structure that looks like this:
1517

16-
If you'd like to try the latest development builds, you can use the `@dev` tag when setting up a CommandKit project using the CLI.
18+
```
19+
.
20+
├── src/
21+
│ ├── app/
22+
│ │ ├── commands/
23+
│ │ │ └── ping.ts
24+
│ │ └── events/
25+
│ │ └── ready/
26+
│ │ └── log.ts
27+
│ └── app.ts
28+
├── .env
29+
├── .gitignore
30+
├── commandkit.config.ts
31+
├── package.json
32+
└── tsconfig.json
33+
```
34+
35+
## Development version
1736

1837
:::warning
1938
The development version is likely to have bugs.
2039
:::
2140

22-
```bash npm2yarn
41+
If you'd like to try the latest development builds, you can use the `@dev` tag like so:
42+
43+
```sh npm2yarn
2344
npx create-commandkit@dev
2445
```

apps/website/docs/guide/01-getting-started/03-setup-commandkit-manually.mdx

+50-69
Original file line numberDiff line numberDiff line change
@@ -6,128 +6,109 @@ description: Learn how to install CommandKit.
66
import Tabs from '@theme/Tabs';
77
import TabItem from '@theme/TabItem';
88

9-
First of all, create a `commandkit.config.ts` at the root of your project. This file is used to configure CommandKit and its plugins.
9+
If you don't want to use the CLI to automatically generate a base CommandKit project, you can integrate CommandKit manually into your application.
1010

11-
```ts
11+
:::info
12+
While this guide primarily uses TypeScript, you can alternatively use JavaScript files to follow along if that's what you're comfortable with (e.g. `commandkit.config.js`, `app.js`, etc)
13+
:::
14+
15+
## Configuration file
16+
17+
To get started, create a file called `commandkit.config.ts` at the root of your project. This file is used to configure CommandKit and its plugins.
18+
19+
```ts title="commandkit.config.ts"
1220
import { defineConfig } from 'commandkit';
1321

1422
export default defineConfig({});
1523
```
1624

17-
Then, create a `src` folder and add `app.ts` file inside it.
25+
## Entrypoint file
1826

19-
:::info
20-
You can also use `.js` files instead of `.ts`. CommandKit supports both JavaScript and TypeScript out of the box.
21-
:::
27+
Then, create a folder called `src`, followed by a file called `app.ts` inside it. The file should look like this:
2228

23-
The `app.ts` file is the entrypoint of your bot. This file exports your discord.js client.
24-
25-
```ts
29+
```ts title="src/app.ts"
2630
import { Client } from 'discord.js';
2731

2832
const client = new Client({
29-
intents: [...],
33+
intents: ['Guilds', 'GuildMembers'],
3034
});
3135

32-
// must export the client here
36+
// must export the `client` here
3337
export default client;
3438
```
3539

36-
You don't need to call `client.login()` as commandkit will do that for you. You should store your bot token in `.env` file under `DISCORD_TOKEN` or `TOKEN` variable.
40+
With the current entrypoint file created, it's important to understand that:
41+
42+
1. The `app.ts` file is the entrypoint for this application. This file **must** export your discord.js client instance.
43+
2. You don't have to call `client.login()` as CommandKit will handle that for you.
44+
3. You should store your bot token as an environment variable with either `DISCORD_TOKEN` or `TOKEN` as the name.
3745

38-
Now, lets create a simple command. Create a `src/app/commands` folder and add `ping.ts` file inside it.
46+
## Adding commands
3947

40-
```ts
48+
To add a command, create a folder inside the `src/app` directory called `commands` and create your command file (e.g. `ping.ts`). This example will use a simple ping/pong command which will register as a chat input (slash) and message (legacy) command.
49+
50+
```ts title="src/app/commands/ping.ts"
4151
import type { SlashCommand, CommandData, MessageCommand } from 'commandkit';
4252

4353
export const command: CommandData = {
4454
name: 'ping',
45-
description: 'Ping pong!',
55+
description: 'Pong!',
4656
};
4757

48-
export const chatInput: SlashCommand = async (ctx) => {
49-
await ctx.interaction.reply('Pong!');
58+
export const chatInput: SlashCommand = async ({ interaction }) => {
59+
await interaction.reply('Pong!');
5060
};
5161

52-
// you can also add a message command handler
53-
export const message: MessageCommand = async (ctx) => {
54-
await ctx.message.reply('Pong!');
62+
export const message: MessageCommand = async ({ message }) => {
63+
await message.reply('Pong!');
5564
};
5665
```
5766

58-
This command will reply with `Pong!` when the user types `/ping` or `!ping` in the chat.
59-
60-
:::info
61-
You can override the default prefix for message commands using:
62-
63-
```ts
64-
import { commandkit } from 'commandkit';
65-
66-
commandkit.setPrefixResolver(async (message) => {
67-
// you can use `message` to fetch guild specific prefixes if needed
68-
return '?';
69-
});
70-
```
71-
72-
An example of this could be:
67+
This command will reply with "Pong!" when the user runs the `/ping` slash command, or sends `!ping` in the chat.
7368

74-
```ts
75-
import { commandkit, cacheTag } from 'commandkit';
76-
import { database } from '../database.ts';
77-
78-
// this function's result will be automatically cached
79-
async function fetchGuildPrefix(guildId: string) {
80-
'use cache';
81-
82-
cacheTag(`prefix:${guildId}`);
83-
84-
const setting = await database.findUnique({
85-
where: { guildId },
86-
select: { prefix: true },
87-
});
88-
89-
// if no prefix is found, return the default prefix
90-
return setting?.prefix ?? '!';
91-
}
92-
93-
commandkit.setPrefixResolver((message) => {
94-
return fetchGuildPrefix(message.guildId);
95-
});
96-
```
97-
98-
You can also return array of prefixes.
69+
:::tip
70+
Prefixes for your message (legacy) commands can be changed. Learn more [here](/docs/next/guide/resolve-message-commands-prefix).
9971
:::
10072

101-
Now, lets run the bot. You can use `commandkit dev` to start the bot in development mode. This will automatically reload the bot when you make changes to your code.
73+
## Running the app in development
10274

103-
```sh
75+
To run your application in development mode, you can use the `commandkit dev` command in your terminal. This will automatically reload the bot when you make changes to your code.
76+
77+
```sh npm2yarn
10478
npx commandkit dev
10579
```
10680

107-
When running in development mode, CommandKit will generate a `.commandkit` folder in your project directory. This folder contains the compiled files used in development mode. You should not commit this folder to your version control system. You should add it to your `.gitignore` file.
81+
:::warning
82+
When running in development mode, CommandKit will generate a `.commandkit` folder in the root of your project. This folder contains the compiled files used in development mode. You should not commit this folder to your version control system by making sure you add it to your `.gitignore` file.
83+
:::
10884

109-
## Production build
85+
## Building for production
11086

11187
When you are ready to deploy your bot, you can use `commandkit build` to create a production build of your bot. This will create a `dist` folder in your project directory containing the compiled files.
11288

11389
```sh
11490
npx commandkit build
11591
```
11692

117-
## Starting the bot in production mode
93+
:::warning
94+
After running your build script, CommandKit will generate a `.dist` folder in the root of your project. This folder contains the compiled files used for production. You should not commit this folder to your version control system by making sure you add it to your `.gitignore` file.
95+
:::
96+
97+
## Running the app in production
11898

119-
You can use `commandkit start` to start the bot in production mode. This will load the environment variables from `.env` file in your project directory.
99+
You can use `commandkit start` to start the bot in production mode. This will load the environment variables from the `.env` file in the root of your project directory.
120100

121101
```sh
122102
npx commandkit start
123103
```
124104

125-
If you want to manually start the bot in production mode, you can use the following command:
105+
:::info
106+
If you want to manually start the bot in production mode, you can alternatively use the following command:
126107

127108
```sh
128109
node dist/index.js
129110
```
130111

131-
:::info
132-
The `index.js` file is autogenerated by `CommandKit`.
112+
The `dist/index.js` file is generated by CommandKit.
113+
133114
:::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Custom command prefixes
3+
description: Implement custom prefixes for legacy/message commands
4+
---
5+
6+
The default prefix for message (legacy) commands can be overridden using:
7+
8+
```ts title="src/app.ts"
9+
import { commandkit } from 'commandkit';
10+
11+
commandkit.setPrefixResolver(async (message) => {
12+
// you can use `message` to fetch guild specific prefixes if needed
13+
return '?';
14+
});
15+
```
16+
17+
:::tip
18+
You can also return array of strings to support multiple custom prefixes.
19+
:::
20+
21+
## With caching
22+
23+
An example of this using CommandKit's caching could look like:
24+
25+
```ts title="src/app.ts"
26+
import { commandkit, cacheTag } from 'commandkit';
27+
import { database } from '../database.ts';
28+
29+
// this function's result will be automatically cached
30+
async function fetchGuildPrefix(guildId: string) {
31+
'use cache';
32+
33+
cacheTag(`prefix:${guildId}`);
34+
35+
const setting = await database.findUnique({
36+
where: { guildId },
37+
select: { prefix: true },
38+
});
39+
40+
// if no prefix is found, return the default prefix
41+
return setting?.prefix ?? '!';
42+
}
43+
44+
commandkit.setPrefixResolver((message) => {
45+
return fetchGuildPrefix(message.guildId);
46+
});
47+
```

apps/website/sidebars.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const sidebars: SidebarsConfig = {
2525
'guide/using-cli',
2626
'guide/commandkit-config',
2727
'guide/caching',
28+
'guide/resolve-message-commands-prefix',
2829
],
2930
};
3031

0 commit comments

Comments
 (0)