Command Loader
In this guide, we will explain how to get started with command handlers, create multiple commands in one file, and handle events.
Why Should You Use a Loader?
Why should you use a command handler? Storing your commands in a single file may seem fine at first, but as the number of commands grows, it becomes difficult to find and update them. That's why you should use a command handler to maintain order in your main file and rid yourself of clutter.
Main File
- In the
LoadCommandsclass constructor, it is mandatory to specify your instance of theAoiClientclass. In theloadCommandsmethod, the first argument is the path to the folder, and the second argument is responsible for console output. - For easier use or tracking of variables in the library, you can utilize the
loadVariablesmethod, which is built into theLoadCommandsclass.
const { LoadCommands } = require("aoitelegram");
const loader = new LoadCommands(bot)
loader.loadCommands("./command/", true);
loader.loadVariables("./variables/", true);
Example
One Command in a File
- ESModule
- Commonjs
export default {
name: "ping",
code: `$replyMessage[Bot ping: $ping ms]`,
};
module.exports = {
name: "ping",
code: `$replyMessage[Bot ping: $ping ms]`,
};
Multiple Commands in a File
- ESModule
- Commonjs
export default [
{
name: "ping",
code: `$replyMessage[Bot ping: $ping ms]`,
},
{
name: "pong",
code: `$replyMessage[ping 🏓]`,
},
];
module.exports = [
{
name: "ping",
code: `$replyMessage[Bot ping: $ping ms]`,
},
{
name: "pong",
code: `$replyMessage[ping 🏓]`,
},
];
Events
- ESModule
- Commonjs
export default [
{
name: "count",
code: `$replyMessage[Count value: $getVar[count]]`,
},
{
type: "message",
code: `$setVar[count;$message[1]]`,
},
];
module.exports = [
{
name: "count",
code: `$replyMessage[Count value: $getVar[count]]`,
},
{
type: "message",
code: `$setVar[count;$message[1]]`,
},
];
Variables
- ESModule
- Commonjs
export default {
variables: {
sempai: 10,
string: "Hello, world!",
aoijs: true,
webapp: false,
mz: [],
},
tables: ["main"],
};
module.exports = {
variables: {
sempai: 10,
string: "Hello, world!",
aoijs: true,
webapp: false,
mz: [],
},
tables: ["main"],
};