Making your own module - Paultje52/BananenBase GitHub Wiki
The BananenBase has different modules, but sometimes you want to make your own module.
Modules are, just like commands, in classes. Start by making a file. In this example, we call it logger
.
The constructor is needed for the module name, dependencies, configurations and priority.
The easiest way to make your own module, is by using the Module class from the BananenBase. It will make sure that configuration is done correctly, for the BananenBase itself and for other users with the module options.
const BananenBase = require("bananenbase");
class MyModule extends BananenBase.module {
constructor() {
super({
// Options
});
}
}
module.exports = MyModule;
This is the name of your module. An instance of your class will be on BananenBase.modules.<NAME>
. This is the only required field and it's a string.
You're name can't contain any uppercase letters!
The next option is toConfigure
. Users can add configuration options to your module.
For each key that you want to configure, add a new key in the toConfigure
object. The value is a string with requirements for that key. Firstly, set if the key is required
or optional
. Then set a point (.
). After that, set the type it needs to be. The BananenBase will check with Node's typeof
function.
Examples
- Named
options
, it's required and it needs to be a string.
option: "required.string"
- Named
pizza
, it's required and it needs to be an object.
pizza: "required.object"
- Named
files
, it's optional and it needs to be a number.
files: "optional.number"
When your module requires dependencies, you can add the NPM package names in an array called dependencies
. The BananenBase will install these dependencies if they aren't installed!
Warning: Require your dependencies in other functions in your module, don't load them in your constructor or before you make your class!
Lastly, you can set a priority. It needs to be between 1 and 10. One (default) is last and 10 is first. Use this wisely!
super({
name: "MyModule", // Name is "MyModule"
toConfigure: {
files: "required.string" // Option "files" is required and needs to be a string
},
dependencies: ["keyv", "@keyv/sqlite"], // Uses "keyv" and "@keyv/sqlite" as dependencies
priority: 2 // Priority of 2, so the code gets executed before the base packages
});
Like I sad earlier, you can add configuration options to your module. These will be set on this.options
. After the configuration is done, the method afterConfigure
is called in your module.
If your module requires dependencies, but they aren't installed,
afterConfigure
will be called when the dependencies are installed too.
Example
class MyModule /* Stuff*/ {
constructor() { /* Stuff */ }
afterConfigure() {
// this.options > Working
// Dependencies named in constructor are installed!
}
}
Some modules want to check if a user has special permissions to continue. If that is the case, return false
if the user can't go further!
With OnMessage
, there are two parameters.
- The message object
- If the bot is going further with executing the message listener.
If another module returnedfalse
, this will befalse
. Otherwise, it will betrue
.
If you're checking something (permissions etc), be sure to first check if the user won't go further, because you don't want to waste resources.
For command
methods, there are three.
- The message object
- The command object
- If the bot is going further with executing the message listener.
If a commands has specific settings for a module (Read about it here), each setting will be on command.arguments.<OPTION>
.
If you want to do something when the bot receives a message, for example changing the message object, use the onMessage
method.
class MyModule /* Stuff*/ {
constructor() { /* Stuff */ }
onMessage(message, continues) {
// Do something!
// Return "false" if the bot shouldn't go further.
}
}
Before a command is executed, but the BananenBase knows the command, the method beforeCommandExecute
is called. For example, when you're making a security module, you can use this to check if the command may be executed by that user.
class MyModule /* Stuff*/ {
constructor() { /* Stuff */ }
beforeCommandExecute(message, command, continues) {
// Do something!
// Return "false" if the bot shouldn't go further.
}
}
After a command is executed, the method afterCommandExecute
is called.
class MyModule /* Stuff*/ {
constructor() { /* Stuff */ }
afterCommandExecute(message, command) {
// Do something!
}
}
If you want to listen to client events with your module, you can do that in the AfterConfiguration method by adding this.
this.BananenBase.client.on("EVENT", (...args) => {
// Do stuff!
});
If you thing your module needs to be in the BananenBase, you can create a pull request on this github page!
You can also make a NPM package where you export your class. This is easy for users. Be sure to write a small documentation for your module!
Other users can load your module with bot.addModule(MyModule);
(MyModule
is the class name of your module)!