discord-commander
v1.0.1
Published
A command manager for discord.js bots.
Downloads
6
Readme
discord-commander
A command manager for discord.js bots.
import { Client } from 'discord.js';
import { Command, Commander, Event, parse, subscribe } from 'discord-commander';
class TestCommand extends Command('test') {
@parse.nextWord private subCommand: string;
@parse.remainingWords private arguments: string[];
@subscribe('new')
public onMessage(ctx: Event) {
ctx.message.reply(`${ctx.command} ${this.subCommand} [${this.arguments.join(', ')}]`);
}
}
const commander = new Commander('!', [
TestCommand,
]);
const client = new Client();
// Either let commander setup its own simple event listeners
commander.simpleSetup(client);
// or delegate messages to commander manually
const isInvalid = (message) => message.author.bot || !commander.isKnownCommand(message);
const logCommand = (message) => {
const [commandName, argumentString] = commander.parseCommand(message);
console.log(`Command(name="${commandName}", args="${argumentString}")`);
}
client.on('message', (message) => {
if (isInvalid(message)) { return; }
logCommand(message);
commander.handleMessage('new', message);
});
client.on('messageUpdate', (message) => {
if (isInvalid(message)) { return; }
logCommand(message);
commander.handleMessage('edit', message);
});
client.login(discord_secret);
!test someSubCommand argumentOne argumentTwo
Install
- Install library:
npm i discord-commander
. - Enable
experimentalDecorators
andemitDecoratorMetadata
intsconfig.json
. - Try the example above.
Note: If you are using vscode you might need to set javascript.implicitProjectConfig.experimentalDecorators
to true
in the workspace settings.
Development
- Grab your discord-bot secret from the discord developer portal.
- Create a
secrets.json
file and store your discord-bot secret asdiscord_token
inside it. - Install dependencies:
npm i
. - Start demo:
npm run demo:start
.