# SLCommands Events

SLCommands comes with some custom events.

The `databaseConnected`, `commandException` and `handlerReady` events are enabled by default.&#x20;

But there are one more event: `commandDevsOnly` that is only enabled when you set the `useDefaultMessages` property to `false` when creating the handler. It let you handle command checks the way you want.

## handlerReady

This event is emitted once when the handler set up is done.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const { Event } = require('sl-commands')

/** ctx is a object containing the client and the handler instances */
new Event(
  'handlerReady', 
  (ctx) => {
    console.log(`The handler is ready!`)
  }
)
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import { Event } from 'sl-commands'

/** ctx is a object containing the client and the handler instances */
new Event(
  'handlerReady', 
  (ctx) => {
    console.log(`The handler is ready!`)
  }
)
```

{% endtab %}
{% endtabs %}

## databaseConnected

This event is emitted whenever the database is sucessfully connected via SLCommands.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const { Event } = require('sl-commands')

/** ctx is a object containing the client and the handler instances */
new Event(
  'databaseConnected', 
  (ctx, connection, state) => {
    console.log(`The connection state is "${state}".`)
  }
)
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import { Event } from 'sl-commands'

/** ctx is a object containing the client and the handler instances */
new Event(
  'databaseConnected', 
  (ctx, connection, state) => {
    console.log(`The connection state is "${state}".`)
  }
)
```

{% endtab %}
{% endtabs %}

## commandException

This is emitted whenever the handler detects any exception with the commands.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const { Event } = require('sl-commands')

/** ctx is a object containing the client and the handler instances */

/** interaction parameter can be undefined */
new Event(
  'commandException',
  (ctx, commandName, error, interaction) => {
    console.log(`Error in command ${commandName}.`, error)
  
    if (interaction) {
      interaction.reply({ content: 'Oops! An error occurred.' })
    }
  }
)
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import { Event } from 'sl-commands'

/** ctx is a object containing the client and the handler instances */

/** interaction parameter can be undefined */
new Event(
  'commandException',
  (ctx, commandName, error, interaction) => {
    console.log(`Error in command ${commandName}.`, error)
  
    if (interaction) {
      interaction.reply({ content: 'Oops! An error occurred.' })
    }
  }
)
```

{% endtab %}
{% endtabs %}

## commandDevsOnly

This is emitted whenever some unauthorized user tries to use a devsOnly command.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const { Event } = require('sl-commands')

/** ctx is a object containing the client and the handler instances */
new Event(
  'commandDevsOnly', 
  (ctx, interaction) => {
    interaction.reply({ content: 'This command can only be used by my devs.' })
  }
)
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import { Event } from 'sl-commands'

/** ctx is a object containing the client and the handler instances */
new Event(
  'commandDevsOnly', 
  (ctx, interaction) => {
    interaction.reply({ content: 'This command can only be used by my devs.' })
  }
)
```

{% endtab %}
{% endtabs %}


---

# 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://synclab.gitbook.io/sl-commands/events-and-features/slcommands-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.
