An NPM Package to make creating new Discord.JS bots efficiently
Discordjs-commands is a NPM module that adds in a command handler and helpful functions
npm install djs-commands --save
This is not a full Discord.JS bot tutorial. Please check out TheSourceCode for that.
1 - To start using the Command Handler after installation, we'll first need require djs-commands and create a new Command Handler with the proper folder name and prefixes.
const { CommandHandler } = require("djs-commands")
const CH = new CommandHandler({
folder: __dirname + '/commands/',
prefix: ['?', '??', 'tsc?']
});
2 - Inside of the message event, we're going to do a little parsing and checking if they ran an available command or not.
bot.on("message", (message) => {
if(message.channel.type === 'dm') return;
if(message.author.type === 'bot') return;
let args = message.content.split(" ");
let command = args[0];
let cmd = CH.getCommand(command);
if(!cmd) return;
try{
cmd.run(bot,message,args)
}catch(e){
console.log(e)
}
});
3 - And of course we're going to need a command file. So inside of your bot folder, create a folder called commands. I'm going to create a file called test.js and put the following code inside of it.
module.exports = class test {
constructor(){
this.name = 'test',
this.alias = ['t'],
this.usage = '?test'
}
async run(bot, message, args) {
await message.delete();
message.reply(this.name + " worked!")
}
}
4 - And that's it! You have a working command handler now for all the commands you could want!
- 1.0.0
- ADD: Readme, CommandHandler, and basic stuff I will need.
- 1.1.0
- UPDATE: Changed system to work as module
- 1.1.3
- UPDATE: Fixing file loader system
- 1.2.0
- FIX: Fixed the loading system, added usage examples, and fixed index file.
- 1.2.1
- FIX: Forgot to change index.js file back to normal...
- 1.2.2
- UPDATE: Fixed some documentation.
https://github.com/nedinator/djs-commands
- Fork it (https://github.com/nedinator/djs-commands/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request