0.2.0 • Published 6 years ago

mock-discordjs v0.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Install

npm install mock-discordjs

Usage - conditional require

Use an environment variable such as MOCK_DISCORD to turn on the mock behavior instead of the actual discordjs. You can see an example in example/ folder, but I will walk through it here.

var isMocked = (process.env.MOCK_DISCORD || false);
var discord_file = ( isMocked ? '../src/mock_discord.js' : 'discord.js');

const Discord = require(discord_file);
const client = new Discord.Client();

Usage - simulating requests from discord server to your bot

Create a file named mock_scenarios.js and create functions for each message you want to simulate. A request defines the user, mentions, content, and whether the it will succeed or fail.

simpleReverseRequest : function(client) {
  client.mockMessage(
    // this is the request message we want to simulate
    {
      content: '.reverse three blind mice'
      ,isMemberMentioned: false
      ,author: { username:'user1234' }
      ,mentions:{
        members: [ {nickname: '@dude1'} ]
      }
    },
    // this gets used by channel.send(), etc to simulate success or failure
    function(resolve, reject) {
      resolve('ok');
    });
}

Run MacOS/Linux

This example will call two of your functions in mock_scenarios.js.

export MOCK_DISCORD=mock_scenarios.js:simpleReverseRequest,failToSend
node ./mybot.js

Run Windows

set MOCK_DISCORD=mock_scenarios.js:simpleReverseRequest,failToSend
node ./mybot.js
0.2.0

6 years ago

0.1.0

6 years ago