# discord-handles

> A simple Discord command handler.

Latest version **7.3.5** (published 2017-10-30) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install discord-handles
pnpm add discord-handles
yarn add discord-handles
bun add discord-handles
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 7.3.5 |
| Published | 2017-10-30 |
| First published | 2016-12-16 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Will Nelson |
| Maintainers | appellation |
| Keywords | discord, discord.js, command handler |

## Links

- npm: https://www.npmjs.com/package/discord-handles
- Repository: https://github.com/appellation/handles
- Homepage: https://github.com/appellation/handles#readme
- Issues: https://github.com/appellation/handles/issues
- npm.io page: https://npm.io/package/discord-handles

## Dependencies (1)

- [tsubaki](https://npm.io/package/tsubaki.md) ^1.2.0

## Recent versions

- 7.3.5 (latest) — 2017-10-30
- 8.0.0-dev.2 (next) — 2018-03-14
- 8.0.0-dev.1 — 2017-12-16
- 8.0.0-dev — 2017-12-15
- 7.3.4 — 2017-10-26
- 7.3.3 — 2017-10-12
- 7.3.2 — 2017-10-09
- 7.3.1 — 2017-09-23
- 7.3.0 — 2017-09-23
- 7.2.0 — 2017-09-12
- 7.1.0 — 2017-09-11
- 7.0.1 — 2017-09-10
- 7.0.0 — 2017-09-10
- 6.2.0 — 2017-08-19
- 6.1.1 — 2017-08-18
- … 28 more at https://npm.io/package/discord-handles/versions

## README

# Handles

[![Handles support server](https://discordapp.com/api/guilds/251245211416657931/embed.png)](https://discord.gg/DPuaDvP)
[![Build Status](https://travis-ci.org/appellation/handles.svg?branch=master)](https://travis-ci.org/appellation/handles)
![Downloads](https://img.shields.io/npm/dt/discord-handles.svg)

For those of us who get frustrated with writing command handlers but don't quite want to use a full framework.  Intended for use with [Discord.js](https://github.com/hydrabolt/discord.js).

Documentation is available at [handles.topkek.pw](http://handles.topkek.pw).

## Getting started

### Installation

```xl
npm install --save discord-handles
```

Or, if you want to risk cutting yourself, install the bleeding edge version:

```xl
npm install --save appellation/handles#master
```

Usually I try to avoid pushing broken code, but sometimes I move a little too fast.

### The basics

```js
const discord = require('discord.js');
const handles = require('discord-handles');

const client = new discord.Client();
const handler = new handles.Client(client);

client.login('token');
```

This will automatically load all commands in the `./commands` directory and handle incoming messages.  See [`Command`](https://handles.topkek.pw/modules/_structures_command_.html) in the docs for information on how to format the exports of the files you place in `./commands`.  Particularly of interest are the `pre`, `exec`, and `post` methods.  The loader and handler can be configured according to [`Config`](https://handles.topkek.pw/modules/_interfaces_config_.html) options passed to the constructor.

```js
const handler = new handles.Client(client, {
    directory: './some/other/awesome/directory',
    prefixes: new Set(['dank', 'memes'])
});
```

Here's an example of what you might place in the `./commands` directory.
```js
const { MessageMentions, Permissions } = require('discord.js');
const { Command, Argument, Validator } = require('discord-handles');

module.exports = class extends Command {
    static get triggers() {
        return ['banne', 'ban'];
    }

    async pre() {
        await this.guild.fetchMembers();

        await new Validator(this)
            .apply(this.guild.me.permissions.has(Permissions.FLAGS.BAN_MEMBERS), 'I don\'t have permission to ban people.')
            .apply(this.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS), 'You don\'t have permission to ban people.');

        const member = await new Argument(this, 'member')
            .setResolver(c => {
                const member = this.guild.members.get(c);

                // if they provided a raw user ID
                if (member) return member;
                // if they mentioned someone
                else if (MessageMentions.USERS_PATTERN.test(c)) return this.guild.members.get(c.match(MessageMentions.USERS_PATTERN)[1]);
                // if they provided a user tag
                else if (this.guild.members.exists(u => u.tag === c)) return this.guild.members.find(u => u.tag === c);
                else return null;
            })
            .setPrompt('Who would you like to ban?')
            .setRePrompt('You provided an invalid user. Please try again.');

        await new Validator(this)
            .apply(member.bannable, 'I cannot ban this person.');
            .apply(member.highestRole.position < this.member.highestRole.position, 'You cannot ban this person.')

        await new Argument(this, 'days')
            .setResolver(c => parseInt(c) || null);
            .setOptional();
    }

    async exec() {
        await this.args.member.ban(this.args.days);
        return this.response.success(`banned ${this.args.member.user.tag}`);
    }
};
```

---
_Source: https://npm.io/package/discord-handles · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
