Custom Functions
Table of Content
Getting Started with Custom Functions
To begin with, there are two types of custom functions.
aoi.js
custom functions and djs
custom functions.
As you may can tell by the name aoi.js
custom functions include aoi.js functions, on the other side djs
refers to discord.js
which means it will create a discord.js custom function using discord.js functions/syntax.
Aoi.js Custom Functions
1bot.functionManager.createFunction({2 name: "$function", // custom function name, can be anything.3 params: [ "parameter", ... ], // function parameters, $function[parameter;parameter]4 type: "aoi.js", // custom function type5 code: "..." // the code that will be executed6});
Pretty simple example, and usage.
- To return any parameter of
params: [...]
you can use{name}
(name stands for the parameter name).
Discord.js Custom Functions
1client.functionManager.createFunction({2 name: "$function", // custom function name, can be anything.3 type: "djs", // custom function type4 code: ... // the code that will be executed5});
1client.functionManager.createFunction({2 name: "$function",3 type: "djs",4 code: async d => { // fetching data5 const data = d.util.aoiFunc(d);6 const [parameter, ...] = data.inside.splits; // command parameters7
8 ... // your code9
10 data.result = ... // needed for the function to properly work, will set the "output" of the function11 return {12 code: d.util.setCode(data) // will return the output from data.result13 };14 }15});
You can create error messages like the following;
For message errors:
1aoiError.fnError(d, type, data, message);
For console errors:
1aoiError.consoleError(name, e);
TYPE | RETURNS |
---|---|
member | Invalid Member ID Provided In … |
message | Invalid Message ID Provided In … |
channel | Invalid Channel ID Provided In … |
user | Invalid User ID Provided In … |
role | Invalid Role ID Provided In … |
guild | Invalid Guild ID Provided In … |
emoji | Invalid Emoji ID Provided In … |
option | Invalid Option ID Provided In … |
custom | Invalid custom message |
1if (!parameter) return d.aoiError.fnError(d, "custom", {}, "Custom Error Message");2// will return a error message when the parameter "parameter" doesn't have any arguments.
Custom Functions Examples
Advanced Custom Functions Examples can be found here, some are useful others are more likely miscellaneous.
Aoi.js Custom Functions Examples
- This section has been removed due to aoi.js custom functions being broken. We will update this section once aoi.js custom functions are fixed.
- A fix has been implemented in 6.8.10 which will be released in the near future.
Discord.js Custom Functions Examples
In this example we will create a custom function which sends an image as an attachment. The usage is as following:
1$sendImage[URL;name?];
1// we need this for our function!2const { AttachmentBuilder } = require("discord.js");3
4client.functionManager.createFunction({5 name: "$sendImage", // our function name6 type: "djs", // notice how we don't need any params? that's the magic of JavaScript7 code: async (d) => {8 const data = d.util.aoiFunc(d);9 if (data.err) return d.error(data.err); // we can make this function require arguments, if user doesnt pass one itll throw an error message10 const [URL, name = "image.png"] = data.inside.splits; // our parameter of the function, image has a default value "image.png" therefore making it optional11
12 const result = new AttachmentBuilder(13 type === "buffer"14 ? Buffer.from(image.addBrackets())15 : image.addBrackets(),16 { name: name.addBrackets() }17 );18
19 d.files.push(result); // pushing our file to make aoijs "register it"20 // we COULD send it directly, but this would break other file related commands!21
22 return {23 code: d.util.setCode(data),24 files: d.files25 };26 }27});