From 5a0b4201f4acd9f6a8cd5241ec58e7537b1c0a57 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Tue, 15 Jul 2025 20:51:12 -0500 Subject: [PATCH] reload commands --- .idea/discord.xml | 2 +- bot/runtime/commands/dev/reload.js | 18 +++++++++--------- bot/runtime/events/messageCreate.js | 4 ++-- bot/startup/collect/commands.js | 20 +++++++++++--------- util/log/log.js | 2 +- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/.idea/discord.xml b/.idea/discord.xml index 30bab2a..d8e9561 100644 --- a/.idea/discord.xml +++ b/.idea/discord.xml @@ -1,7 +1,7 @@ - \ No newline at end of file diff --git a/bot/runtime/commands/dev/reload.js b/bot/runtime/commands/dev/reload.js index 209ddc0..fa61d53 100644 --- a/bot/runtime/commands/dev/reload.js +++ b/bot/runtime/commands/dev/reload.js @@ -3,7 +3,7 @@ const fs = require('fs'); const chalk = require('chalk'); const ora = require('ora'); -const {EmbedBuilder} = require("discord.js"); +const {EmbedBuilder, Collection} = require("discord.js"); module.exports = { name: "reload", @@ -18,16 +18,16 @@ module.exports = { category: 'Developer', info: "Refresh all client commands and events and clear most of the require cache.", syntax: '`reload`', - async execute(client, message, args, cmd) { - if (!client.config.developers.has(message.author.id)) {return message.channel.send(`You must be a ${client.config.bot.name} developer in order to do this!`);} + async run(client, message, args, cmd) { + if (!client.config.developers.includes(message.author.id)) {return message.channel.send(`You must be a ${client.config.bot.name} developer in order to do this!`);} - if (!args.length) { + //if (!args.length) { let timer = new Date().getTime(); - let commands = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); - let dirSet = new Map(); - fs.readdirSync('./commands').filter(file => !file.includes('.')).forEach(dir => fs.readdirSync(`./commands/${dir}`).filter(file => file.endsWith('.js')).forEach(x => {commands.push(x); dirSet.set(x, dir)})); client.warn(`${chalk.gray('Reload:')} ${chalk.white('All commands and events are being reloaded!')}\``); - client.log(`Developer ${message.author.username} initiated the system refresh`, {color: 'ff4fd0', source: "info"}, 0, 1); - } + client.log(`Developer ${chalk.white(message.author.username)} initiated the system refresh`, {color: '#ff4fd0', source: "info"}, 0, 1); + client.commands = new Collection(); + await require('../../../startup/collect/commands')(client, true); //TODO reload events + return message.channel.send(`Done! Reloaded **${client.commands.size}** commands with **${client.aliases.size}** aliases in **${new Date().getTime() - timer}ms**`) + //} } }; \ No newline at end of file diff --git a/bot/runtime/events/messageCreate.js b/bot/runtime/events/messageCreate.js index 6dff18a..0cfc345 100644 --- a/bot/runtime/events/messageCreate.js +++ b/bot/runtime/events/messageCreate.js @@ -33,6 +33,6 @@ module.exports = async (client, message) => { let cmdToRun = client.commands.get(cmd.name) || client.commands.get(client.aliases.get(cmd.name)); if (!cmdToRun) {return;} - try {cmdToRun.run(client, message, args, cmd).catch(e => client.error(`There was an error in the ${cmdToRun.name} command.`, 0, 1, e, '\n'));} - catch (e) {client.error(`There was an error in the ${cmdToRun.name} command.`, 0, 1, e, '\n');} + try {cmdToRun.run(client, message, args, cmd).catch(e => client.error(`There was an error in the ${cmdToRun.name} command.`, 0, 0, 1, e, '\n'));} + catch (e) {client.error(`There was an error in the ${cmdToRun.name} command.`, 0, 0, 1, e, '\n');} }; \ No newline at end of file diff --git a/bot/startup/collect/commands.js b/bot/startup/collect/commands.js index 00d7190..25e8a1e 100644 --- a/bot/startup/collect/commands.js +++ b/bot/startup/collect/commands.js @@ -4,13 +4,13 @@ const {Collection} = require('discord.js'); const commandsDirName = './bot/runtime/commands'; -module.exports = async client => { +module.exports = async (client, silent) => { client.aliases = new Collection(); //THIS SHIT WAS ALREADY FUCKING EMPTY let dirErrors = []; let fileErrors = []; //collect error objects to output them all at the end let readDirs = []; //list of dirs to print at the end let conflictingAliases = {}; - client.log('Loading commands...', {source: 'boot', color: 'blue'}, 0, 1); + if (!silent) {client.log('Loading commands...', {source: 'boot', color: 'blue'}, 0, 1);} const readDir = dir => { //when you're trying to comment your code but realize you have good variable naming for once and don't need to :D let dirRead; try {dirRead = fs.readdirSync(dir);} @@ -22,16 +22,18 @@ module.exports = async client => { let folders = dirRead.filter(item => fs.lstatSync(`${dir}/${item}`).isDirectory()); files.forEach(file => { try { + //in the event that this collector is being re-run, purge require's cache so that command updates will register + if (Object.keys(require.cache).includes(require.resolve(`../../../${dir.slice(2)}/${file}`))) {delete require.cache[require.resolve(`../../../${dir.slice(2)}/${file}`)]} const command = require(`../../../${dir.slice(2)}/${file}`); client.commands.set(command.name, command); if (command.aliases) {command.aliases.forEach(alias => { if (client.aliases.has(alias)) { - if (conflictingAliases[alias]) {conflictingAliases[alias].push(command.name);} //object of alias conflictions keyed by alias -> array[command names] + if (conflictingAliases[alias]) {conflictingAliases[alias].push(command.name);} //object of alias conflicts keyed by alias -> array[command names] else {conflictingAliases[alias] = [command.name];} } client.aliases.set(alias, command.name); });} - client.log(`Loaded ${chalk.white(command.name)} with ${chalk.white(`${command.aliases ? command.aliases.length : 0}`)} alias${command.aliases && command.aliases.length === 1 ? '' : 'es'}.`, {color: 'blueBright', source: 'boot', sourceColor: 'blue'}); + if (!silent) {client.log(`Loaded ${chalk.white(command.name)} with ${chalk.white(`${command.aliases ? command.aliases.length : 0}`)} alias${command.aliases && command.aliases.length === 1 ? '' : 'es'}.`, {color: 'blueBright', source: 'boot', sourceColor: 'blue'});} } catch (e) { client.error(`Failed to read file ${chalk.white(file)}`); @@ -39,15 +41,15 @@ module.exports = async client => { } }); readDirs.push(`${dir.split('/').slice(4).join('/')}/`); // "commands/..." - return folders.forEach(folder => readDir(`${dir}/${folder}`)); //recurse infinitely + return folders.forEach(folder => readDir(`${dir}/${folder}`)); //recurse directories infinitely }; readDir(commandsDirName); - console.log(""); + if (!silent) {console.log("");} dirErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); fileErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); - readDirs.forEach(dir => client.log(`Read from directory ${chalk.green(dir)}`, {source: 'proc'})); - console.log(''); + if (!silent) {readDirs.forEach(dir => client.log(`Read from directory ${chalk.green(dir)}`, {source: 'proc'}));} + if (!silent) {console.log('');} Object.keys(conflictingAliases).forEach(alias => client.warn(`Alias ${chalk.white(alias)} appears on ${client.utils.as(conflictingAliases[alias].length, 'command')} ${chalk.white(conflictingAliases[alias].join(chalk.yellow(', ')))}`)); - if (Object.keys(conflictingAliases).length) {console.log('')}; + if (Object.keys(conflictingAliases).length) {console.log('');} client.log(`Loaded ${chalk.white(client.commands.size)} command${client.utils.s(client.commands.size)}!`, {color: 'blue', source: 'boot'}, 0, 1); }; \ No newline at end of file diff --git a/util/log/log.js b/util/log/log.js index 1de2b49..a07fbcb 100644 --- a/util/log/log.js +++ b/util/log/log.js @@ -26,7 +26,7 @@ const tlog = (client) => (message = "Test Log", options = {}, prenl = false, pos client.config.logLevel = getLevel(client.config.logLevel); if (client.config.logLevel < opt.level) {return;} } - console.log(`${prenl ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${options.nc || options.noColor ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${postnl ? '\n' : ''}`, ...multcons); + console.log(`${prenl ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${(options.nc || options.noColor) ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${postnl ? '\n' : ''}`, ...multcons); }; module.exports = (client) => {