All Projects → lmmfranco → discord-message-handler

lmmfranco / discord-message-handler

Licence: Apache-2.0 license
Message and command handler for discord.js bots and applications

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to discord-message-handler

DiscordBot-Template
A boilerplate / template for discord.js bots with 100% coverage of Discord API, command handler, error handler based on https://discordjs.guide/
Stars: ✭ 129 (+578.95%)
Mutual labels:  discord-api, discord-js, command-handler
command-handler
Discord Bot (w/ Command Handler)
Stars: ✭ 25 (+31.58%)
Mutual labels:  discord-js, command-handler
discord-clock
A simple clock script for your bot to show what time it is in your server | Discord.js v13 ready!
Stars: ✭ 29 (+52.63%)
Mutual labels:  discord-api, discord-js
discord-rose
The simple Discord library for advanced users
Stars: ✭ 37 (+94.74%)
Mutual labels:  discord-api, discord-js
opensource-discordbots
Curated list of awesome open-source Discord Bots
Stars: ✭ 19 (+0%)
Mutual labels:  discord-api, discord-js
Commando-guide
Beginner's guide for discord.js-commando.
Stars: ✭ 56 (+194.74%)
Mutual labels:  discord-api, discord-js
Discord-BOT-Dashboard-V2
Discord BOT Dashboard V2 is made to make Discord BOT Development easy, designed to help create applications without writing a single line of code while using a user friendly Web-Dashboard!
Stars: ✭ 120 (+531.58%)
Mutual labels:  discord-api, discord-js
Discord-Presser-Server-Nuker
Nuke Discord Bot in Js (Beta has arrived)
Stars: ✭ 253 (+1231.58%)
Mutual labels:  discord-api, discord-js
framework
✨ A framework for creating discord bots build with discord.js. Modular | Flexible | Powerful | Development | Interactions
Stars: ✭ 41 (+115.79%)
Mutual labels:  discord-api, discord-js
Discord-Rich-Presence-Party-Mode
Discord Rich Presence Tool. Party Mode | Cycle Mode integrated. The first to do it.
Stars: ✭ 18 (-5.26%)
Mutual labels:  discord-api, discord-js
discord-voice
⏲️ A complete framework to facilitate the tracking of user voice time using discord.js
Stars: ✭ 33 (+73.68%)
Mutual labels:  discord-api, discord-js
blaster
Web hooks for message queues
Stars: ✭ 14 (-26.32%)
Mutual labels:  messaging, message-handler
Karuma
Karuma is a Discord Bot including Nukes, Raids, Mass DM and other features. Only for educational purposes 🥱🚀
Stars: ✭ 132 (+594.74%)
Mutual labels:  discord-api, discord-js
slshx
⚔️ Strongly-typed Discord commands on Cloudflare Workers
Stars: ✭ 163 (+757.89%)
Mutual labels:  discord-api, discord-js
Hurricano
An amazing open-source Discord bot using MongoDB with many features such as a customizable prefix, a reaction menu, music, role requirement giveaways and much more!
Stars: ✭ 97 (+410.53%)
Mutual labels:  discord-api, discord-js
KCommando
Annotation-based multifunctional command handler framework for JDA & Javacord.
Stars: ✭ 26 (+36.84%)
Mutual labels:  discord-api, command-handler
Discord-BOT-Dashboard
This version is outdated, please check out Discord BOT Dashboard v2
Stars: ✭ 32 (+68.42%)
Mutual labels:  discord-api, discord-js
discord-super-utils
A modern python module including many useful features that make discord bot programming extremely easy.
Stars: ✭ 106 (+457.89%)
Mutual labels:  discord-api, discord-js
Aometry
An awesome multipurpose discord bot build using discord.js v13 with support for slash commands and context menus
Stars: ✭ 51 (+168.42%)
Mutual labels:  discord-api, discord-js
DJS-Handler
Simple and easy to use Discord.js command and event handler, use this template for your next bot!
Stars: ✭ 21 (+10.53%)
Mutual labels:  discord-js, command-handler

NPM version NPM downloads Build Status

About

discord-message-handler is a module written to simplify message and command handling for discord.js bots and applications.

Table of Contents

Installation

Simply navigate to your project's folder and type npm install discord-message-handler --save on the command line.

Usage

To start using the module you must require it into you script like this (changed in 2.0)

Old style require:

const MessageHandler = require('discord-message-handler').MessageHandler;
const handler = new MessageHandler();

ES2015:

const { MessageHandler } = require('discord-message-handler');
const handler = new MessageHandler();

Typescript:

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:

client.on('message', message => {
    handler.handleMessage(message);
});

Simple message handlers

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));

Command handler

handler.onCommand("/doit").do((args, rawArgs, message) => {
    message.channel.send(`Doing something for ${message.author}...`)
});

Commands with alias

handler.onCommand("/information").alias("/info").alias("/i").do((args) => {
    doSomething(args[0]);
});

Commands with usage info

handler 
    .onCommand("/info")
    .minArgs(2)
    .whenInvalid("Invalid command. Usage: /info <a> <b>")
    .do((args) => {
        doSomething(args[0]);
        doSomethingElse(args[1]);
    });

Commands with regex validation

handler
    .onCommand("!roll")
    .minArgs(1)
    .matches(/(\d+)?\s?d(6|20|100)/g)
    .whenInvalid("Invalid command. Usage: `!roll <number of dices> d<type of dice>`. Valid dices: d6, d20, d100")
    .do((args) => {
        // Dice roll logic
    })

Commands with multiple validations

In this version, you can chain commands and give whenInvalid an object with multiple parameters. They will be validated in the following order: 1. allowedChannels 2. minimumArguments 3. regularExpression

You can also pass in a flag when you want to send it to a user or a channel. Standard will be a reply in the channel.

handler
    .onCommand("!roll")
    .minArgs(1)
    .matches(/(\d+)?\s?d(6|20|100)/g)
    .allowedChannels(["45054768187"])
    .whenInvalid({
        sendToPlayer: true, 
        allowedChannels: "You are not allowed to use that in this channel.",
        minimumArgs: "Did you forget something?",
        regexPattern: "Please check your input."})
    .do((args) => {
        // Dice roll logic
    })

Command invocation deletion

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.

// User's message will be deleted after 1500ms
handler.onCommand("/afk").deleteInvocation(1500).then((message) => {
    message.channel.send(`${message.author} is now AFK.`);
});

Example handling messages across multiple files

Consider you have the following structure:

├── commands
│   ├── greetings.js
│   └── helper.js
└── index.js

greetings.js:

module.exports.setup = function(handler) {
    handler.onCommand("/help").reply("<some helpful message>");
    handler.onCommand("/ping").reply("<actually calculate ping>");
}

helper.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:

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

Case sensitivity

In case you want message filters to be case sensitive you just need to call this function once:

handler.setCaseSensitive(true);

By default all message filters are case insensitive. (false)

Logging

To enable logging call handler.enableLogging() and pass a function to handle logs.

handler.enableLogging((filterType, filter, message) => {
    console.log(`${new Date().toISOString()} ${filterType}: ${filter} - "${message.content}"`);
});

Contributing

Feel free to send a pull request or open an issue if something is not working as intended or you belive could be better.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].