diff --git a/commands/help.js b/commands/help.js index 662268b..e3601ce 100644 --- a/commands/help.js +++ b/commands/help.js @@ -5,9 +5,19 @@ const Discord = require('discord.js'); module.exports = { name: 'help', description: 'List all available commands.', - execute({message, args, prefix}) { + execute({ message, args, prefix }) { var commands = " " - const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + let commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + let x = 0 + if (args[0] == "netload") { + commandFiles = fs.readdirSync('./netload').filter(file => file.endsWith('.js')); + if (commandFiles.length == 0) { + message.channel.send("There are no netmodules currently loaded.") + x = 1; + } + + } + if (x == 1) return; const embed = new Discord.MessageEmbed() .setColor(15780145) @@ -20,12 +30,12 @@ module.exports = { const command = require(`./${file}`); - if(args[0] == "admin"){ + if (args[0] == "admin") { if (command.admin && !command.disabled) commands = commands + `${prefix}${command.name} | ${command.description}\n` - }else - if(!command.admin && !command.disabled) - commands = commands + `${prefix}${command.name} | ${command.description}\n` + } else + if (!command.admin && !command.disabled) + commands = commands + `${prefix}${command.name} | ${command.description}\n` } embed.addFields( { name: "General", value: commands }, diff --git a/commands/netload.js b/commands/netload.js new file mode 100644 index 0000000..3b5d0b0 --- /dev/null +++ b/commands/netload.js @@ -0,0 +1,64 @@ +const https = require('https'); +const fs = require('fs') +const netloadDir = "./netload" +const validUrl = require('valid-url'); + +module.exports = { + name: 'netload', + description: 'Load a module from the internet', + execute({ message, args, prefix, client, owners }) { + const json = fs.readFileSync('netmoduleWhitelist.json', 'utf8'); + let whitelist = JSON.parse(json) + if (json.indexOf(message.author.id.toString()) == -1) { + message.channel.send("You do not have permissions to use this command."); + return; + } + if (args[0] == "whitelist" && owners.indexOf(message.author.id.toString()) >= 0) { + whitelist.push(args[1]) + fs.writeFileSync("netmoduleWhitelist.json", JSON.stringify(whitelist)) + return; + } + if (!args[0] && message.attachments.size == 0) { + message.channel.send(`You have to either specify a url or upload a file via the command.\nTo get an example file, execute \`${prefix}netload example\``) + return; + + } if (args[0] == "example") { + message.channel.send({ files: [{ attachment: "./commands/.example" }] }) + return; + } + + + let url, fileName; + if (message.attachments.size > 0) { + url = message.attachments.first().url + fileName = message.attachments.first().name + } else { + url = args[0] + if (!validUrl.isUri(url)) { + message.channel.send("This does not look like a valid url") + return; + } + fileName = args[0].split("/")[args[0].split("/").length - 1] + } + if (fs.existsSync(`${netloadDir}/${fileName}`)) { + message.channel.send(`A module with this filename(${fileName}) already exists.`) + return; + } + https.get(url, (res) => { + res.on('data', (d) => { + fs.writeFileSync(`${netloadDir}/${fileName}`, d); + loadNetModules(client); + }); + + }).on('error', (e) => { + message.channel.send("Error download file."); + console.log(e) + }); + + let loadNetModules = require('../util/loadNetModules'); + loadNetModules(client) + + + } +}; + diff --git a/package-lock.json b/package-lock.json index 5a56174..aad065e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "discord.js": "^12.5.1", "express": "^4.17.1", "parse-ms": "^2.1.0", - "thesaurize": "^1.0.11" + "valid-url": "^1.0.9" } }, "node_modules/@discordjs/collection": { @@ -290,24 +290,6 @@ "node": ">= 0.6" } }, - "node_modules/fs-extra": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", - "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^4.0.0", - "universalify": "^0.1.0" - }, - "engines": { - "node": ">=6 <7 || >=8" - } - }, - "node_modules/graceful-fs": { - "version": "4.2.6", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", - "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==" - }, "node_modules/http-errors": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", @@ -347,19 +329,6 @@ "node": ">= 0.10" } }, - "node_modules/jsonfile": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", - "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", - "dependencies": { - "graceful-fs": "^4.1.6" - } - }, - "node_modules/lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" - }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -464,14 +433,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, - "node_modules/pluralize": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-7.0.0.tgz", - "integrity": "sha512-ARhBOdzS3e41FbkW/XWrTEtukqqLoK5+Z/4UeDaLuSW+39JPeFgs4gCGqsrJHVZX0fUrx//4OF0K1CUGwlIFow==", - "engines": { - "node": ">=4" - } - }, "node_modules/prism-media": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.5.tgz", @@ -589,22 +550,6 @@ "node": ">= 0.6" } }, - "node_modules/thesaurize": { - "version": "1.0.11", - "resolved": "https://registry.npmjs.org/thesaurize/-/thesaurize-1.0.11.tgz", - "integrity": "sha512-xrJ5ayc02wquyeTBQ8l1RlFo7YOBY0jjyKUJOYuHgoWtS1I9GKOoVWsaYV3GTsErvAokslYnvQ/a5G7vsmVXSg==", - "dependencies": { - "fs-extra": "^8.0.0", - "lodash": "^4.17.11", - "pluralize": "^7.0.0", - "thesaurus": "0.0.0" - } - }, - "node_modules/thesaurus": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/thesaurus/-/thesaurus-0.0.0.tgz", - "integrity": "sha1-CjszVvwdgYxsFsI/Vrp/PFolyQU=" - }, "node_modules/toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -630,14 +575,6 @@ "node": ">= 0.6" } }, - "node_modules/universalify": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", - "engines": { - "node": ">= 4.0.0" - } - }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -654,6 +591,11 @@ "node": ">= 0.4.0" } }, + "node_modules/valid-url": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", + "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -891,21 +833,6 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, - "fs-extra": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", - "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", - "requires": { - "graceful-fs": "^4.2.0", - "jsonfile": "^4.0.0", - "universalify": "^0.1.0" - } - }, - "graceful-fs": { - "version": "4.2.6", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", - "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==" - }, "http-errors": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", @@ -936,19 +863,6 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" }, - "jsonfile": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", - "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", - "requires": { - "graceful-fs": "^4.1.6" - } - }, - "lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" - }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -1020,11 +934,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, - "pluralize": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-7.0.0.tgz", - "integrity": "sha512-ARhBOdzS3e41FbkW/XWrTEtukqqLoK5+Z/4UeDaLuSW+39JPeFgs4gCGqsrJHVZX0fUrx//4OF0K1CUGwlIFow==" - }, "prism-media": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.5.tgz", @@ -1123,22 +1032,6 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, - "thesaurize": { - "version": "1.0.11", - "resolved": "https://registry.npmjs.org/thesaurize/-/thesaurize-1.0.11.tgz", - "integrity": "sha512-xrJ5ayc02wquyeTBQ8l1RlFo7YOBY0jjyKUJOYuHgoWtS1I9GKOoVWsaYV3GTsErvAokslYnvQ/a5G7vsmVXSg==", - "requires": { - "fs-extra": "^8.0.0", - "lodash": "^4.17.11", - "pluralize": "^7.0.0", - "thesaurus": "0.0.0" - } - }, - "thesaurus": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/thesaurus/-/thesaurus-0.0.0.tgz", - "integrity": "sha1-CjszVvwdgYxsFsI/Vrp/PFolyQU=" - }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -1158,11 +1051,6 @@ "mime-types": "~2.1.24" } }, - "universalify": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" - }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -1173,6 +1061,11 @@ "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" }, + "valid-url": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", + "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index aa23ffb..ccf8903 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "discord.js": "^12.5.1", "express": "^4.17.1", "parse-ms": "^2.1.0", - "thesaurize": "^1.0.11" + "valid-url": "^1.0.9" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/server.js b/server.js index 00f0f92..76450a7 100644 --- a/server.js +++ b/server.js @@ -12,10 +12,13 @@ const { client.commands = new Discord.Collection(); client.serverPrefixes = new Discord.Collection(); +client.netmodules = new Discord.Collection(); var reloadCommands = require("./util/reloadCommands.js"); const loadServerPrefixes = require('./util/loadServerPrefixes'); +const loadNetModules = require('./util/loadNetModules'); reloadCommands(client) +loadNetModules(client) client.once('ready', () => { console.log('Ready!'); @@ -48,13 +51,23 @@ client.on('message', async message => { const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName); + const netModule = client.netmodules.get(commandName); if (!message.guild) return; if (message.author.bot) return; if (!message.content.startsWith(prefix)) return; - if (!command) return; + if (!command){ + if (netModule){ + try { + netModule.execute({message: message, args: args, client: client, prefix: prefix}) + }catch(e){ + console.log(e) + } + } + return; + } if (command.admin && owners.indexOf(message.author.id.toString()) == -1) return; try { - command.execute({ message: message, args: args, client: client, prefix: prefix}) + command.execute({ message: message, args: args, client: client, prefix: prefix, owners: owners}) } catch (error) { console.log(`${error}\n-------`) } diff --git a/util/loadNetModules.js b/util/loadNetModules.js new file mode 100644 index 0000000..abb6fdc --- /dev/null +++ b/util/loadNetModules.js @@ -0,0 +1,19 @@ +const fs = require('fs') +const netloadDir = 'netload/' + + +module.exports = function (client) { + if (!fs.existsSync(netloadDir)) fs.mkdirSync(netloadDir); + let commandFiles = fs.readdirSync(netloadDir).filter(file => file.endsWith('.js')); + if (client.netmodules.size != 0) { + for (const i of commandFiles) { + delete require.cache[require.resolve(`../${netloadDir}${i}`)]; + } + } + client.netmodules.clear() + for (const file of commandFiles) { + const command = require(`../${netloadDir}${file}`); + client.netmodules.set(command.name, command); + } +} +