|
1 | 1 | ---
|
2 |
| -title: Discord.js events |
| 2 | +title: Discord.js Events |
3 | 3 | ---
|
| 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