{"id":28300250,"url":"https://github.com/lmmfranco/discord-message-handler","last_synced_at":"2025-10-05T18:13:37.851Z","repository":{"id":48270731,"uuid":"76195927","full_name":"lmmfranco/discord-message-handler","owner":"lmmfranco","description":"Message and command handler for discord.js bots and applications","archived":false,"fork":false,"pushed_at":"2021-08-03T20:04:33.000Z","size":1429,"stargazers_count":19,"open_issues_count":19,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-30T08:45:22.824Z","etag":null,"topics":["bot","command","command-handler","command-line-parser","discord","discord-api","discord-js","message-filter","message-handler","messaging","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lmmfranco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-11T19:49:24.000Z","updated_at":"2022-06-07T17:05:04.000Z","dependencies_parsed_at":"2022-09-01T20:35:44.579Z","dependency_job_id":null,"html_url":"https://github.com/lmmfranco/discord-message-handler","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/lmmfranco/discord-message-handler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmmfranco%2Fdiscord-message-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmmfranco%2Fdiscord-message-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmmfranco%2Fdiscord-message-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmmfranco%2Fdiscord-message-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lmmfranco","download_url":"https://codeload.github.com/lmmfranco/discord-message-handler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lmmfranco%2Fdiscord-message-handler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259768508,"owners_count":22908228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","command","command-handler","command-line-parser","discord","discord-api","discord-js","message-filter","message-handler","messaging","nodejs"],"created_at":"2025-05-23T15:20:21.497Z","updated_at":"2025-10-05T18:13:37.843Z","avatar_url":"https://github.com/lmmfranco.png","language":"TypeScript","readme":"\u003cp\u003e\n    \u003ca href=\"https://www.npmjs.com/package/discord-message-handler\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/discord-message-handler.svg?maxAge=3600\" alt=\"NPM version\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/discord-message-handler\"\u003e\u003cimg src=\"https://img.shields.io/npm/dt/discord-message-handler.svg?maxAge=3600\" alt=\"NPM downloads\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://circleci.com/gh/lmmfranco/discord-message-handler/tree/master\"\u003e\u003cimg src=\"https://img.shields.io/circleci/project/github/lmmfranco/discord-message-handler/master.svg?style=flat-square\" alt=\"Build Status\" title=\"Build Status\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## About\n**discord-message-handler** is a module written to simplify message and command handling for [discord.js](https://github.com/discordjs/discord.js) bots and applications.\n\n## Table of Contents\n\n\u003c!-- toc --\u003e\n\n- [Installation](#installation)\n- [Usage](#usage)\n  * [Simple message handlers](#simple-message-handlers)\n  * [Command handler](#command-handler)\n  * [Commands with alias](#commands-with-alias)\n  * [Commands with usage info](#commands-with-usage-info)\n  * [Commands with regex validation](#commands-with-regex-validation)\n  * [Commands with multiple validations](#commands-with-multiple-validations)\n  * [Command invocation deletion](#command-invocation-deletion)\n  * [Example handling messages across multiple files](#example-handling-messages-across-multiple-files)\n  * [Case sensitivity](#case-sensitivity)\n  * [Logging](#logging)\n- [Contributing](#contributing)\n\n\u003c!-- tocstop --\u003e\n\n## Installation\nSimply navigate to your project's folder and type `npm install discord-message-handler --save` on the command line.\n\n## Usage\nTo start using the module you must require it into you script like this (changed in 2.0)\n\nOld style require:\n```js\nconst MessageHandler = require('discord-message-handler').MessageHandler;\nconst handler = new MessageHandler();\n```\n\nES2015:\n```js\nconst { MessageHandler } = require('discord-message-handler');\nconst handler = new MessageHandler();\n```\n\nTypescript:\n```ts\nimport { MessageHandler } from 'discord-message-handler';\nconst handler = new MessageHandler();\n```\n\n\nDefine rules for the message handler (shown later in the next sections) then parse messages in the as they arrive:\n```js\nclient.on('message', message =\u003e {\n    handler.handleMessage(message);\n});\n```\n\n### Simple message handlers\n```js\nhandler.whenMessageContainsWord(\"shrug\").reply(\"¯\\\\_(ツ)_/¯\");\nhandler.whenMessageContains(\"lol\").sometimes(33).reply(\"kek\"); // 33% chance\nhandler.whenMessageContainsExact(\"dota\").replyOne([\"volvo pls\", \"rip doto\"]);\nhandler.whenMessageContainsOne([\"br\", \"brazil\"]).reply(\"huehue\");\nhandler.whenMessageStartsWith(\"help\").then(message =\u003e doSomething(message));\n```\n\n### Command handler\n```js\nhandler.onCommand(\"/doit\").do((args, rawArgs, message) =\u003e {\n    message.channel.send(`Doing something for ${message.author}...`)\n});\n```\n\n### Commands with alias\n```js\nhandler.onCommand(\"/information\").alias(\"/info\").alias(\"/i\").do((args) =\u003e {\n    doSomething(args[0]);\n});\n```\n\n### Commands with usage info\n```js\nhandler \n    .onCommand(\"/info\")\n    .minArgs(2)\n    .whenInvalid(\"Invalid command. Usage: /info \u003ca\u003e \u003cb\u003e\")\n    .do((args) =\u003e {\n        doSomething(args[0]);\n        doSomethingElse(args[1]);\n    });\n```\n\n### Commands with regex validation\n```js\nhandler\n    .onCommand(\"!roll\")\n    .minArgs(1)\n    .matches(/(\\d+)?\\s?d(6|20|100)/g)\n    .whenInvalid(\"Invalid command. Usage: `!roll \u003cnumber of dices\u003e d\u003ctype of dice\u003e`. Valid dices: d6, d20, d100\")\n    .do((args) =\u003e {\n        // Dice roll logic\n    })\n```\n\n### Commands with multiple validations\n\nIn this version, you can chain commands and give whenInvalid an object with multiple parameters.\nThey will be validated in the following order:\n    1. allowedChannels\n    2. minimumArguments\n    3. regularExpression\n\nYou 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.\n\n\n```js\nhandler\n    .onCommand(\"!roll\")\n    .minArgs(1)\n    .matches(/(\\d+)?\\s?d(6|20|100)/g)\n    .allowedChannels([\"45054768187\"])\n    .whenInvalid({\n        sendToPlayer: true, \n        allowedChannels: \"You are not allowed to use that in this channel.\",\n        minimumArgs: \"Did you forget something?\",\n        regexPattern: \"Please check your input.\"})\n    .do((args) =\u003e {\n        // Dice roll logic\n    })\n```\n\n### Command invocation deletion\nYou 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.\n```js\n// User's message will be deleted after 1500ms\nhandler.onCommand(\"/afk\").deleteInvocation(1500).then((message) =\u003e {\n    message.channel.send(`${message.author} is now AFK.`);\n});\n```\n\n### Example handling messages across multiple files\n\nConsider you have the following structure:\n```\n├── commands\n│   ├── greetings.js\n│   └── helper.js\n└── index.js\n```\n\ngreetings.js:\n```js\nmodule.exports.setup = function(handler) {\n    handler.onCommand(\"/help\").reply(\"\u003csome helpful message\u003e\");\n    handler.onCommand(\"/ping\").reply(\"\u003cactually calculate ping\u003e\");\n}\n```\n\nhelper.js:\n```js\nconst { MessageHandler } = require('discord-message-handler');\n\nmodule.exports.setup = function(handler) {\n    /* [Optional] You can recreate the handler using the parent context so your IDE will properly give out suggestions for the handler */\n    const myhandler = new MessageHandler(handler);\n    myhandler.whenMessageContainsWord(\"hey\").reply(\"yo!\");\n    myhandler.whenMessageContainsWord(\"hi\").reply(\"oh hi there :)\");\n}\n```\n\nindex.js:\n```js\nconst { MessageHandler } = require('discord-message-handler');\nconst greetingsCommands = require('./commands/greetings');\nconst helperCommands = require('./commands/helper')\n\nconst handler = new MessageHandler();\ngreetingsCommands.setup(handler);\nhelperCommands.setup(handler);\n\n// (...) code continues\n```\n\n### Case sensitivity\nIn case you want message filters to be case sensitive you just need to call this function once:\n```js\nhandler.setCaseSensitive(true);\n```\nBy default all message filters are case insensitive. (false)\n\n### Logging\nTo enable logging call `handler.enableLogging()` and pass a function to handle logs.\n```js\nhandler.enableLogging((filterType, filter, message) =\u003e {\n    console.log(`${new Date().toISOString()} ${filterType}: ${filter} - \"${message.content}\"`);\n});\n```\n\n## Contributing\nFeel free to send a pull request or open an issue if something is not working as intended or you belive could be better.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flmmfranco%2Fdiscord-message-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flmmfranco%2Fdiscord-message-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flmmfranco%2Fdiscord-message-handler/lists"}