Getting Started with Disnode
This page will help you get started with Disnode. You'll be up and running in a jiffy!
This tutorial will get on started on making a bot using all the available features of the Disnode Library. This will get updated as more features get added!
NEED HELP?
We understand the Disnode's documentation is lacking and we working hard in expanding it. If you need help or have and questions please feel free to join our discord server:
1. Installing and Getting Bot Token
Installing
npm install disnode --save
That simple! Note you will most likely have to compile many of the C Files used by 3rd party packages. Heres a great tutorial on how to set this up, but if its already setup on your machine, it should just build automatically.
Getting API Keys
Discord
- Go to: https://discordapp.com/ and create an account/login (Normal Discord user account is fine)
- Then Navigate to More->Developers->MyApplications or go to https://discordapp.com/developers/applications/me
- Click new application and fill out the info for your bot. (REDIRECT NOT NECESSARY FOR BOTS)
- Then click Create Bot User.
- Congrats you created your bot. Now bookmark this page as its info will be useful later on.
2. Initilzting Disnode
Base Bot Code
Now you have the required packages and API Keys. Lets begin the code! We're going section by section here, but full source code will provided.
First things first lets require disnode at the top of our code.
var Disnode = require("disnode")
Then we are going to create our bot using the Disnode constructor. We pass in our Discord Bot User Token and a path to our config. Now keep this private and out of your git commits (trust me).
var mybot = new Disnode("DISCORD_BOTUSER_TOKEN" ,"PATH/TO/CONFIG.json")
Now we are going to create an empty object for storing custom commands we write inside this bot class. More in this coming in a later tutorial.
var myCommands = {}
Now we have the base of our code and can start the bot by calling
mybot.startBot();
Nothing will happen. Sorry to get you hyped but we havn't assigned it to do anything. So shall we make it do something?
CURRENT CODE:
var Disnode = require("disnode");
var myBot = new Disnode("KEY","./BotConfig.json");
var myCommands = {};
//...
myBot.startBot();
3. Events
First things lets set our events. Currently Disnode sends out these events:
`
Bot_Init
Called when we start our bot.Bot_Ready
Called when the bot has successfully logged into Discord. (usually takes 1-2s)Bot_RawMessage
Called when Any Message is sent in any channel or server. Good for logging but for running commands we have our own system you might enjoy ;)
Since these are impliment like normal events in NodeJS well just copy the boilerplate into our bot.
var Disnode = require("disnode");
var myBot = new Disnode("KEY","./BotConfig.json");
var myCommands = {};
myBot.on("Bot_Init", function () {
console.log("[MyBot] Bot Init.");
});
myBot.on("Bot_Ready", function () {
console.log("[MyBot] Bot Ready.");
});
myBot.on("Bot_RawMessage", function (msg) {
console.log("[MyBot] RawMessage: " + msg.author.name + " :: " + msg.content);;
});
myBot.startBot();
4. Loading Config
Our bot config is where our commands and manager settings are loaded from, so its important to load it first. We do this by calling the loadConfig(cb)
function in Disnode. loadConfig takes in a callback which is called after the config has been successfully loaded.
IMPORTANT
Load your managers after you config has been loaded, else you will get null errors!
We can implement it as such to out bot.
var Disnode = require("disnode");
var myBot = new Disnode("KEY","./BotConfig.json");
var myCommands = {};
myBot.on("Bot_Init", function () {
console.log("[MyBot] Bot Init.");
});
myBot.on("Bot_Ready", function () {
console.log("[MyBot] Bot Ready.");
myBot.loadConfig(OnLoad);
});
myBot.on("Bot_RawMessage", function (msg) {
console.log("[MyBot] RawMessage: " + msg.author.name + " :: " + msg.content);;
});
var OnLoad = function(){
}
myBot.startBot();
Now also create a BotConfig.json
file in your root directory of your bot. (NOTE: This file can be named anything and put anywhere, just make sure its the same as the path you specified in your bot's constructor).
Inside this file just type: {commands: []}
and save it. This is just an empty JSON object that will be filled when the bot starts up! Later we plan to make this automated.
5. Loading Managers!
Now to the fun part! Loading managers. For this example well just be loading Core Managers, but we have information on how to load third-party manager here: Managers Explaintion.
For this bot, we want to have these features (manager name):
- Read Commands (CommandHandler)
- Play Audio (MusicManager)
- Help Command (HelpManager)
- Add Commands from Chat (SayManager)
To load a manager, you call addManager({name,options}
from Disnode. The name parameter is the name of the Manager (Should be the same as the Filename) and options are is an object of options that the manager takes in. This should be specified in the managers documentation.
MANAGER NAMING
Managers all always be named the same throughout, including configs, filenames, variables, etc.
Now our bot with managers added:
var Disnode = require("disnode");
var myBot = new Disnode("KEY","./BotConfig.json");
var myCommands = {};
myBot.on("Bot_Init", function () {
console.log("[MyBot] Bot Init.");
});
myBot.on("Bot_Ready", function () {
console.log("[MyBot] Bot Ready.");
myBot.loadConfig(OnLoad);
});
myBot.on("Bot_RawMessage", function (msg) {
console.log("[MyBot] RawMessage: " + msg.author.name + " :: " + msg.content);;
});
var OnLoad = function(){
myBot.addManager({name:"CommandHandler", options:{prefix: "!"}});
myBot.addManager({name: "VoiceManager", options:{voiceEvents:true}});
myBot.addManager({name:"MusicManager", options:{maxVolume:2.0}});
myBot.addManager({name:"HelpManager", options:{}});
myBot.addManager({name:"SayManager", options:{}});
myBot.CommandHandler.LoadList(myBot.config.commands); // Load Commands from Config
}
myBot.startBot();
6. PROFIT!
Thats it! You should have a functional bot now! If you go to your bots config file after starting it up, you should see all the default values and commands. Typing "!help" should list all commands and their usage. Now in the next steps, well going to be writing custom commands in our bot.
Updated less than a minute ago