diff --git a/CHANGELOGS.md b/CHANGELOGS.md index d5ab67a..479fa81 100644 --- a/CHANGELOGS.md +++ b/CHANGELOGS.md @@ -1,5 +1,12 @@ # Changelogs +## V2.2 +- Removed - `reload-cmd` command +- Removed - `reload-event` command +- Added - `reload` command +- Added - Reload all commands function +- Added - Reload all events function + ## V2.1 - Optimised - Main.js (Reorganisation) - Added - `reload-cmd` command (Function in main.js (`client.reloadCommand(commandName)`)) diff --git a/commands/utils/reload-command.js b/commands/utils/reload-command.js deleted file mode 100644 index 4941bf7..0000000 --- a/commands/utils/reload-command.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -const Command = require("../../structure/Command.js"); - -class ReloadCommand extends Command { - constructor() { - super({ - name: 'reload-cmd', - category: 'utils', - description: 'reload-cmd Commands', - usage: 'reload-cmd ', - example: ['reload-cmd help','reload-cmd ping'], - perms: "owner" - }); - } - - async run(client, message, args) { - if(!args[1]) { - message.channel.send("> No command specified!"); - } else { - client.reloadCommand(args[1]).then(async res => { - await message.channel.send(res); - }); - } - } -} - -module.exports = new ReloadCommand; \ No newline at end of file diff --git a/commands/utils/reload-event.js b/commands/utils/reload-event.js deleted file mode 100644 index cae8f6d..0000000 --- a/commands/utils/reload-event.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -const Command = require("../../structure/Command.js"); - -class ReloadEvent extends Command { - constructor() { - super({ - name: 'reload-event', - category: 'utils', - description: 'Reload Events', - usage: 'reload ', - example: ['reload-event message','reload-event ready'], - perms: "owner" - }); - } - - async run(client, message, args) { - if(!args[1]) { - message.channel.send("> No event specified!"); - } else { - client.reloadEvent(args[1]).then(async res => { - await message.channel.send(res); - }); - } - } -} - -module.exports = new ReloadEvent; \ No newline at end of file diff --git a/commands/utils/reload.js b/commands/utils/reload.js new file mode 100644 index 0000000..ae403b7 --- /dev/null +++ b/commands/utils/reload.js @@ -0,0 +1,54 @@ +'use strict'; + +const Command = require("../../structure/Command.js"); + +class ReloadCommand extends Command { + constructor() { + super({ + name: 'reload', + category: 'owner', + description: 'reload files', + usage: 'reload <-all/-c/-e> ', + example: ['reload -c help','reload -c ping','reload -e message', 'reload -e ready', 'reload -all -c','reload -all -e','reload -all -all'], + perms: "owner", + aliases: ['rl'] + }); + } + + async run(client, message, args) { + if(!args[1]) { + message.channel.send("> No type specified!"); + } else if(args[1] === '-all') { + if(!args[2]) { + client.reloadAllCommands().then(res_c => { + client.reloadAllEvents().then(res_e => { + message.channel.send(`${res_c}\n${res_e}`) + }) + }) + } else if(args[2] === "-c") { + client.reloadAllCommands().then(res => { + message.channel.send(res) + }) + } else if(args[2] === "-e") { + client.reloadAllEvents().then(res => { + message.channel.send(res) + }) + } + } else if(!args[2]) { + message.channel.send("> No file specified!"); + } else { + if(args[1] === '-c') { + client.reloadCommand(args[2]).then(async res => { + await message.channel.send(res); + }); + } else if(args[1] === '-e') { + client.reloadEvent(args[2]).then(async res => { + await message.channel.send(res); + }); + } + + } + } +} + +module.exports = new ReloadCommand; \ No newline at end of file diff --git a/main.js b/main.js index b2fc769..f367937 100644 --- a/main.js +++ b/main.js @@ -74,6 +74,43 @@ class Class extends Client { resolve(`> Event named: \`${reload_event}\` not found`) }) } + this.reloadAllCommands = function() { + return new Promise((resolve) => { + let count = 0; + const folders = readdirSync(join(__dirname, "commands")); + for (let i = 0; i < folders.length; i++) { + const commands = readdirSync(join(__dirname, "commands", folders[i])); + count = count + commands.length; + for(const c of commands){ + try { + this.reloadCommand(c.replace('.js','')); + } catch (error) { + console.log(`${red('[Commands Reload]')} Failed to reload command ${c}: ${error.stack || error}`) + throw new Error(`${red('[Commands Reload]')} Failed to load event ${e}: ${error.stack || error}`) + } + } + } + console.log(`${green('[Commands Reload]')} Reloaded ${this.commands.size}/${count} commands`); + resolve(`> Reloaded \`${this.commands.size}\`/\`${count}\` commands`) + }) + } + this.reloadAllEvents = function() { + return new Promise((resolve) => { + let count = 0; + const files = readdirSync(join(__dirname, "events")); + files.forEach((e) => { + try { + count++; + const fileName = e.split('.')[0]; + this.reloadEvent(fileName); + } catch (error) { + throw new Error(`${red('[Events Reload]')} Failed to load event ${e}: ${error.stack || error}`) + } + }); + console.log(`${green('[Events Reload]')} Loaded ${count}/${files.length} events`); + resolve(`> Reloaded \`${count}\`/\`${files.length}\` events`) + }) + } try { this.launch().then(() => { console.log(blue('All is launched, Connecting to Discord..')); }) } catch (e) { diff --git a/package.json b/package.json index d073a8f..4255bd1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "handler-discord", - "version": "2..0", + "version": "2.2.0", "description": "Handler in Class for Discord.js", "main": "main.js", "scripts": {