{"size": 1855, "ext": "js", "max_stars_repo_path": "lib/router.js", "max_stars_repo_name": "mklabs/discord-li", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/router.js", "max_issues_repo_name": "mklabs/discord-li", "max_issues_repo_issues_event_min_datetime": "2018-07-07T11:58:02.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-07T16:31:38.000Z", "max_forks_repo_path": "lib/router.js", "max_forks_repo_name": "mklabs/discord-li", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 39.4680851064, "max_line_length": 104, "alphanum_fraction": 0.6566037736}
const commands = require('./commands');
const path     = require('path');

// Thanks to [The Perfect Lil' Bot](https://gist.github.com/eslachance/3349734a98d30011bb202f47342601d3)
module.exports = (client, config, commandsPath) => {
  const botCommands = commands.findCommands(commandsPath);

  return async (message) => {
    // This event will run on every single message received, from any channel or DM.

    // It's good practice to ignore other bots. This also makes your bot ignore itself
    // and not get into a spam loop (we call that 'botception').
    if(message.author.bot) return;

    // Also good practice to ignore any message that does not start with our prefix,
    // which is set in the configuration file.
    if(message.content.indexOf(config.prefix) !== 0) return;

    // Here we separate our 'command' name, and our 'arguments' for the command.
    // e.g. if we have the message '+say Is this the real life?' , we'll get the following:
    // command = say
    // args = ['Is', 'this', 'the', 'real', 'life?']
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    // Now, find the good command to execute based on fist param
    const cmd = botCommands.find(cmdFile => path.basename(cmdFile, '.js') === command);

    if (!cmd) {
      const msg = `Sorry mate, no ${command} to execute ...`
      const m = await message.channel.send(msg);
      return m.edit(msg);
    }

    // Failsafe check on empty command
    if (!command) {
      const msg = `Sorry mate, no ${command} to execute ...`
      const m = await message.channel.send(msg);
      return m.edit(msg);
    }

    // We found a command, execute with (client, message, command, args)
    const commander = require(cmd);
    return commander(client, message, command, args);
  };
};
