Creating an addon

Creating addons are not very hard with the Zyno Bot addons library. To start with, you need to create a new folder inside the /addons/ folder of Zyno Bot, which will be the base folder of your addon. Inside the folder, you need to create a file called index.js. This is the only file which will get executed by the bot, so if you're using additional files, you need to import them inside your index.js file.

To create the addon itself, you need to start by importing the Zyno Bot addons library. The library includes various classes and methods which allows you to easily create your addon. To create the addon itself, you will need to use the Addon class. This will comunicate between the library and the bot and makes you able to make changes or detect any changes made to the Discord server(s).

The Zyno Bot addons library is by default installed on all copies of Zyno Bot. The library only works for version 1.5.0 and higher.

An example to create an addon is:

const addons = require('zyno-bot-addons'); // Import the addons library

const myAddon = new addons.Addon({ // Create a new addon
    name: 'My new addon', // The name of your addon
    description: 'Creates a ping command', // A small description of what your addon does
    author: 'Luuk', // Your (user)name so the owner of the copy knows who created the addon
    version: '1.0.0', // The version of your addon
    bitfield: [addons.bitfield.COMMANDS] // The permissions your addon needs
});

This is an example of how you can create an addon. After the addon has been added, the owner of the bot would need to execute the /addons command. This is a manager for the owner where the owner can enable, disable or restart addons. By default new addons will be disabled. Once the owner presses the start button, the addon will start. After adjusting Addon class, the addon will automatically be stopped again, which requires the owner to start the addon again.

As you can see in the example, the addon requires some information in order to be created. In the example is already explained what everything is, but here is another small explanation:

  • name: This is the name of the addon you'd like to create. The only requirement for the name is that it's a string between 1 and 50 characters.

  • description: A brief description of what your addon does, so the owner of the bot understands what it does. The only requirement for the description is that it's a string between 1 and 100 characters.

  • author: A (user)name to show the owner of the bot who the person is who created the bot. The only requirement for the author is that it's a string between 1 and 50 characters.

  • version: The version of your addon. You can change this for updates of your addon for example. The only requirement for the version is that it's a string between 1 and 10 characters.

  • bitfield: The bitfield is more complicated. The bitfield should be an array which gives the library information about what permissions your addon needs. The value inside the array should be received from the bitfield object of the library which you imported earlier. See here which permissions you need for your addon.

To create the command itself, we will need to use the CommandBuilder class. This will allow us to easily build a command. The data will be passed to the bot itself which can create the data from this class the Discord API needs to create the actual command.

const commandBuilder = new addons.CommandBuilder() // This will allow us to create a new command
.setName('ping') // This will be the name of the new command
.setDescription("Replies with the message 'pong'") // This will tell the member what the command does
.setOverwrite(true); // If there is another command with the same name, it will overwrite the command

As you can see, the CommandBuilder class requires some functions. We are using three functions of the class where two of them are required to create a command.

  • setName: This is a required function and will create the name of the command which the members of the servers are able to execute.

  • setDescription: This is also a required function and will create the description of the command which helps the members of the servers understand what the command does.

  • setOverwrite: This is not a required function, but we use this as there already exists a command named ping on Zyno Bot by default. This function will remove the previous ping command and create a new command named ping which will be our own command.

Now we have build the command, we need to actually create it so users will be able to execute the command. To do this, we need to call the createCommand function of the Addon class we created earlier. But before we can do this, we need to make sure we can actually use the functions of the Addon class. It takes some time before the bot is ready and if we call these functions before the bot is ready, it may result in an error, which we don't want to happen. To do this, we can listen to the ready event of the Addon class which will be emitted once the bot is ready. We will need to add the following lines to our code:

myAddon.once('ready', () => { // When the bot is ready, we use once as the event only gets emitted once
    myAddon.createCommand(commandBuilder).then(cmd => { // Creates the command
        // A function which gets executed when the command was successfully created
    }).catch(err => {
        // A function which gets executed when creating the command failed
    });
});

You can see that after we use the createCommand function of the Addon class, we add some functions behind the createCommand function named then and catch. This means that the createCommand function returns a Promise. A Promise allows the function to create something, which might take some time, and when it's done, it executes the code provided in the then function. When the Promise gets 'fulfilled', which we call it when we can use the then function, it will return an instance of the CommandHandler class, where we will talk about more in a minute. However, if an error has occured, it will not execute the code provided in the then function, but the code in the catch function. The catch function returns a string which tells us why the command couldn't be created.

Now we have created the command itself, we want to make sure that the bot actually replies to the command. To do this, we need to use the CommandHandler class which we've received in the then function of the Promise. The CommandHandler class allows us to listen to the execute event. This event will be emitted once a user executes the command and provides the Command class which we can use to reply to this command. To make a reply, we need to add the following lines inside the then function of the Promise we received when we'd created the command:

cmd.on('execute', command => { // When the command is being executed by a user
    command.reply('Pong!').catch(console.log); // Replies to the command
});

As you can see, the reply function of the Command class also returns the catch function which we saw in the createCommand function of the Addon class before, which means that also here a Promise is being returned. What we're doing here is that if we can't send the reply, the bot will log the reason why we can't send the reply in the console of the bot and doesn't crash. Just like the createCommand function of the Addon class, you can use the then function here too, which we won't talk about in this guide.

Eventually you should have this code by now:

const addons = require('zyno-bot-addons');

const myAddon = new addons.Addon({
    name: 'My new addon', // The name of your addon
    description: 'Creates a ping command', // A small description of what your addon does
    author: 'Luuk', // Your (user)name so the owner of the copy knows who created the addon
    version: '1.0.0', // The version of your addon
    bitfield: [addons.bitfield.COMMANDS] // The permissions your addon needs
});

const commandBuilder = new addons.CommandBuilder() // This will allow us to create a new command
.setName('ping') // This will be the name of the new command
.setDescription("Replies with the message 'pong'") // This will tell the member what the command does
.setOverwrite(true); // If there is another command with the same name, it will overwrite the command

myAddon.once('ready', () => { // When the bot is ready, we use once as the event only gets emitted once
    myAddon.createCommand(commandBuilder).then(cmd => { // Creates the command
        // A function which gets executed when the command was successfully created
        cmd.on('execute', command => { // When the command is being executed by a user
            command.reply('Pong!').catch(console.log); // Replies to the command
        });
    }).catch(err => {
        // A function which gets executed when creating the command failed
        console.log(err);
    });
});

In this version, we have added a console.log function inside the catch function of the Promise which is being returned in the createCommand function of the Addon class. This is not necessary to add, but it does tell you in the console why a command couldn't be created if it couldn't be created.

Last updated