Merge branch 'feature/config-file' into 'develop'

Command configs are now in their own file

See merge request Vylpes/vylbot-core!29
This commit is contained in:
Vylpes 2020-12-23 22:05:23 +00:00
commit 7840bc9be8
4 changed files with 67 additions and 9 deletions

2
.gitignore vendored
View file

@ -111,3 +111,5 @@ config.json
# Linux Environment Files # Linux Environment Files
*.swp *.swp
cmdconfig.json

View file

@ -8,6 +8,7 @@ Welcome to the VylBot Core documentation. In this file we will explain how to se
2. Configuring the client 2. Configuring the client
3. Creating a command 3. Creating a command
4. Handling an event 4. Handling an event
5. Configuring command configs
## 1. Initial Setup ## 1. Initial Setup
@ -45,11 +46,13 @@ An example configuration:
], ],
"events": [ "events": [
"events" "events"
] ],
"cmdconfig": "cmdconfig.json"
} }
``` ```
Make sure the folders that you set for `commands` and `events` exist and are at your project's current working directory. - Make sure the folders that you set for `commands` and `events` exist and are at your project's current working directory.
- Make sure the file set in `cmdconfig` exists and contains json data for the commands (see below)
## 3. Creating a command ## 3. Creating a command
@ -64,7 +67,7 @@ class test extends command {
super("test"); super("test");
super.description = "Test description"; super.description = "Test description";
super.category = "general"; super.category = "general";
super.requiredConfigs = "link"; super.configs = "link";
super.roles = "Moderator"; super.roles = "Moderator";
super.roles = "Admin"; super.roles = "Admin";
super.users = "<ID-HERE>"; super.users = "<ID-HERE>";
@ -89,9 +92,9 @@ module.exports = test;
> **Note:** You can set more than one user to be required by settubg `super.users` again. > **Note:** You can set more than one user to be required by settubg `super.users` again.
6. If you want the command to require a variable in the config, set the name in `super.requiredConfigs` 6. If you want the command to require a variable in the config, set the name in `super.configs`
> **Note:** You can set more than one role to be required by setting `super.requiredConfigs` again. > **Note:** You can set more than one role to be required by setting `super.configs` again.
7. Create a method using the name you set in `super(run)`, with the `context` as its parameter. 7. Create a method using the name you set in `super(run)`, with the `context` as its parameter.
@ -107,7 +110,7 @@ The `context` parameter will be a JSON object of:
} }
``` ```
The command's config will be stored under the command's name, for example, if a command called `test` has set `super.requiredConfigs = "link"`, it will be set in the config as `test.link`. The command's config will be stored under the command's name, for example, if a command called `test` has set `super.configs = "link"`, it will be set in the config as `test.link`.
## 4. Handling an event ## 4. Handling an event
@ -135,3 +138,47 @@ module.exports = message;
3. Create a method using the name you set in `super(run)`, with the parameters being as per your event's paramaters in the discord.js documentation. 3. Create a method using the name you set in `super(run)`, with the parameters being as per your event's paramaters in the discord.js documentation.
> **Note:** The name of the event file will determine what event it will be triggered on. For example, if you want to have an event trigger everytime a message is sent, put into your event folder a file called `message.js` and follow the steps above. > **Note:** The name of the event file will determine what event it will be triggered on. For example, if you want to have an event trigger everytime a message is sent, put into your event folder a file called `message.js` and follow the steps above.
## 5. Configuring command configs
The command config json (specified in the main config's `cmdconfig` variable) contiains all the configuration set for the commands.
The syntax is as follows:
```json
{
"Command": {
"Variable": "Value"
}
}
```
- **Command:** The name of the command, such as `ban.js` command would be `ban`.
- **Variable:** The variable name set in the command, this would be one for every `super.configs` in the command's constructor.
- **Value:** The value for the command.
An example would be:
```js
// ban.js
class ban extends Command {
constructor() {
super("ban");
super.configs = "modrole";
// ...
}
// ...
}
```
Would have in the command config file:
```json
{
"ban": {
"modrole": "Moderators"
}
}
```

View file

@ -46,10 +46,15 @@ class client extends Client {
if (typeof config.token != "string") throw "Token is not a string"; if (typeof config.token != "string") throw "Token is not a string";
if (typeof config.prefix != "string") throw "Prefix is not a string"; if (typeof config.prefix != "string") throw "Prefix is not a string";
// Make sure the command config string is set and the file exists
if (typeof config.cmdconfig != "string") throw "Cmdconfig is not a string";
if (!existsSync(config.cmdconfig)) throw `The file '${config.cmdconfig}' does not exist`;
// Make sure commands and events are arrays, each item inside will be validated later // Make sure commands and events are arrays, each item inside will be validated later
if (typeof config.commands != "object") throw "Commands is not a string"; if (typeof config.commands != "object") throw "Commands is not a string";
if (typeof config.events != "object") throw "Events is not a string"; if (typeof config.events != "object") throw "Events is not a string";
this._config = config; this._config = config;
} }
} }

View file

@ -1,6 +1,5 @@
// Required Components // Required Components
const { stat, readdirSync } = require('fs'); const { stat, readdirSync } = require('fs');
const { config } = require('process');
// Util Class // Util Class
class util { class util {
@ -25,14 +24,19 @@ class util {
let commandFile = require(`${process.cwd()}/${folder}/${name}.js`); let commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
let command = new commandFile(); let command = new commandFile();
// Require the command config file and get the config for the current command
let configString = this._client.config.cmdconfig
let configFile = require(`${process.cwd()}/${configString}`);
let config = configFile[name];
// Get the list of required configurations the command needs // Get the list of required configurations the command needs
let commandConfigs = command.configs; let commandConfigs = command.configs;
// Loop through all the required configs of the command // Loop through all the required configs of the command
for (let i = 0; i < commandConfigs.length; i++) { for (let i = 0; i < commandConfigs.length; i++) {
// If the command doesn't have the configs in the config string, throw an error // If the command doesn't have the configs in the config string, throw an error
if (!this._client.config[name]) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`; if (!config) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`;
if (!this._client.config[name][commandConfigs[i]]) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`; if (!config[commandConfigs[i]]) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`;
} }
// Get the roles required for this command to run // Get the roles required for this command to run