reload commands

master
Kit Kasune 2 weeks ago
parent fb7c7249d0
commit 5a0b4201f4
  1. 2
      .idea/discord.xml
  2. 18
      bot/runtime/commands/dev/reload.js
  3. 4
      bot/runtime/events/messageCreate.js
  4. 20
      bot/startup/collect/commands.js
  5. 2
      util/log/log.js

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

@ -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**`)
//}
}
};

@ -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');}
};

@ -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);
};

@ -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) => {

Loading…
Cancel
Save