https://github.com/dragory/knub-command-manager
A JavaScript/TypeScript library for managing and matching text-based commands
https://github.com/dragory/knub-command-manager
Last synced: 10 months ago
JSON representation
A JavaScript/TypeScript library for managing and matching text-based commands
- Host: GitHub
- URL: https://github.com/dragory/knub-command-manager
- Owner: Dragory
- Created: 2019-06-16T02:13:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:51:25.000Z (over 3 years ago)
- Last Synced: 2023-03-31T12:57:35.248Z (about 3 years ago)
- Language: TypeScript
- Homepage:
- Size: 462 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Knub Command Manager
[][npm]
[npm]: https://www.npmjs.com/package/knub-command-manager
Knub Command Manager is a library for managing and matching text-based commands.
It was created for use in [Knub](https://github.com/dragory/Knub), a Discord bot
framework, but can also be used as standalone.
## Features
* Command prefixes (e.g. `!command` where `!` is the prefix)
* Regex support for prefixes and command names
* Required and optional parameters
* `-options` and `-switches` with shortcuts (e.g. `-f`)
* Customizable option prefixes (defaults to `--` and `-`)
* Parameter/option types with runtime validation and type conversion
* Including custom types
* Default values for arguments/options
* "Rest arguments" (multiple arguments returned in an array) and "catch-all"
arguments (treats the rest of the string as the value for that argument)
* Filters that are run before and after commands signature validation
* "Pre-filters" (run before) are useful for e.g. checking if the command is
valid to use in the current context. If a pre-filter fails, command matching
is resumed to other potentially matching commands.
* "Post-filters" (run after) are useful for e.g. command cooldowns. If a
post-filter fails, command matching is not resumed and no error is returned,
making the result equivalent to no command matching at all.
* Full TypeScript typings with generics for custom data passed to filters and
argument/option type conversion functions and for extra data passed in command
configuration
## Installation
`npm install knub-command-manager`
## Basic usage
```js
import { CommandManager, parseSignature as p, string } from 'knub-command-manager';
const manager = new CommandManager({
prefix: '!'
});
const command1 = manager.add('echo', p(''));
const command2 = manager.add('ping', { ip: string() });
const matchedCommand = manager.findMatchingCommand('!echo hello');
```