dbot-regex-handler
v1.0.0
Published
A module that adds regex based command handling utilities to the bot client object of discord.js and Eris
Downloads
4
Readme
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)