# discord-response-mock

> Makes the behavior of your discord bot testable

Latest version **0.3.2** (published 2019-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install discord-response-mock
pnpm add discord-response-mock
yarn add discord-response-mock
bun add discord-response-mock
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2019-12-09 |
| First published | 2019-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 40.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Julian Kaindl |
| Maintainers | juliankaindl |
| Keywords | discord, discord.js, testing, mocking |

## Links

- npm: https://www.npmjs.com/package/discord-response-mock
- Repository: https://github.com/KaindlJulian/discord-response-mock
- Issues: https://github.com/KaindlJulian/discord-response-mock/issues
- npm.io page: https://npm.io/package/discord-response-mock

## Dependencies (1)

- [discord.js](https://npm.io/package/discord.js.md) ^11.5.1

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 0.3.2 (latest) — 2019-12-09
- 0.3.1 — 2019-11-21
- 0.2.6 — 2019-11-20
- 0.2.5 — 2019-11-20
- 0.2.4 — 2019-11-16
- 0.2.3 — 2019-11-16
- 0.2.2 — 2019-11-16
- 0.2.1 — 2019-11-10
- 0.1.1 — 2019-11-09
- 0.1.0 — 2019-11-06

## README

# Discord Response Mock

> Making the behavior of your discord bot testable

<br/>
<p>
<!--Build-->
<a href="https://github.com/KaindlJulian/discord-response-mock/actions?query=workflow%3Abuild"><img alt="build" src="https://github.com/KaindlJulian/discord-response-mock/workflows/build/badge.svg"></a>
<!--npm ver-->
<a href="https://www.npmjs.com/package/discord-response-mock"><img alt="npm" src="https://img.shields.io/npm/v/discord-response-mock?color=blue&label=%20npm"></a>
<!--dependencies-->
<a href="https://depfu.com/github/KaindlJulian/discord-response-mock?project_id=10173"><img alt="dependencies" src="https://badges.depfu.com/badges/e275d67f0a44c91d4752f5f25d337921/overview.svg"></a>
<br/><br/>
<!--npm banner-->
<a href="https://www.npmjs.com/package/discord-response-mock"><img alt="dependencies" src="https://nodei.co/npm/discord-response-mock.png"></a>
</p>
<br/>

## Prerequisites

- ID of a discord guild to test in
- The token of a bot application with admin rights on this guild
- If you want to test a bot it should be running in a seperate process

For more information take a look at the [setup FAQ](guild_bot_setup.md).

## Getting started

#### Install

:exclamation: This package relies on a connection to the given discord guild.

```
npm install --save-dev discord-response-mock
```

## Examples

### Responses

#### Basic

```js
import { ResponseClient } from 'discord-response-mock';

const client = await new ResponseClient().setup(
  '[TEST_GUILD_ID]',
  '[TEST_BOT_TOKEN]',
);

client.write('!ping').then(response => {
  console.log(response.content); // pong
});
```

In the discord client this will look like:

<a float="right" href="https://support.discordapp.com/hc/en-us/articles/204849977-How-do-I-create-a-server"><img src="media/example.png" width="250"></a>

#### Mocha command test

A mocha test for a command of your discord bot could look like this.

```js
import assert from 'assert';
import { ResponseClient } from 'discord-response-mock';

const options = {
    messagePrefix: '!',
    specificUserId: '[YOUR_BOT_ID]',
    responseTimeout: 3000
};

let client;

before(async () => {
    client = await new ResponseClient(options).setup('[TEST_GUILD_ID]', '[TEST_BOT_TOKEN]');
});

describe('ping', function () => {
    this.timeout(options.responseTimeout);    // mochas default timeout is 2s

    it('should respond with correct message', done => {
        client.write('ping').then(response => {
            assert.equal(response.content, 'pong');
            done();
        });
    });
});

after(async () => {
    await client.cleanup();
});
```

### Mocking

#### Message Object

```js
import { MockClient } from 'discord-response-mock';

const mock = await new MockClient().setup(
  '[YOUR_GUILD_ID]',
  '[YOUR_BOT_TOKEN]',
);

const message = await mock.message('message content');
```

## Options

```js
const opts = {
    /**
     * If set and a valid discord text channelId, the tests will be performed on
     * this channel. Otherwise a tempory channel will be created.
     *
     * @type {string}
     * @memberof ClientOptions
     */
    channelId?: string;

    /**
     * If set and a valid discord voice channelId, the bot will automatically
     * connect to this channel.
     *
     * @type {string}
     * @memberof ClientOptions
     */
    voiceChannelId?: string;

    /**
     * Changes the name of the temporary testing channels. voice and text.
     *
     * @type {string}
     * @memberof ClientOptions
     * @default 'Running tests | created: Mon, 01 Jan 2019 00:00:00 GMT'
     */
    tempChannelName?: string;

    /**
     * Sets a string that will be prefixed to each message
     *
     * @type {string}
     * @memberof ClientOptions
     */
    messagePrefix?: string;

    /**
     * Sets a timeout on how long to wait for responses. Can only be between 100 and 10000 __milliseconds__.
     *
     * `100 <= timeout <= 10000`
     *
     * @type {number}
     * @memberof ResponseClientOptions
     * @default 5000
     */
    responseTimeout?: number;

    /**
     * If set, only use the responses from this userId. Others are filtered out.
     * If this field is not set, the first response will be returned.
     *
     * @type {string}
     * @memberof ResponseClientOptions
     */
    specificUserId?: string;
};
```

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