description |
---|
A simple boilerplate discord bot you can easily build upon |
Starterbot - Discord
{% embed url="https://andre.gg/git/starterbot" caption="Source code here" %}
Prerequisites
{% embed url="https://docs.andre.gg/basicbot\#prerequisites" caption="Prerequisites are the same as in basic bot so follow those" %}
Setup
The setup is super simple. Run this command to clone the repo:
git clone https://github.com/andrejarrell/Starterbot.git
Enter bot folder and run:
npm install
node setup.js
Run
Run bot with: node bot.js
PM2
PM2 is my favorite process manager for node.js applications. It will restart anytime the app crashes and can reload when changes in the code are detected. First install pm2 globally.
{% embed url="https://pm2.keymetrics.io/" %}
npm i -g pm2
To run your bot run:
pm2 start bot.js
{% embed url="https://devhints.io/pm2" caption="Use this cheat sheet to learn the commands" %}
Commands
All commands are stored in the commands folder and follow a very specific syntax. You may add more .js files but they must follow the same structure.
{% code title="ping.js" %}
module.exports = {
'name': 'ping',
'description': 'Checks the bot\'s latency',
'usage': 'ping',
'owneronly': false,
execute: (bot, args, msg, config) => {
msg.channel.send(`🏓 Pong! \`${Math.floor(bot.ping)}ms\``);
}
};
{% endcode %}
An example of adding a command with the proper syntax:
{% code title="hello.js" %}
module.exports = {
'name': 'hello',
'description': 'Bot will say hello back',
'usage': 'hello',
'owneronly': false,
execute: (bot, args, msg, config) => {
msg.channel.send(`Hello ${msg.author}!`);
}
};
{% endcode %}
Execute()
The execution function is invoked when the command is typed into Discord. It passes 4 important parameters that will be used in our code.
execute: (bot, args, msg, config)
bot
{% hint style="info" %} https://discord.js.org/#/docs/main/stable/class/Client {% endhint %}
The ****bot
****object refers to the Client constructor.
Though others use client
, we use bot
to keep it simple.
All the extended methods and properties are still the same in the Discord.js Docs
//Properties
console.log(bot.user.tag) // 'Testerbot#1542'
console.log(bot.uptime) // 1239123
console.log(bot.ping) // 34
console.log(bot.guilds.size) // 3
//Methods
bot.login('token goes here') // Logs in
//Events
bot.on('guildMemberAdd', member => {
member.send(`Welcome to the server!`); //Emitted when a user joins the server
});
bot.commands
This is an object that stores all of your commands to make them easily accessible.
{
eval: {
name: 'eval',
description: 'Evaluates javascript code',
usage: 'eval <code>',
owneronly: true,
execute: [Function: execute]
},
help: {
name: 'help',
description: "Checks the bot's commands",
usage: 'help <commandname>',
owneronly: false,
execute: [Function: execute]
},
ping: {
name: 'ping',
description: "Checks the bot's latency",
usage: 'ping',
owneronly: false,
execute: [Function: execute]
},
reboot: {
name: 'reboot',
description: 'Reboots the bot',
usage: 'reboot',
owneronly: true,
execute: [Function: execute]
},
stats: {
name: 'stats',
description: "Checks the bot's stats",
usage: 'stats',
owneronly: false,
execute: [Function: execute]
}
}
args
Arguments is a popular way to take parameters in a message and to do what we please with them.
$commandname these are my arguments
turns into ["these","are","my","arguments"]
console.log(args.join(' ')) // 'these are my arguments'
//Since array's index starts at zero
console.log(args[0]) // 'these'
console.log(args[1]) // 'are'
console.log(args[2]) // 'my'
console.log(args[3]) // 'arguments'
//To check if the user supplied arguments
if (!args) return console.log('User supplied no arguments')
//To get specific arguments
if (args[0] !== somevariable) return console.log('User supplied invalid arguments')
//Some commands like:
//$ban @user reason
//require you to use this to get the full reason
let reason = args.slice(1).join(' ')
console.log(reason) // Returns the rest of the args
msg
{% hint style="info" %} https://discord.js.org/#/docs/main/stable/class/Message {% endhint %}
The msg
object refers to parameter we passed in the message
event in bot.js
//Properties
console.log(msg.content) // 'Message content appears here'
console.log(msg.author.username) // 'andrejarrell'
console.log(msg.author.id) // '123456789012345678'
//Methods
msg.channel.send('Hello there!')
msg.delete() // Deletes message
msg.react('😃') // Reacts with emoji
config
config
refers to the config.json
file in the root directory.
{% code title="config.json" %}
{
"prefix": "$",
"ownerid": "123456789012345678",
"token": "NTYxMDM3MDM1Mzc3MjYyNTkz.D385Ig.FfFodONLqmHVZKIGh7SokUq26oM"
}
{% endcode %}
console.log(config.prefix) // '$'
console.log(config.ownerid) // '123456789012345678'
console.log(config.token) // 'NTYxMDM3MDM1Mzc3MjYyNTkz.D385Ig.FfFodONLqmHVZKIGh7SokUq26oM'
Properties
All commands have properties that identify them and let us know what they do.
{% code title="reboot.js" %}
name: 'reboot',
description: 'Reboots the bot',
usage: 'reboot',
owneronly: true,
{% endcode %}
name
Type: string
name: 'eval', // the name of the command
description
Type: string
description: 'Reboots the bot', // the description of the command
usage
Type: string
usage: 'reboot', // make sure to include arguments if they exist
owneronly
Type: boolean
owneronly : true, // stops users from using owner command