# Project
This is an extremely simple example project to showcase how easy it is to set up a bot with Chariot.js!
# Project structure
Let's say we have an example project with following simple folder structure:
Project/
├── commands/
│ ├── top_secret/
│ │ └── Secret.js
│ │
│ └── Ping.js
│
├── events/
│ └── Ready.js
│
└── index.js
In this example we built ourselves a nice and simple project structure abstracting commands and events into their respecting folders. This allows for easy organization and abstraction of different parts of the code base.
PRO TIP
Chariot.js supports loading commands and events from any folder structure, no matter how sophisticated or simplistic it may be! More examples in terms of structuring a project can be found on the Getting Started page.
# Entry Point
The main entry point in this example is the index.js
file, in which we'll be requiring Chariot.js and instantiate our bot client!
const Chariot = require('chariot.js');
class MyExampleBot extends Chariot.Client {
constructor() {
super(new Chariot.Config(
'<TOKEN>',
{
prefix: ['c!', '@mention'],
guildPrefixes: [
{ guildID: '<Guild 1>', prefix: '?' },
{ guildID: '<Guild 1>', prefix: ';' }
],
defaultHelpCommand: true,
primaryColor: 'ORANGE',
owner: [
'<Discord Snowflake 1>',
'<Discord Snowflake 2>',
'<Discord Snowflake n>'
],
excludeDirectories: [
'top_secret'
]
},
{
messageLimit: 50,
defaultImageFormat: 'png'
}
));
}
}
module.exports = new MyExampleBot();
We create a main bot class and extend the Chariot.js client class. Within the constructor we call super();
in order to instantiate the superclass Client
which in return instantiates our bot client. The constructor of the Chariot.Client class expects a Chariot.Config object. Within the config we first specify the Discord bot application token, followed by a configuration object for Chariot.js. In this object we've specified components like an array of default prefixes for our bot, an array of custom per-guild prefixes, whether we'll be using the default help command, what the primary color of the help embed should be and which Discord users "own" this bot specified by their IDs. Additionally we tell Chariot.js to ignore the top_secret
folder within the commands
folder. This will as a result ignore all files within the folder, including our Secret.js
file! The last parameter is an object for configuring the Eris client, which in this case tells Eris to reduce the message cache limit to 50 messages per channel and switch the default image format to png
.
That's It! The Chariot.js client is set up and we can now move on to adding our first command and event.
# Adding Commands and Events
Adding new commands and events to our example bot is as simple as creating a new file, filling out the command template or the event handler template (depending on whether we want to add a command or new event handler) and saving the file! Chariot.js will automagically find your new files, recognizes them as valid command and event files and registers them.
For this example we'll be adding a Ping.js
command file and a Ready.js
event file:
# Ping.js
Command File
const Chariot = require('chariot.js');
/**
* This example is an extremely basic command example of how a command could look like and work with Chariot.js
*/
class Ping extends Chariot.Command {
constructor() {
super();
this.name = 'ping';
this.cooldown = 3;
this.help = {
message: 'A super simple ping command',
usage: 'ping',
example: ['ping'],
inline: true
}
}
/**
* This is the main method getting executed by the MessageHandler upon a valid command request
*
* @param {Object} message The emitted message object that triggered this command
* @param {String[]} args The arguments fetched from using the command
* @param {Object} chariot The bot client itself
*/
async execute(message, args, chariot) {
message.channel.createMessage("Pong!");
}
}
module.exports = new Ping();
# Secret.js
Command File
const Chariot = require('chariot.js');
/**
* This command will never be loaded because the directory it lies in is ignored in the Chariot.js client options!
*/
class Secret extends Chariot.Command {
constructor() {
super();
this.name = 'secret';
this.cooldown = 5;
this.owner = true;
this.allowDMs = false;
this.help = {
message: 'A super secret command',
usage: 'secret',
example: ['secret'],
inline: true
}
}
/**
* This is the main method getting executed by the MessageHandler upon a valid command request
*
* @param {Object} message The emitted message object that triggered this command
* @param {String[]} args The arguments fetched from using the command
* @param {Object} chariot The bot client itself
*/
async execute(message, args, chariot) {
message.channel.createMessage("Top Secret command!");
}
}
module.exports = new Secret();
# Ready.js
Event Handler File
const Chariot = require('chariot.js');
class Ready extends Chariot.Event {
/**
* Instantiating the superclass with the appropriate event name
*/
constructor() {
super('ready');
}
/**
* No parameters since the "ready" event doesn't emit any
*/
async execute() {
Chariot.Logger.event(`Bot is ready in ${this.client.guilds.size} guilds!`);
}
}
module.exports = new Ready();
That's It! After saving our new files we can move to the last step, starting our bot!
# Starting The Bot
Given we've followed this example accordingly, it's now only a matter of starting the bot with node index.js
.
If we haven't made any mistakes during setup, we'll be greeted by our console with an output similar to this:
[ 22.10.2019 21:40:34 ] [ COMMANDS ] Successfully loaded 2 commands
[ 22.10.2019 21:40:34 ] [ EVENTS ] Successfully loaded 1 event
[ 22.10.2019 21:40:34 ] [ CHARIOT STARTUP ] Successfully started and logged in!
It might be worth explaining why the console tells us that Chariot.js has successfully loaded 2 commands, albeit only having added a single command so far. This is because we've specified in our client config that we'd like to use the default help command, which internally is registered just as any other command.
# Conclusion
We've now successfully created an extremely powerful, scalable and super easily extensible bot using Chariot.js! This means our new bot will be ready for the most demanding of tasks and run super efficiently. Chariot.js is used in production by many bots, many of which being part of thousands of guilds, proving in the real world that Chariot.js is ready for any task!
What's Next? Be sure to check out the many command and even handler examples and be sure to drop by in our Discord guild if there's any questions! We're an ever growing community of passionate bot developers and would love welcome you!