Utilities - Paultje52/SupaBotBase GitHub Wiki

There are a couple tricks in SupaBaseBot to do some things easier.

Reload commands

  1. Get the command using bot#commands#get (With the command name).
  2. Unload the command by calling bot#commands#delete (With the command name).
  3. Unload all the aliases by looping through them (command#aliases, it's a string array) and calling bot#aliases#delete (With the alias name).
  4. Load the command again with bot#loadCommand with the directory on command#_file.
/* In a command */
// Getting the command information
let command = this.main.commands.get("command_name");

// Unloading the command
thia.main.commands.delete(command);
command.aliases.forEach((alias) => {
  this.main.aliases.delete(alias);
});

// Loading it again
this.main.loadCommand(command._file);

Reload events

Reloading events is harder, because you need the full path to that event file. It's recommended to reload all the events at once.

  1. Get the event function by using bot#events#get with the full file path (path.join(process.cwd(), ...things))
  2. Unload the event with bot#client#removeEventListener with the name of the event (event#instance#getEvent) and the function (event#function).
  3. Load the event again by calling bot#loadEvent with the event path.
/* In a command */
// Get the event
let eventData = this.main.events.get("event_path");
// Unload the event
this.main.client.removeEventListener(eventData.instance.getEvent(), event.function);
// Load the event again
this.main.loadEvent("event_path");

Reload function

When reloading a function, you can just load the function again, because the old function will be overwritten by the new one.

/* In a command */
bot.loadFunction("path"); // Load one function with the path to that function. The path needs to be a full path (process.cwd() etc)!
// OR
bot.loadFunctions(); // Load all the functions

Relogging

If you want to disconnect your bot and then connect your bot again to the api without restarting the whole process, follow these steps.

  1. Disconnect your bot with bot#client#destroy().
  2. Start your bot agian with bot#start. The message handler and slash command handler will be reloaded. The commands, events and files don't reload automatically.

Warning: It's better to restart the node process to prevent memory leaks. The problem with this is that you can cause bugs in your code.

/* In a command */
this.client.destroy();
this.main.start();