# Events

A full reference and API documentation can be found on the Chariot.Event documentation page.

# Template

This is an empty event template ready to be used for creating new event handler files. This template can be used and filled in as required for creating new events. A detailed explanation can be found down below underneath the template.

const Chariot = require('chariot.js');

class EventName extends Chariot.Event {
    constructor() {
        super('EVENT_NAME');
    }

    /**
     * Main event handling method running upon the registered event being fired.
     * Arguments passed to the execute method are always relative to the event it is processing.
     * For instance the "ready" event has no args, whereas the "message" event gets passed "message"
     * 
     * @param {*} args Arguments for the main event executor as described above.
     */
    async execute(...args) {
        /* Main event handler logic goes here */
    }
}

module.exports = new EventName();

PRO TIP

Don't forget to export a newly instantiated object of the event class as shown above instead of just the class!

# Explanation

A valid Chariot.js event file that's inherited the abstract Chariot.Event class can be split into two distinct sections:

  •   Constructor Instantiation
  •   Main event executor

# Constructor Instantiation

Instantiating an event handler file requires the extending of the abstract base class, which in return requires a super call to its superclass. As stated in the full API reference on the Chariot.Event documentation page, the constructor of said abstract class, hence also the super call for the actual event handler class, requires a valid Eris client event name the event file should handle. Specifying an event name is case sensitive and will throw an error upon initialization in case an invalid event name was passed.

PRO TIP

A full list of valid Eris client events including their passed parameters and types can be found here.

# Main Event Executor

The main event executor is the "main" method of each Chariot.js event and is the only executor for the registered event. This method gets invoked with all passed parameters from the event upon being fired from the Chariot.Client.

# Examples

Here are a few event examples to demonstrate how to structure a Chariot.js event file

# Client Ready

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();

# Check if a Message contains a certain Keyword

const Chariot = require('chariot.js');

class CheckUser extends Chariot.Event {
    /**
     * Instantiating the superclass with the appropriate event name
     */
    constructor() {
        super('messageCreate');
    }

    async execute(message) {
        if (message.content.includes('Foo')) {
            message.channel.createMessage('Bar!');
        } 
    }
}

module.exports = new CheckUser();

# Handling Shard Errors

const Chariot = require('chariot.js');

class ShardErrors extends Chariot.Event {
    /**
     * Instantiating the superclass with the appropriate event name
     */
    constructor() {
        super('error');
    }

    async execute(error, id) {
        Chariot.Logger.error('SHARD ERROR', `Shard ${id} encountered following error: ${error}`);
    }
}

module.exports = new ShardErrors();

# Notify upon joining a new Guild

const Chariot = require('chariot.js');

class GuildJoin extends Chariot.Event {
    /**
     * Instantiating the superclass with the appropriate event name
     */
    constructor() {
        super('guildCreate');
    }

    async execute(guild) {
        Chariot.Logger.event(`Joined a new guild called ${guild.name}!`);
    }
}

module.exports = new GuildJoin();