From 19c4e1df3fd90b8a7e4738a5d30088c8c9056d02 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Tue, 20 Oct 2020 10:45:24 +0100 Subject: [PATCH] Initial work --- .gitignore | 6 +++ package-lock.json | 107 ++++++++++++++++++++++++++++++++++++++++++ package.json | 14 ++++++ src/client/client.js | 46 ++++++++++++++++++ src/client/events.js | 21 +++++++++ src/client/util.js | 58 +++++++++++++++++++++++ src/index.js | 7 +++ src/package-lock.json | 107 ++++++++++++++++++++++++++++++++++++++++++ src/package.json | 14 ++++++ src/type/command.js | 56 ++++++++++++++++++++++ src/type/event.js | 7 +++ 11 files changed, 443 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/client/client.js create mode 100644 src/client/events.js create mode 100644 src/client/util.js create mode 100644 src/index.js create mode 100644 src/package-lock.json create mode 100644 src/package.json create mode 100644 src/type/command.js create mode 100644 src/type/event.js diff --git a/.gitignore b/.gitignore index 6704566..d15602c 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,9 @@ dist # TernJS port file .tern-port + +# VylBot-Core Testing Files +commands/ +events/ +bot.js +config.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7e1c9a7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,107 @@ +{ + "name": "vylbot-core", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@discordjs/collection": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", + "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==" + }, + "@discordjs/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "requires": { + "event-target-shim": "^5.0.0" + } + }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, + "discord.js": { + "version": "12.3.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.3.1.tgz", + "integrity": "sha512-mSFyV/mbvzH12UXdS4zadmeUf8IMQOo/YdunubG1wWt1xjWvtaJz/s9CGsFD2B5pTw1W/LXxxUbrQjIZ/xlUdw==", + "requires": { + "@discordjs/collection": "^0.1.6", + "@discordjs/form-data": "^3.0.1", + "abort-controller": "^3.0.0", + "node-fetch": "^2.6.0", + "prism-media": "^1.2.2", + "setimmediate": "^1.0.5", + "tweetnacl": "^1.0.3", + "ws": "^7.3.1" + } + }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" + }, + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + }, + "mime-types": { + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "requires": { + "mime-db": "1.44.0" + } + }, + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + }, + "prism-media": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.2.tgz", + "integrity": "sha512-I+nkWY212lJ500jLe4tN9tWO7nRiBAVdMv76P9kffZjYhw20raMlW1HSSvS+MLXC9MmbNZCazMrAr+5jEEgTuw==" + }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" + }, + "tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + }, + "ws": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..813b7a5 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "vylbot-core", + "version": "1.0.0", + "description": "A discord client based upon discord.js", + "main": "./src/index", + "scripts": { + "test": "echo \"No Tests Specified\"" + }, + "author": "Vylpes", + "license": "ISC", + "dependencies": { + "discord.js": "^12.3.1" + } +} diff --git a/src/client/client.js b/src/client/client.js new file mode 100644 index 0000000..30f1a2d --- /dev/null +++ b/src/client/client.js @@ -0,0 +1,46 @@ +const { Client } = require('discord.js'); +const { existsSync } = require('fs'); + +const events = require('./events'); +const util = require('./util'); + +class client extends Client { + constructor(config) { + super(); + this.config = config; + this.events = new events(); + this.util = new util(this); + } + + start() { + super.on("message", this.events.message); + super.on("ready", this.events.ready); + super.login(this._config.token); + + this.util.loadEvents(); + } + + // Config + get config() { + return this._config; + } + + set config(config) { + // Validate the config + if (this._config) throw "Config has already been set"; + if (typeof config != "object") throw "Config is not a JSON object"; + + 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 (!existsSync(`${process.cwd()}/${config.commands}`)) throw "Commands folder doesn't exist"; + + if (typeof config.events != "string") 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 diff --git a/src/client/events.js b/src/client/events.js new file mode 100644 index 0000000..9eabab9 --- /dev/null +++ b/src/client/events.js @@ -0,0 +1,21 @@ +class event { + message(message) { + if (!message.guild) return; + if (message.author.bot) return; + + let prefix = this.config.prefix; + + if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) { + let args = message.content.substring(prefix.length).split(" "); + let name = args.shift(); + + this.util.loadCommand(name, args, message); + } + } + + ready() { + console.log("Ready"); + } +} + +module.exports = event; \ No newline at end of file diff --git a/src/client/util.js b/src/client/util.js new file mode 100644 index 0000000..53a9a15 --- /dev/null +++ b/src/client/util.js @@ -0,0 +1,58 @@ +const { stat, readdirSync } = require('fs'); +const { config } = require('process'); + +class util { + constructor(client) { + this._client = client; + } + + 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(); + + 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`; + } + + 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; + } + } + + command[command.run]({ + "command": name, + "arguments": args, + "client": this._client, + "message": message, + "config": config + }); + } else if (err.code === 'ENOENT') { + // FILE DOESN'T EXIST + } + }); + } + + loadEvents() { + let eventFiles = readdirSync(`${process.cwd()}/${this._client.config.events}/`); + + for (let i = 0; i < eventFiles.length; i++) { + let eventName = eventFiles[i].split('.')[0]; + let file = require(`${process.cwd()}/${this._client.config.events}/${eventName}.js`); + + let event = new file; + + this._client.on(eventName, event[event.run]); + } + } +} + +module.exports = util; \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..11f6140 --- /dev/null +++ b/src/index.js @@ -0,0 +1,7 @@ +const discord = require('discord.js'); + +module.exports = { + client: require('./client/client'), + command: require('./type/command'), + event: require('./type/event') +} \ No newline at end of file diff --git a/src/package-lock.json b/src/package-lock.json new file mode 100644 index 0000000..7e1c9a7 --- /dev/null +++ b/src/package-lock.json @@ -0,0 +1,107 @@ +{ + "name": "vylbot-core", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@discordjs/collection": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", + "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==" + }, + "@discordjs/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "requires": { + "event-target-shim": "^5.0.0" + } + }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, + "discord.js": { + "version": "12.3.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.3.1.tgz", + "integrity": "sha512-mSFyV/mbvzH12UXdS4zadmeUf8IMQOo/YdunubG1wWt1xjWvtaJz/s9CGsFD2B5pTw1W/LXxxUbrQjIZ/xlUdw==", + "requires": { + "@discordjs/collection": "^0.1.6", + "@discordjs/form-data": "^3.0.1", + "abort-controller": "^3.0.0", + "node-fetch": "^2.6.0", + "prism-media": "^1.2.2", + "setimmediate": "^1.0.5", + "tweetnacl": "^1.0.3", + "ws": "^7.3.1" + } + }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" + }, + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + }, + "mime-types": { + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "requires": { + "mime-db": "1.44.0" + } + }, + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + }, + "prism-media": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.2.tgz", + "integrity": "sha512-I+nkWY212lJ500jLe4tN9tWO7nRiBAVdMv76P9kffZjYhw20raMlW1HSSvS+MLXC9MmbNZCazMrAr+5jEEgTuw==" + }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" + }, + "tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + }, + "ws": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" + } + } +} diff --git a/src/package.json b/src/package.json new file mode 100644 index 0000000..813b7a5 --- /dev/null +++ b/src/package.json @@ -0,0 +1,14 @@ +{ + "name": "vylbot-core", + "version": "1.0.0", + "description": "A discord client based upon discord.js", + "main": "./src/index", + "scripts": { + "test": "echo \"No Tests Specified\"" + }, + "author": "Vylpes", + "license": "ISC", + "dependencies": { + "discord.js": "^12.3.1" + } +} diff --git a/src/type/command.js b/src/type/command.js new file mode 100644 index 0000000..a6b098b --- /dev/null +++ b/src/type/command.js @@ -0,0 +1,56 @@ +class command { + constructor(run) { + this.run = run; + } + + // Description + get description() { + return this._description; + } + + set description(description) { + this._description = description; + } + + // Category + get category() { + return this._category; + } + + set category(category) { + this._category = category; + } + + // Usage + get usage() { + return this._usage; + } + + set usage(usage) { + this._usage = usage; + } + + // Roles + _roles = []; + + get roles() { + return this._roles; + } + + set roles(role) { + this._roles.push(role); + } + + // Config + _requiredConfigs = []; + + get requiredConfigs() { + return this._requiredConfigs; + } + + set requiredConfigs(conf) { + this._requiredConfigs.push(conf); + } +} + +module.exports = command; \ No newline at end of file diff --git a/src/type/event.js b/src/type/event.js new file mode 100644 index 0000000..49de514 --- /dev/null +++ b/src/type/event.js @@ -0,0 +1,7 @@ +class event { + constructor(run) { + this.run = run; + } +} + +module.exports = event; \ No newline at end of file