mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
style: format files
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
# Creating Events!
|
||||
|
||||
Woah! You are almost half way done with understanding all of Discordeno. Amazing isn't it? Something you may have noticed in the last section was when the bot started up you could see a bunch of messages like `Loaded X Commands` and such. Let's jump into this part now and understand how event handling works in Discordeno.
|
||||
Woah! You are almost half way done with understanding all of Discordeno. Amazing
|
||||
isn't it? Something you may have noticed in the last section was when the bot
|
||||
started up you could see a bunch of messages like `Loaded X Commands` and such.
|
||||
Let's jump into this part now and understand how event handling works in
|
||||
Discordeno.
|
||||
|
||||
## What Is An Event?
|
||||
|
||||
An event in Discordeno is a function that can be called when a specific event occurs. For the most part, events will usually be the ones that are available from Discordeno. However, you can create your own custom events as well if you wish.
|
||||
An event in Discordeno is a function that can be called when a specific event
|
||||
occurs. For the most part, events will usually be the ones that are available
|
||||
from Discordeno. However, you can create your own custom events as well if you
|
||||
wish.
|
||||
|
||||
## Understanding The Events
|
||||
|
||||
Go ahead and open up the `src/events/ready.ts` file. When you open this file, you will see the code that is triggered on the `ready` event. Whenever the bot completely starts up, Discordeno emits the `ready` event. This is when this code will be run allowing you to log these messages.
|
||||
Go ahead and open up the `src/events/ready.ts` file. When you open this file,
|
||||
you will see the code that is triggered on the `ready` event. Whenever the bot
|
||||
completely starts up, Discordeno emits the `ready` event. This is when this code
|
||||
will be run allowing you to log these messages.
|
||||
|
||||
```ts
|
||||
import { botCache, cache } from "../../deps.ts";
|
||||
@@ -27,13 +37,18 @@ botCache.eventHandlers.ready = function () {
|
||||
};
|
||||
```
|
||||
|
||||
> **Note:** Some of the code from the ready.ts file was removed here to make it easier to understand.
|
||||
> **Note:** Some of the code from the ready.ts file was removed here to make it
|
||||
> easier to understand.
|
||||
|
||||
Overall, this code is pretty self-explanatory. When the bot is ready, it logs all these things to the console for you.
|
||||
Overall, this code is pretty self-explanatory. When the bot is ready, it logs
|
||||
all these things to the console for you.
|
||||
|
||||
## Creating A Custom Event
|
||||
|
||||
Make a new file in the events folder called `discordLog.ts` that will send a message to a discord channel whenever we get an error so we don't need to always be watching the console to see errors. Once you made the file, go ahead and paste the base event snippet below.
|
||||
Make a new file in the events folder called `discordLog.ts` that will send a
|
||||
message to a discord channel whenever we get an error so we don't need to always
|
||||
be watching the console to see errors. Once you made the file, go ahead and
|
||||
paste the base event snippet below.
|
||||
|
||||
```ts
|
||||
import { botCache } from "../../deps.ts";
|
||||
@@ -44,7 +59,8 @@ botCache.eventHandlers.eventname = function () {
|
||||
```
|
||||
|
||||
- Change the event name to `discordLog`
|
||||
- Go to `src/types/events.ts` and add in the following code so it looks like this:
|
||||
- Go to `src/types/events.ts` and add in the following code so it looks like
|
||||
this:
|
||||
|
||||
```ts
|
||||
// This interface is a placeholder that allows you to easily add on custom events for your need.
|
||||
@@ -55,7 +71,7 @@ export interface CustomEvents extends EventHandlers {
|
||||
|
||||
Awesome, now we can get started on adding the code.
|
||||
|
||||
```ts
|
||||
````ts
|
||||
import { botCache, cache, sendMessage } from "../../deps.ts";
|
||||
import { Embed } from "../utils/Embed.ts";
|
||||
import { configs } from "../../configs.ts";
|
||||
@@ -66,7 +82,7 @@ botCache.eventHandlers.discordLog = function (error) {
|
||||
.setDescription([
|
||||
"```ts",
|
||||
error,
|
||||
"```"
|
||||
"```",
|
||||
].join("\n"))
|
||||
.setTimestamp();
|
||||
|
||||
@@ -76,12 +92,17 @@ botCache.eventHandlers.discordLog = function (error) {
|
||||
// Send the message
|
||||
return sendEmbed(errorChannel, embed);
|
||||
};
|
||||
```
|
||||
````
|
||||
|
||||
Now that we have fully covered events, it would be a good time to get some practice here. Feel free to make more events that you would like in your bot. The following is a list of all the events available to you by the library at the time of writing this guide. There may be more or some may have been removed. I'll try to keep this updated but either way, VSC will let you know through autocompletion what is and isn't available.
|
||||
Now that we have fully covered events, it would be a good time to get some
|
||||
practice here. Feel free to make more events that you would like in your bot.
|
||||
The following is a list of all the events available to you by the library at the
|
||||
time of writing this guide. There may be more or some may have been removed.
|
||||
I'll try to keep this updated but either way, VSC will let you know through
|
||||
autocompletion what is and isn't available.
|
||||
|
||||
```ts
|
||||
botUpdate?: (user: UserPayload) => unknown;
|
||||
botUpdate?: (user: UserPayload) => unknown;
|
||||
channelCreate?: (channel: Channel) => unknown;
|
||||
channelUpdate?: (channel: Channel, cachedChannel: Channel) => unknown;
|
||||
channelDelete?: (channel: Channel) => unknown;
|
||||
|
||||
Reference in New Issue
Block a user