Main Class - Paultje52/SupaBotBase GitHub Wiki

When using require to import the SupaBotBase, the primary export is the main class.

Contructor options

When constructing the SupaBotBase, you have three options inside a object. All the options are optional.

Name Description Value type Default Value
Dir The main bot directory Path (__dirname, process.cwd()) process.cwd()
Token The Discord Bot token String (Read from token.txt in dir)
clientOptions The options for discord.js Object {}

Example

const SupaBotBase = require("SupaBotBase");
let bot = new SupaBotBase({
  dir: __dirname,
  clientOptions: {
    autoReconnect: true
  }
});
// Token is read from token.txt. This is just your token.

setConfig(config)

This sets the config for the bot. The config is saved on bot.config, or in commands, events and functions this.main.config. The config can be the config itself (an object) or the path to the config, relative from the main directory (Set at the option dir in the main constructor). There are a couple options you can set from your config. Look at Config for the options.

Example

bot.setConfig({
  prefix: "."
});
bot.setConfig("config.js");
// File config.js
module.exports = {
  prefix: "."
}

loadAll

Asynchronous function to load the functions, events and commands, with the default parameters. Look at loadCommands(), loadEvents() and loadFunctions() for the default values and more explanation.

Example

bot.loadAll().then(() => {
  // Everything loaded!
});

loadFunctions

Asynchronous function to load all the functions and bound then to bot.<functionName>. There is one parameter, path. This is the relative path (from dir in your constructor options). The default value is functions. Look at Functions for more information about the functions.

Example

bot.loadFunctions("functions").then(() => {
  // Functions in directory "functions" loaded!
});

loadEvents

Asynchronous function to load all the events and bound then to the discord.js client. There is one parameter, path. This is the relative path (from dir in your constructor options). The default value is events. Look at Events for more information about the events.

Example

bot.loadEvents("events").then(() => {
  // Events in directory "events" loaded!
});

loadCommands

Asynchronous function to load all the commands. There is one parameter, path. This is the relative path (from dir in your constructor options). The default value is commands. Look at Commands for more information about the commands.

Example

bot.loadCommands("commands").then(() => {
  // Commands in directory "commands" loaded!
});

createDatabase

This is a function to load a key-value sqlite database in your bot. This function has one parameter, the database name. The default value is database.sqlite. The database is set on bot.database or for commands, events and functions on this.main.database. There are a couple options for default values in the Config.

Example

bot.createDatabase("db.sqlite");

For more information about the database system, see Database.

addMessageFunction

You can add your own functions to the message object with addMessageFunction. There are two required parameters to add your own message function. The first one is the name and the second one is the function.

Example

// Add a simple default embed function.
const { MessageEmbed } = require("discord.js");
bot.addMessageFunction("embed", () => {
  return new MessageEmbed().setFooter("©SupaBotBase").setColor("#2f3136");
});

// In your commands, you can now use `message.embed()`.

addMessageHandler

This function adds a message handler function. You can check your things and return false to stop the command handling. Returning nothing or true says that the bot can continue handling the message and fire the command. Your function gets called with one parameter: The message object, including the added message functions from addMessageFunction.

Example

bot.addMessageHandler((message) => {
  // Check things and return nothing, the bot can continue.
});

setPermissionCheck

In your commands, you can add custom permission checks. You have to register the names and the functions in your main bot file. This function has two parameters: The name and the function. Your function is called with two parameters: message and args. The command arguments are parsed. You can return multiple things in your function.

  • Returning nothing or true means that the bot can continue.
  • Returning false stops the execution and doesn't tell something to the user
  • Returning a string or an instance of discord.js' MessageEmbed class sends it and stops execution

The function is only called when it's enabled in the command. Look at the Command page for more information.

Example

bot.setPermissionCheck("test", () => {
  return "STOP!"; // Sends the message "STOP"
});

start

When calling this function, SupaBotBase starts your bot! It returns a promise that gets resolved when the client is logged in.

When the promise get's resolved, doesn't mean that the bot is ready. Discord.js is still processing the guilds of the bot.

Example

bot.start().then(() => {
  // Bot is logged in, but not ready!
});

registerSlashCommands

This function uses all the loaded commands to register slash commands at discord. During development, paste your bot token as a string as the only parameter to only enable the commands in one guild. This is usefull for development. Look at Slash Commands for more information about slash commands in discord and SupaBotBase.

It's recommended to register the slash commands after you logged in your bot for stability reasons.

Example

// Register slash commands everywhere (for production)
bot.registerSlashCommands();
// Register slash commands in one guild (for development)
bot.registerSlashCommands("GUILD_ID");

activateErrorHandler

This activates the ErrorHandler. Click here for more information.

bot.activateErrorHandler();

translateRawMessageEvents

This method makes sure that when a reaction is added to a message or removed from a message, but that message isn't cached, the messageReactionAdd or messageReactionRemove event is still called with the normal parameters.

bot.translateRawMessageEvents();

Whole main bot file

This is an example of the whole bot file

let bot = new SupaBotBase({
  dir: __dirname
});
bot.loadAll().then(async () => {
  bot.setConfig("config.js");
  bot.createDatabase("database.sqlite");
  bot.addMessageFunction("embed", () => {
    return new MessageEmbed().setFooter("©SupaBotBase").setColor("#2f3136");
  });
  bot.addMessageHandler((message) => {
    // console.log(1);
    // Return false > Stop message handling.
  });

  bot.setPermissionCheck("test", (_message, _args) => {
    return "Nope!";
  });

  // Activate the error handler
  bot.activateErrorHandler();

  // Translate raw events (MessageReactionAdd and MessageReactionRemove) that discord.js doesn't do because they aren't cached!
  bot.translateRawMessageEvents();

  // Start the bot
  console.log("Starting bot...");
  await bot.start();

  // Log that the bot is logged in!
  console.log("\x1b[2mBot logged in, waiting for ready signal...\x1b[0m");
  bot.registerSlashCommands("534285160040890368");
});