> For the complete documentation index, see [llms.txt](https://ayfri.gitbook.io/advanced-command-handler/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayfri.gitbook.io/advanced-command-handler/concepts/events.md).

# Events

Events in the directory `eventsDir` (set with `CommandHandler.create`) are automatically loaded, you can see an example of how to create an Event here :

{% code title="ExampleEvent.js" %}

```javascript
const {Event} = require('advanced-command-handler');

module.exports = class ExampleEvent extends Event {
    name = '';
    once = false;
    
    async run(ctx, ...eventsArguments) {
        // Your code goes here.
    }
}
```

{% endcode %}

The property `name` should be equal to the discord.js event you want to use, like `message`. The `eventsArguments` is a list of arguments that will be different with what events you use, see [Client Events](https://discord.js.org/#/docs/main/stable/class/Client).

And you can see a "complete" example of what a command could look like :

{% code title="GuildMemberAddEvent.js" %}

```javascript
const {Event, Logger} = require('advanced-command-handler');

module.exports = class GuildMemberAddEvent extends Event {
    name = 'ready';
    
    async run(ctx, member) {
        member.user.send(`Hi ! Welcome to our guild ${member.guild.name}, we now have **${member.guild.memberCount}** members !`);
        Logger.event(`A member has joined the guild '${member.guild.name}'.`, ctx.eventName);
    }
}
```

{% endcode %}

{% hint style="info" %}
Note that setting `once` to `true` is not needed, it will just use the `once` method for `EventEmitter` and the difference on performances is minimal.
{% endhint %}

## [Documentation](https://advanced-command-handler.github.io/docs/classes/Event.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ayfri.gitbook.io/advanced-command-handler/concepts/events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
