Skip to content

Commit a8f7488

Browse files
committed
document discord.js events
1 parent 879ed6b commit a8f7488

File tree

3 files changed

+158
-5
lines changed

3 files changed

+158
-5
lines changed

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

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

1919
:::
2020

21-
## Basic Setup
21+
## Basic setup
2222

2323
Message commands, like all the other command types, live in your
2424
`src/app/commands` directory. To let CommandKit know that this is a
@@ -61,7 +61,7 @@ With the above command as an example, if a user on Discord runs
6161
`!ping john jack jane`, the value of `args` will equal
6262
`['john', 'jack', 'jane']`.
6363

64-
## Custom Prefixes
64+
## Custom prefixes
6565

6666
The simplest way to set a custom prefix is using the
6767
`setPrefixResolver` method on the `commandkit` instance:
@@ -88,7 +88,7 @@ return ['?', '!', '>'];
8888

8989
:::
9090

91-
### Guild-Specific Prefixes
91+
### Guild-specific prefixes
9292

9393
For a more dynamic approach, you might want different prefixes for
9494
different guilds. Here's how to implement that:

apps/website/docs/guide/02-commands/06-category-directory.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ category directories to create a hierarchy of commands. This can then
1212
be used across your application in places such as middlewares,
1313
plugins, or even other commands through your `commandkit` instance.
1414

15-
## Example Structure
15+
## Example structure
1616

1717
```title="" {4-15}
1818
.
Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,156 @@
11
---
2-
title: Discord.js events
2+
title: Discord.js Events
33
---
4+
5+
Events allow your Discord bot to react to various actions happening on
6+
Discord, such as when users send messages, join guilds, or when your
7+
bot comes online.
8+
9+
CommandKit provides a simple way to organize and handle Discord.js
10+
events using the `events` directory inside your `src/app` directory.
11+
This approach makes it easy to manage multiple events and keep your
12+
code organized.
13+
14+
## Basic event directory structure
15+
16+
Each Discord.js event gets its own directory within `src/app/events`,
17+
and the directory name must match the exact Discord.js event name.
18+
Inside each event directory, you can create multiple handler files
19+
that will all execute when that event is triggered.
20+
21+
Here's how your project structure might look with the
22+
`"messageCreate"` and `"ready"` events:
23+
24+
```title="" {4-9}
25+
.
26+
├── src/
27+
│ ├── app/
28+
│ │ └── events/
29+
│ │ ├── messageCreate/
30+
│ │ │ ├── give-xp.ts
31+
│ │ │ └── log-message.ts
32+
│ │ └── ready/
33+
│ │ └── log.ts
34+
│ └── app.ts
35+
├── .env
36+
├── .gitignore
37+
├── commandkit.config.ts
38+
├── package.json
39+
└── tsconfig.json
40+
```
41+
42+
:::tip
43+
44+
You can see what events are available in Discord.js by checking the
45+
[Discord.js documentation](https://discord.js.org/docs/packages/discord.js/main/ClientEventTypes:Interface).
46+
47+
:::
48+
49+
## Creating an event handler
50+
51+
To create an event handler, create a folder inside the
52+
`src/app/events` directory which should match the event name from
53+
Discord.js. This example will use the `"ready"` event.
54+
55+
```ts title="src/app/events/ready/log.ts"
56+
import type { Client } from 'commandkit';
57+
58+
export default function (client: Client<true>) {
59+
console.log(`🤖 ${client.user.displayName} is online!`);
60+
}
61+
```
62+
63+
That's it! CommandKit will automatically detect this file and register
64+
it as one of the handler functions for the `"ready"` event.
65+
66+
### Event parameters
67+
68+
Just like the Discord.js `client.on()` method, the parameters you
69+
receive in your event file will be based on what event you're trying
70+
to handle. In the example above, the `"ready"` event should give you a
71+
`Client` instance.
72+
73+
If you were to, for example, handle the `"messageCreate"` event, you
74+
would get a `Message` instance.
75+
76+
### Events with multiple parameters
77+
78+
Some Discord.js events have multiple parameters, such as the
79+
`"messageUpdate"` event.
80+
81+
```ts title="src/app/events/messageUpdate/log-message-update.ts"
82+
import type { Message } from 'discord.js';
83+
84+
export default function (oldMessage: Message, newMessage: Message) {
85+
console.log(
86+
`Message from ${oldMessage.author.username} updated: ${oldMessage.content} -> ${newMessage.content}`,
87+
);
88+
}
89+
```
90+
91+
### Additional parameters
92+
93+
Outside of the parameters provided by Discord.js, CommandKit will pass
94+
additional parameters to the event handler, including a `Client` and
95+
`CommandKit` instance.
96+
97+
```ts title="src/app/events/messageCreate/log.ts"
98+
import type { CommandKit } from 'commandkit';
99+
import type { Message } from 'discord.js';
100+
101+
export default function (
102+
message: Message,
103+
client: Client<true>,
104+
commandkit: CommandKit,
105+
) {
106+
console.log(
107+
`Message from ${message.author.username}: ${message.content}`,
108+
);
109+
}
110+
```
111+
112+
## Multiple handlers for the same event
113+
114+
One of the powerful features of CommandKit's event system is the
115+
ability to have multiple handlers for the same event. Each handler
116+
file you create in an event directory will be executed when that event
117+
is called.
118+
119+
For example, you might want to do several things when a message is
120+
created:
121+
122+
```ts title="src/app/events/messageCreate/give-xp.ts"
123+
import type { MessageCreateEvent } from 'commandkit';
124+
125+
export default function ({ message }: MessageCreateEvent) {
126+
// Don't give XP to bots
127+
if (message.author.bot) return;
128+
129+
// Give XP to the user
130+
console.log(`Giving XP to ${message.author.username}`);
131+
}
132+
```
133+
134+
```ts title="src/app/events/messageCreate/log-message.ts"
135+
import type { MessageCreateEvent } from 'commandkit';
136+
137+
export default function ({ message }: MessageCreateEvent) {
138+
console.log(
139+
`Message from ${message.author.username}: ${message.content}`,
140+
);
141+
}
142+
```
143+
144+
Both handler functions will be called whenever a message is sent in
145+
Discord.
146+
147+
:::tip
148+
149+
Event handler files can be named anything you want - CommandKit
150+
identifies them by their location in the event directory, not by their
151+
filename. However, since each file is treated as a handler function,
152+
they're executed one-by-one in the order of their file name.
153+
154+
Example: `01-give-xp.ts` will be executed before `02-log-message.ts`.
155+
156+
:::

0 commit comments

Comments
 (0)