Skip to content

Custom Functions

Written by


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.

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).

1
bot.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
bot.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.

Advanced Custom Functions Examples can be found here, some are useful others are more likely miscellaneous.

  • 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.

In this example we will create a custom function which sends an image as an attachment. The usage is as following:

1
$sendImage[URL];
1
bot.functionManager.createFunction({
2
name: "$sendImage", // our function name
3
type: "djs", // notice how we don't need any params? that's the magic of JavaScript
4
code: async (d) => {
5
const data = d.util.aoiFunc(d);
6
const [URL] = data.inside.splits; // our parameter of the function
7
8
let image = URL;
9
const a = await d.message.channel.send({
10
// send the attachment in the current channel
11
files: [
12
{
13
attachment: image // set the given URL as attachment
14
}
15
]
16
});
17
18
data.result = a; // set the "message" as result, to make aoi.js acknowledge it
19
return {
20
code: d.util.setCode(data)
21
};
22
}
23
});