Message and command handler for discord.js bots and applications.
npm install discord-message-handlernpm install discord-message-handler --save on the command line.
js
const MessageHandler = require('discord-message-handler').MessageHandler;
const handler = new MessageHandler();
`
ES2015:
`js
const { MessageHandler } = require('discord-message-handler');
const handler = new MessageHandler();
`
Typescript:
`ts
import { MessageHandler } from 'discord-message-handler';
const handler = new MessageHandler();
`
Define rules for the message handler (shown later in the next sections) then parse messages in the as they arrive:
`js
client.on('message', message => {
handler.handleMessage(message);
});
`
$3
`js
handler.whenMessageContainsWord("shrug").reply("¯\\_(ツ)_/¯");
handler.whenMessageContains("lol").sometimes(33).reply("kek"); // 33% chance
handler.whenMessageContainsExact("dota").replyOne(["volvo pls", "rip doto"]);
handler.whenMessageContainsOne(["br", "brazil"]).reply("huehue");
handler.whenMessageStartsWith("help").then(message => doSomething(message));
`
$3
`js
handler.onCommand("/doit").do((args, rawArgs, message) => {
message.channel.send(Doing something for ${message.author}...)
});
`
$3
`js
handler.onCommand("/information").alias("/info").alias("/i").do((args) => {
doSomething(args[0]);
});
`
$3
`js
handler
.onCommand("/info")
.minArgs(2)
.whenInvalid("Invalid command. Usage: /info ")
.do((args) => {
doSomething(args[0]);
doSomethingElse(args[1]);
});
`
$3
`js
handler
.onCommand("!roll")
.minArgs(1)
.matches(/(\d+)?\s?d(6|20|100)/g)
.whenInvalid("Invalid command. Usage: !roll . Valid dices: d6, d20, d100")
.do((args) => {
// Dice roll logic
})
`
$3
You can automatically delete the message that triggered a command using the deleteInvocation method. The time argument is optional, and if absent the message will be deleted imediatelly.
`js
// User's message will be deleted after 1500ms
handler.onCommand("/afk").deleteInvocation(1500).then((message) => {
message.channel.send(${message.author} is now AFK.);
});
`
$3
Consider you have the following structure:
`
├── commands
│ ├── greetings.js
│ └── helper.js
└── index.js
`
greetings.js:
`js
module.exports.setup = function(handler) {
handler.onCommand("/help").reply("");
handler.onCommand("/ping").reply("");
}
`
helper.js:
`js
const { MessageHandler } = require('discord-message-handler');
module.exports.setup = function(handler) {
/ [Optional] You can recreate the handler using the parent context so your IDE will properly give out suggestions for the handler /
const myhandler = new MessageHandler(handler);
myhandler.whenMessageContainsWord("hey").reply("yo!");
myhandler.whenMessageContainsWord("hi").reply("oh hi there :)");
}
`
index.js:
`js
const { MessageHandler } = require('discord-message-handler');
const greetingsCommands = require('./commands/greetings');
const helperCommands = require('./commands/helper')
const handler = new MessageHandler();
greetingsCommands.setup(handler);
helperCommands.setup(handler);
// (...) code continues
`
$3
In case you want message filters to be case sensitive you just need to call this function once:
`js
handler.setCaseSensitive(true);
`
By default all message filters are case insensitive. (false)
$3
To enable logging call handler.enableLogging() and pass a function to handle logs.
`js
handler.enableLogging((filterType, filter, message) => {
console.log(${new Date().toISOString()} ${filterType}: ${filter} - "${message.content}");
});
``