Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Plugins

ærion edited this page Aug 9, 2017 · 10 revisions

This framework consists of plugins that extend the functionality of the Discord client.

Plugins can be added via external modules; several have already been included in the framework.

Commander

The Commander plugin allows Commands to be executed. When a successful command is called, the Commander will execute the corresponding command functions.

Recommended to be used with the Bridge plugin.

Example usage:

const filepath = 'path/to/commands'
class PingCommand extends Command {
  constructor (...args) {
    super(...args, {
      name: 'ping',
      options: { guildOnly: true }
    })
  }

  handle ({ msg }) {
    return msg.reply('Pong!')
  }
}
const commands = [
  PingCommand,
  {
    name: 'powerup',
    execute: async ({ msg, client }) => {
      const channel = await client.getChannel('247727924889911297')
      return msg.reply(channel ? 'Powered up!' : 'Unpowered...')
    }
  }
]

client
.register('commands', commands)
.register('commands', filepath, { groupedCommands: true })
// groupedCommands should be true if you're grouping commands in folders

Router

The Router plugin takes care of Modules by routing event arguments to the corresponding modules' methods.

Object modules (modules that don't use the Module class) have the client supplied as the last argument in event invokers.

Example usage:

class BanModule extends Module {
  constructor (...args) {
    name: 'guilds:bans',
    events: {
      guildBanAdd: 'onBan'
    }
  }

  onBan (guild, user) {
    console.log(`User ${user.username} has been banned from ${guild.name}`)
  }
}
const modules = [
  BanModule,
  {
    name: 'guilds:logger',
    events: {
      guildCreate: 'newGuild'
    },
    newGuild: (guild, client) => console.log(`New guild: ${guild.name}`)
  }
]

client.register('modules', modules)

Bridge

The Bridge plugin maintains a chain of Middleware, passing messages through each middleware function and resolving a Container object.

Requires Commander to work properly.

Example usage:

client.register('middleware', [{
  name: 'checkPrivate',
  priority: 1,
  process: (container) => {
    container.isPrivate = !!msg.guild
    return Promise.resolve(container)
  }
}])

Additional Usage

Registering Components into Built-in Plugins

As shown in the examples above, to fully utilise the plugins, the components have to be registered into the appropriate plugin with the register() method.

The 1st argument should take in the plugin type, while the 2nd argument should not receive the raw component, but instead should be an array or object containing the components.

// Correct:
client
.register('commands', [ SomeCommand ])
.register('commands', {
  core: {
    'ping': PingCommand,
    'help': HelpCommand
  }
})

// Incorrect:
client.register('commands', SomeCommand)

Custom Plugins

Custom plugin support is available as long as the added plugins follow certain criteria:

  • Must be a class
  • Should contain the following methods, to which any number of arguments can be passed:
    • register(), unregister() - called when client.register() and client.unregister() is run
    • run() - called when client.run() and client.stop() is run
    • reload() - use this to reload files in cache (usually for file paths)
    • stop() - use this when you want to stop the plugin from running

To add a custom plugin:

client.createPlugin('pluginType', PluginClass)

To access a plugin:

const plugin = client.plugins.get('pluginType')

The plugin's register method will be called when client.register() is called with the first argument matching the corresponding plugin type.