From 1d075e5a2c8dbe43e4c1632dc56c03c540bd1db6 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Fri, 23 Oct 2020 18:02:11 +0100 Subject: [PATCH 1/3] Fixed bug where starting the client would sometimes crash due to invalid variable setup --- .gitignore | 5 ++++- src/type/command.js | 9 ++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d15602c..e10fe7f 100644 --- a/.gitignore +++ b/.gitignore @@ -107,4 +107,7 @@ dist commands/ events/ bot.js -config.json \ No newline at end of file +config.json + +# Linux Environment Files +*.swp diff --git a/src/type/command.js b/src/type/command.js index a6b098b..17f2c75 100644 --- a/src/type/command.js +++ b/src/type/command.js @@ -1,6 +1,9 @@ class command { constructor(run) { this.run = run; + + this._roles = []; + this._requiredConfigs = []; } // Description @@ -31,8 +34,6 @@ class command { } // Roles - _roles = []; - get roles() { return this._roles; } @@ -42,8 +43,6 @@ class command { } // Config - _requiredConfigs = []; - get requiredConfigs() { return this._requiredConfigs; } @@ -53,4 +52,4 @@ class command { } } -module.exports = command; \ No newline at end of file +module.exports = command; From bf09d9bed00abadc0ec626f6d4e6eec71a9ddb75 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Fri, 23 Oct 2020 18:17:28 +0100 Subject: [PATCH 2/3] Changed commands and events to be an array, allowing multiple folders --- README.md | 12 ++++++--- src/client/client.js | 6 ++--- src/client/util.js | 60 +++++++++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b22bceb..168c922 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,19 @@ Copy the config template file and fill in the strings. { "token": "", "prefix": "", - "commands": "", - "events": "" + "commands": [ + "" + ], + "events": [ + "" + ] } ``` * **Token:** Your bot's token * **Prefix** The command prefix -* **Commands:** The folder name containing your commands -* **Events:** The folder name containing your events +* **Commands:** An array of the folders which contain your commands +* **Events:** An array of the folders which contain your events ## Usage diff --git a/src/client/client.js b/src/client/client.js index 30f1a2d..74e75f4 100644 --- a/src/client/client.js +++ b/src/client/client.js @@ -33,14 +33,14 @@ class client extends Client { 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.commands != "string") throw "Commands is not a string"; + if (typeof config.commands != "object") throw "Commands is not a string"; if (!existsSync(`${process.cwd()}/${config.commands}`)) throw "Commands folder doesn't exist"; - if (typeof config.events != "string") throw "Events is not a string"; + if (typeof config.events != "object") throw "Events is not a string"; if (!existsSync(`${process.cwd()}/${config.events}`)) throw "Events folder doesn't exist"; this._config = config; } } -module.exports = client; \ No newline at end of file +module.exports = client; diff --git a/src/client/util.js b/src/client/util.js index 53a9a15..25d0540 100644 --- a/src/client/util.js +++ b/src/client/util.js @@ -7,38 +7,42 @@ class util { } loadCommand(name, args, message) { - stat(`${process.cwd()}/${this._client.config.commands}/${name}.js`, (err, stat) => { - if (err == null) { - let commandFile = require(`${process.cwd()}/${this._client.config.commands}/${name}.js`); - let command = new commandFile(); + for (let c = 0; c < this._client.config.commands.length; c++) { + let folder = this._client.config.commands[c]; + + stat(`${process.cwd()}/${this._client.config.commands}/${name}.js`, (err, stat) => { + if (err == null) { + let commandFile = require(`${process.cwd()}/${this._client.config.commands}/${name}.js`); + let command = new commandFile(); - let requiredConfigs = command.requiredConfigs; + let requiredConfigs = command.requiredConfigs; - for (let i = 0; i < requiredConfigs.length; i++) { - if (!this._client.config[name]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`; - if (!this._client.config[name][requiredConfigs[i]]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`; - } + for (let i = 0; i < requiredConfigs.length; i++) { + if (!this._client.config[name]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`; + if (!this._client.config[name][requiredConfigs[i]]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`; + } - let requiredRoles = command.roles; + let requiredRoles = command.roles; - for (let i = 0; i < requiredRoles.length; i++) { - if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { - message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); - return; - } - } + for (let i = 0; i < requiredRoles.length; i++) { + if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { + message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); + return; + } + } - command[command.run]({ - "command": name, - "arguments": args, - "client": this._client, - "message": message, - "config": config - }); - } else if (err.code === 'ENOENT') { - // FILE DOESN'T EXIST - } - }); + command[command.run]({ + "command": name, + "arguments": args, + "client": this._client, + "message": message, + "config": config + }); + } else if (err.code === 'ENOENT') { + // FILE DOESN'T EXIST + } + }); + } } loadEvents() { @@ -55,4 +59,4 @@ class util { } } -module.exports = util; \ No newline at end of file +module.exports = util; From 64a58baef4fde37af18ecd0993b53d945585b5ea Mon Sep 17 00:00:00 2001 From: Vylpes Date: Fri, 23 Oct 2020 18:27:39 +0100 Subject: [PATCH 3/3] Updated docs --- docs/index.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/index.md b/docs/index.md index 78934c1..46e98ab 100644 --- a/docs/index.md +++ b/docs/index.md @@ -40,8 +40,12 @@ An example configuration: { "token": "", "prefix": "!", - "commands": "commands", - "events": "events" + "commands": [ + "commands" + ], + "events": [ + "events" + ] } ``` @@ -125,4 +129,4 @@ module.exports = message; 2. In the `constructor()` you need to call `super(run)`, replacing `run` with a string of the name of the method which will run when the event gets triggered. 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. \ No newline at end of file +> **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.