# 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 %}
