Skip to content

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

1
bot.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 type
5
code: "..." // the code that will be executed
6
});

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

1
client.functionManager.createFunction({
2
name: "$function", // custom function name, can be anything.
3
type: "djs", // custom function type
4
code: ... // the code that will be executed
5
});
1
client.functionManager.createFunction({
2
name: "$function",
3
type: "djs",
4
code: async d => { // fetching data
5
const data = d.util.aoiFunc(d);
6
const [parameter, ...] = data.inside.splits; // command parameters
7
8
... // your code
9
10
data.result = ... // needed for the function to properly work, will set the "output" of the function
11
return {
12
code: d.util.setCode(data) // will return the output from data.result
13
};
14
}
15
});

You can create error messages like the following;

For message errors:

1
aoiError.fnError(d, type, data, message);

For console errors:

1
aoiError.consoleError(name, e);
TYPERETURNS
memberInvalid Member ID Provided In …
messageInvalid Message ID Provided In …
channelInvalid Channel ID Provided In …
userInvalid User ID Provided In …
roleInvalid Role ID Provided In …
guildInvalid Guild ID Provided In …
emojiInvalid Emoji ID Provided In …
optionInvalid Option ID Provided In …
customInvalid custom message
1
if (!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!
2
const { AttachmentBuilder } = require("discord.js");
3
4
client.functionManager.createFunction({
5
name: "$sendImage", // our function name
6
type: "djs", // notice how we don't need any params? that's the magic of JavaScript
7
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 message
10
const [URL, name = "image.png"] = data.inside.splits; // our parameter of the function, image has a default value "image.png" therefore making it optional
11
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.files
25
};
26
}
27
});