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

# Command Cooldowns

Commands should have a cooldown to reduce the number of times a user uses a command to prevent an issue called "Spamming".

### How does `cooldowns` work?

`cooldowns` is an `Object` option that has 4 types of command cooldowns: `perUser`, `perGuild`, `perUserPerGuild` and `global`. These types of cooldowns help control servers that use a command and prevent abusing it which may crash the bot.

Let's use the "ping" command to demonstrate this:

{% tabs %}
{% tab title="JavaScript" %}
{% code title="ping.js" %}

```javascript
/**
 * @type {import("nocli-handler.js").ICommand}
 */
const Command = {
    description: "Checks client latency",
    type: "BOTH",
    cooldowns: {
        // Set a command cooldown type listed in the documentation
    }
    callback: ({ client, message, args, text }) => {
        return `🏓 Pong! \`${client.ws.ping}ms\``;
    }
}

module.exports = Command;
```

{% endcode %}
{% endtab %}

{% tab title="TypeScript" %}
{% code title="ping.ts" %}

```typescript
import { ICommand } from "nocli-handler.js";

export default {
    description: "Checks client latency",
    type: "BOTH",
    cooldowns: {
        // Set a command cooldown type listed in the documentation
    }
    callback: ({ client, message, args, text }) => {
        return `🏓 Pong! \`${client.ws.ping}ms\``;
    }
} as ICommand;
```

{% endcode %}
{% endtab %}
{% endtabs %}

### What does these types of cooldowns help in?

Here is a table and a specific description on how cooldown types work:

<table><thead><tr><th width="206">Cooldown Type</th><th>Description</th></tr></thead><tbody><tr><td><code>perUser</code></td><td>Sets a cooldown for each user, even if they are in another server or not</td></tr><tr><td><code>perGuild</code></td><td>Sets a cooldown for each server. When a user runs a command in a server, other users will be affected when running that command. However, other servers will not be affected.</td></tr><tr><td><code>perUserPerGuild</code></td><td>Sets a cooldown for each user. Other users will not be affected. However, the user can still run that command in another server.</td></tr><tr><td><code>global</code></td><td>Sets a whole cooldown for all users when a user runs this command. This means that even if other users are not in the same server, the cooldown will still affect them.</td></tr></tbody></table>

{% hint style="warning" %}
When specifying cooldowns, please only specify one of them. Multiple cooldowns detected will result in a `NoCliCommandError`.
{% endhint %}

Also, bot developers should have the freedom to manage cooldowns the way they want. This is useful when you want to slow down the usage of a command by a user. This is why `updateCooldown()` and `cancelCooldown()` options are implemented into `ICommand.callback()`

### `updateCooldown()` and `cancelCooldown()` functions

`updateCooldown()` updates the time the cooldown ends for the user while `cancelCooldown()` cancels the whole cooldown for the user.

{% hint style="warning" %}
`updateCooldown()` and `cancelCooldown()`functions can only be used when a command is executed.
{% endhint %}
