Writing Your Custom Aoi.js Plugin Using Aoi.js-Library
If you've ever wanted to enhance your Aoi.js experience by adding custom functionality, you're in the right place! In this post, we'll guide you through the process of creating your own Aoi.js plugin using the Aoi.js-Library.
Getting Started
The first step is to create a fork of the Aoi.js-Library and clone it to your local system. Once cloned, open your terminal in that directory and install all the required dependencies by typing:
npm i
With this, you've initialized your forked repository and are now ready to start working.
Writing Your Plugin
Now, let's move on to creating your custom plugin. In your terminal, execute the following command:
npx aoilib create username/pluginname
This command will generate a folder with your username in the plugins
directory, setting up the structure for your plugin.
Basic Structure
Inside the generated folder, you'll find an index.js
file. This file contains the structural object of an array of functions based on their roles:
module.exports = {
pkgJson: require('./package.json'),
load: [], // functions to run when the client gets ready
commands: {
pre: [], // functions to run before the command is executed
post: [], // functions to run after the command is executed
},
events: [], // functions to run when an event is triggered
functions: [], // custom Aoi.js functions accessible for all commands
}
Writing the Plugin
Create a new file (e.g., min.js
) and add the following code:
module.exports = {
name: "$min", // name of the function
type: "djs", // type of function
author: "usersatoshi", // author name
version: ["6.6.1"], // minimum Aoi.js version required
description: "Returns the minimum value of the given numbers. If there is only one number, it will return that number.",
example: "$min[1,2,3,4,5] // returns 1",
code: async (d) => {
const data = d.util.aoiFunc(d); // get function data
if (data.err) return d.error(data.err); // if the function didn't receive any parameters, and it was required, return an error
const nums = data.inside.splits; // get parameters in $min[] in the form of an array
data.result = Math.min(...nums); // save the output in data.result
return {
code: d.util.setCode(data), // return data
};
},
};
Now, import this file into the main file and add it to the functions
property:
const min = require('./min.js')
module.exports = {
pkgJson: require('./package.json'),
load: [], // functions to run when the client gets ready
commands: {
pre: [], // functions to run before the command is executed
post: [], // functions to run after the command is executed
},
events: [], // functions to run when an event is triggered
functions: [min], // custom Aoi.js functions accessible for all commands
}
Bundling Your Plugin
Before committing your changes to the forked repository and creating a pull request to the original repo, you need to bundle your plugin into a single file. Execute the following command in the terminal:
npx aoilib bundle username/pluginname
This command will create a _bundle.js
file, and the details will be displayed in the console.log.
Now you can commit this bundled file to your forked repository, and once accepted, others will be able to use your plugin automatically. Happy coding!