This repository contains a simple framework for Discord based on Discord.js. It currently implements an argument parser and a command handler with support for subcommands.
This library runs only on Node.js v12 and newer.
const SimpleFramework = require('discord-simple-framework');
var bot = new SimpleFramework({
discord: {
autorun: true, // Connect the bot as soon as possible
token: 'xxx', // Your bot token
shard: [0, 1] // Shard
},
prefixes: ['$', '#BOT_MENTION# '], // Bot will react to messages starting with $ and its mention followed by space
userCooldown: 1000 // Global cooldown for each user
});
bot.addCommand('ping', {
description: 'Responds with pong', // Description for the command. Can be used to generate help messages
channelCooldown: 5000, // Allow using this command only once every 5 seconds per channel
channelReplyCooldown: 30000, // Additionally, the reply will be sent in a DM if executed more than once in 30 seconds
handler: event => event.reply('Pong!') // Reply in the channel the message was sent
})
var helpCmd = bot.addCommand('help', {
description: 'Helps people',
usage: '<command>', // Can be used to generate help messages
handler: (event, args) => { // Used if no subcommands matched
if (args.length === 0) {
// Replies by sending a DM to the issuer. The command list is generated by accessing the list of all commands
event.replyDM('Available commands: ' + Object.keys(bot.commandHandler.subcommands).join(', '));
} else if (bot.commandHandler.subcommands[args[0]]) {
// Embeds are supported. This example also shows how the description and usage properties can be used
var cmd = bot.commandHandler.subcommands[args[0]];
var fields = [];
if (cmd.description) fields.push({
name: 'Description',
value: cmd.description
});
if (cmd.usage) fields.push({
name: 'Usage',
value: args[0] + ' ' + cmd.usage
});
event.replyDM({
embed: {
title: args[0],
fields: fields
}
});
} else {
event.replyDM('Unknown command: ' + args[0]);
}
},
subcommands: {
help: event => event.replyDM('Ha, ha. Very funny.') // You can also specify the handler like this
}
});
// You can also define subcommands by calling a function
helpCmd.addSubcommand('secret', {
description: 'Shh!',
handler: event => event.reply(':tada:')
});