Skip to content

Client Commands

Written by


Before we get started let’s cover the basics.

  • aoi.js reads bottom to top, meaning the code starts from the bottom and starts working towards the top.

An example for that is the following:

1
client.command({
2
name: "example",
3
code: `
4
$sendMessage[Bye!]
5
$sendMessage[Hello!]
6
`
7
});

In this case, the code will send Hello! first and then Bye!. This applies to all commands within aoi.js.

Creating commands brings functionality to your bot, this structure will be used for all commands including events. Events will be covered at a later time.

The structure is as following, (if you’re not using a command handler):

1
<client>.command({ ... });

<client> stands for the AoiClient you defined in your index.js file at the very top.

Commands have a variety of properties, which will be covered in the next section.

For now you only need to know about name and code.


The name defines the name of the command and also how to execute it. Meaning you would be able to use [prefix]name to execute the command.

1
<client>.command({
2
name: "ping"
3
});

This does nothing, as of now. It’s just a command called ping. To give it functionality we use the code property.

The code property defines the functionality of the command - its core.

1
<client>.command({
2
name: "ping",
3
code: `My ping is: $ping MS!`
4
});

This would execute upon using the [prefix]ping within your Discord Server, this would return the current bot latency.

There are a variety of commands, event commands, base commands, interaction commands and way more.

The base command, also known as prefix command has a few properties.

1
<client>.command({
2
name: string,
3
aliases? : string,
4
nonPrefixed? : boolean,
5
executeAt? : string,
6
code: string
7
});

An example of using all properties would be the following:

1
<client>.command({
2
name: "ping",
3
aliases: ["pong"],
4
nonPrefixed: false,
5
executeAt: "both",
6
code: `My ping is $ping MS!`
7
});
OPTIONINPUTEXPLANATION
namestringCommand Name.
aliasesstringAliases, work the same way as name.
nonPrefixedbooleanSet your command as non prefixed, meaning it can be executed without the actual prefix.
dmOnlybooleanSet the command to be useably only in guilds or Direct Messages.
executeAtstringDefine where it may can get executed in.
1. guild
2. dm
3. groupDM
4. both
codestringYour command code.

Event commands have the following structure:

1
<client>.<event>Command({
2
name? : string,
3
channel? : string,
4
code: string,
5
})

<client> stands - once again - for the AoiClient you defined in your index.js file at the very top.

<event> stands for the event you want to use for example onBanAdd would become <client>.banAddCommand(). this applies to all commands.

An example of using all properties would be the following:

1
<client>.banAddCommand({
2
name: "Event Command Example",
3
channel: "123456789012345", // **must** be a valid channel ID
4
code: `
5
<@$authorID> just got banned!
6
`
7
});
OPTIONINPUTEXPLANATION
namestringCommand Name.
typestringDefines the command type, any event, for example: interaction or loop.
channelstringWhere the output may go.
codestringYour command code.

1
<client>.interactionCommand({
2
name: string,
3
type: string,
4
prototype: string,
5
code: string,
6
});
OPTIONINPUTEXPLANATION
namestringCommand Name.
typestringDefines the command type.
prototypestringDefines what the command will be triggered by.
1. button
2. selectMenu
4. slash
5. modal
codestringYour command code.

1
<client>.loopCommand({
2
name: string,
3
type: string,
4
channel? : number,
5
executeOnStartup? : boolean,
6
every: number,
7
code: string
8
});
OPTIONINPUTEXPLANATION
namestringCommand Name.
typestringDefines the command type, any event, interaction or loop.
channelstringWhere the output may go.
executeOnStartupbooleanIf the command will be executed after the bot restarted.
everynumberInterval of the loop command.
codestringYour command code.

You can also keep your index.js - or whatever the file is called for you - neat and tidy, by using command handlers.

By using command handlers, you’re creating a file for each command instead of putting them all in the same file.

1
const { AoiClient } = require("aoi.js");
2
3
const client = new AoiClient({
4
// our configuration
5
});
6
7
client.loadCommands("./commands/", true); // This will be your commands folder, you can give it any name you want. The true enables the logging, you can disable it by replacing true with false.

Make sure to create a commands folder in the root directory, meaning where your index.js file is located.

  • index.js your main file
  • package.json
  • commands the folder for your commands

Once that is done, we start creating files inside of that folder.

  • index.js
  • package.json
  • Directorycommands the folder for your commands
    • ping.js

These files can be named what you want, however you need to add the .js syntax after the name, to turn it into a Javascript file.

Let’s, once again, create a ping command. It’s basically the same, just one line changes.

1
<client>.command({
2
module.exports = ({
3
name: "ping",
4
code: `My ping is $ping MS!` // The ping function returns the current ping.
5
});

Everything besides the first line stays the same, easy right?

Your command should look like this in the end:

1
module.exports = {
2
name: "ping",
3
code: `My ping is $ping MS!` // The ping function returns the current ping.
4
};

So let’s look at adding multiple commands to the same file, yes, multiple!

Yet again, we pretty much only change the first and last line.

1
module.exports = ({
2
module.exports = [{
3
name: "ping",
4
code: `My ping is $ping MS!` // The ping function returns the current ping.
5
});
6
}, {
7
name: "uptime",
8
code: `I'm online for $uptime!` // The uptime function returns the current uptime in hours, minutes and seconds.
9
}];

This is basically an array just with a little extra spice.

Your command should look like this in the end,

1
module.exports = [
2
{
3
name: "ping",
4
code: `My ping is $ping MS!` // The ping function returns the current ping.
5
},
6
{
7
name: "uptime",
8
code: `I'm online for $uptime!` // The uptime function returns the current uptime in hours, minutes and seconds.
9
}
10
];