dbot-regex-handler

1.0.0 • Public • Published

dbot-regex-handler

A command handler that works with regexes, for discord.js and Eris.

Example

const Eris = require('eris');
const CommandHandler = require('dbot-regex-handler');

let bot = new Eris(token); // Replace by token or something

bot.handler = new CommandHandler();

bot.handler.endpoint(/^test$/, (match, message) => {
  bot.createMessage(message.channel.id, 'First test command');
});

bot.handler.endpoint(/^test +(\S.+)$/, async (match, message) => {

  // match is the result of the regex match

  await bot.createMessage(message.channel.id, 'Second test command');
  bot.createMessage(message.channel.id, match[1]);
});

bot.on('ready', () => {
  console.log('Bot ready !');
});

bot.on('messageCreate', async (msg) => {
  if (msg.author.bot) return;
  let content = msg.content.replace(new RegExp(`^(?:<@${bot.user.id}> +|\\+)\\b`), '');
  let trimmedContent = content.trim();
  console.log(trimmedContent);

  /*
  * You have to pass the next function with a trimmed version of the string
  * WITHOUT the prefix or the bot mention
  * 
  * Example : @YourBot test fdsfdsqfdq
  *           should be passed as argument as 'test fdsfdsqfdq'
  */

  bot.handler.apply(trimmedContent, msg); // Returns true if command exist, false if it doesn't.
});

bot.connect();

process.on('SIGINT', async function () {
  await bot.disconnect();
  console.log('Disconnected');
  process.exit();
});

License

MIT (SEE LICENSE FILE)

Readme

Keywords

none

Package Sidebar

Install

npm i dbot-regex-handler

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

3.51 kB

Total Files

5

Last publish

Collaborators

  • ahoziorce