|
1 | 1 | # Events Setup |
| 2 | + |
| 3 | +import { Tabs } from 'nextra/components'; |
| 4 | + |
| 5 | +This is a simple overview of how to set up a simple function that is called when the "ready" event is triggered. All this code does is log to the console when the bot is ready. |
| 6 | + |
| 7 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 8 | + <Tabs.Tab> |
| 9 | + ```js filename="events/ready/console-log.js" copy |
| 10 | + module.exports = (c, client, handler) => { |
| 11 | + console.log(`${c.user.username} is ready!`); |
| 12 | + }; |
| 13 | + ``` |
| 14 | + </Tabs.Tab> |
| 15 | + |
| 16 | + <Tabs.Tab> |
| 17 | + ```js filename="events/ready/console-log.js" copy |
| 18 | + export default function (c, client, handler) { |
| 19 | + console.log(`${c.user.username} is ready!`); |
| 20 | + }; |
| 21 | + ``` |
| 22 | + </Tabs.Tab> |
| 23 | + |
| 24 | + <Tabs.Tab> |
| 25 | + ```ts filename="events/ready/console-log.ts" copy |
| 26 | + import type { Client } from 'discord.js'; |
| 27 | + import type { CommandKit } from 'commandkit'; |
| 28 | + |
| 29 | + export default function (c: Client<true>, client: Client<true>, handler: CommandKit) { |
| 30 | + console.log(`${c.user.username} is ready!`); |
| 31 | + }; |
| 32 | + ``` |
| 33 | + </Tabs.Tab> |
| 34 | + |
| 35 | +</Tabs> |
| 36 | + |
| 37 | +## Parameters explained |
| 38 | + |
| 39 | +The parameters might look a bit confusing at first, but they're actually quite simple. The first parameter `c` is the client object that was returned as a parameter when the "ready" event was triggered. The second parameter `client` is the Discord.js client that was instantiated in your main entry point file. Finally, the `handler` parameter is the current CommandKit instance. |
| 40 | + |
| 41 | +To better understand how the parameters work, here's another example but with the "messageCreate" event listener. |
| 42 | + |
| 43 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 44 | + <Tabs.Tab label="CommonJS"> |
| 45 | + ```js filename="events/messageCreate/say-hi.js" copy |
| 46 | + module.exports = (message, client) => { |
| 47 | + if (message.content === 'hey') { |
| 48 | + message.reply('Hi!'); |
| 49 | + } |
| 50 | + }; |
| 51 | + ``` |
| 52 | + </Tabs.Tab> |
| 53 | + |
| 54 | + <Tabs.Tab label="ESM"> |
| 55 | + ```js filename="events/messageCreate/say-hi.js" copy |
| 56 | + export default function (message, client, handler) { |
| 57 | + if (message.content === 'hey') { |
| 58 | + message.reply('Hi!'); |
| 59 | + } |
| 60 | + }; |
| 61 | + ``` |
| 62 | + </Tabs.Tab> |
| 63 | + |
| 64 | + <Tabs.Tab label="TypeScript"> |
| 65 | + ```ts filename="events/messageCreate/say-hi.ts" copy |
| 66 | + import type { Message, Client } from 'discord.js'; |
| 67 | + import type { CommandKit } from 'commandkit'; |
| 68 | + |
| 69 | + export default function (message: Message<true>, client: Client<true>, handler: CommandKit) { |
| 70 | + if (message.content === 'hey') { |
| 71 | + message.reply('Hi!'); |
| 72 | + } |
| 73 | + }; |
| 74 | + ``` |
| 75 | + </Tabs.Tab> |
| 76 | + |
| 77 | +</Tabs> |
| 78 | + |
| 79 | +In this example you can see that the first parameter is the `message` object that was returned as a parameter when the "messageCreate" event was triggered. The `client` parameter is the same as the previous example. |
| 80 | + |
| 81 | +## Multiple parameters explained |
| 82 | + |
| 83 | +But what if an event returns multiple parameters? Let's take for example the "messageUpdate" event. This event returns two parameters, `oldMessage` and `newMessage`. The parameters will follow the same behaviour as if you were using the `client.on()` method. |
| 84 | + |
| 85 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 86 | + <Tabs.Tab label="CommonJS"> |
| 87 | + ```js filename="events/messageUpdate/log-message-update.js" copy |
| 88 | + module.exports = (oldMessage, newMessage, client) => { |
| 89 | + console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`); |
| 90 | + }; |
| 91 | + ``` |
| 92 | + </Tabs.Tab> |
| 93 | + |
| 94 | + <Tabs.Tab label="ESM"> |
| 95 | + ```js filename="events/messageUpdate/log-message-update.js" copy |
| 96 | + export default function (oldMessage, newMessage, client) { |
| 97 | + console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`); |
| 98 | + }; |
| 99 | + ``` |
| 100 | + </Tabs.Tab> |
| 101 | + |
| 102 | + <Tabs.Tab label="TypeScript"> |
| 103 | + ```ts filename="events/messageUpdate/log-message-update.ts" copy |
| 104 | + import type { Message, PartialMessage } from 'discord.js'; |
| 105 | + import type { CommandKit } from 'commandkit'; |
| 106 | + |
| 107 | + export default function (oldMessage: Message<boolean> | PartialMessage, newMessage: Message<boolean> | PartialMessage, client: Client<true>, handler: CommandKit) { |
| 108 | + console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`); |
| 109 | + }; |
| 110 | + ``` |
| 111 | + </Tabs.Tab> |
| 112 | + |
| 113 | +</Tabs> |
| 114 | + |
| 115 | +As you can see, even with multiple parameters, the last parameter will always be the `client` object. |
| 116 | + |
| 117 | +## Stopping the event loop |
| 118 | + |
| 119 | +The code above is just a simple example of how to set up an event function. But what if you want to stop the next event function in line from running? This is where the `return` statement comes in handy. |
| 120 | + |
| 121 | +<Tabs items={['CommonJS', 'ESM', 'TypeScript']}> |
| 122 | + <Tabs.Tab label="CommonJS"> |
| 123 | + ```js filename="events/messageCreate/say-hi.js" copy |
| 124 | + module.exports = (message, client) => { |
| 125 | + if (message.content === 'hey') { |
| 126 | + message.reply('Hi!') |
| 127 | + |
| 128 | + return true // This stops the event loop |
| 129 | + } |
| 130 | + } |
| 131 | + ``` |
| 132 | + </Tabs.Tab> |
| 133 | + |
| 134 | + <Tabs.Tab label="ESM"> |
| 135 | + ```js filename="events/messageCreate/say-hi.js" copy |
| 136 | + export default function (message, client) { |
| 137 | + if (message.content === 'hey') { |
| 138 | + message.reply('Hi!') |
| 139 | + |
| 140 | + return true // This stops the event loop |
| 141 | + } |
| 142 | + } |
| 143 | + ``` |
| 144 | + </Tabs.Tab> |
| 145 | + |
| 146 | + <Tabs.Tab label="TypeScript"> |
| 147 | + ```js filename="events/messageCreate/say-hi.ts" copy |
| 148 | + import type { Message, Client } from 'discord.js'; |
| 149 | + import type { CommandKit } from 'commandkit'; |
| 150 | + |
| 151 | + export default function (message: Message<true>, client: Client<true>, handler: CommandKit) { |
| 152 | + if (message.content === 'hey') { |
| 153 | + message.reply('Hi!') |
| 154 | + |
| 155 | + return true // This stops the event loop |
| 156 | + } |
| 157 | + } |
| 158 | + ``` |
| 159 | + </Tabs.Tab> |
| 160 | + |
| 161 | +</Tabs> |
| 162 | + |
| 163 | +You can return any truthy value from this function to stop the next event function from running. |
0 commit comments