Caching improvements
This commit is contained in:
@@ -4,29 +4,43 @@ const path = require('path');
|
||||
const logger = require('../../modules/colorfulLogger');
|
||||
const { commandPrefix } = require('../../config/constants');
|
||||
const { getNameById } = require('../utils/discordUtils');
|
||||
const cacheManager = require('../utils/cacheManager');
|
||||
|
||||
function loadLegacyCommands(client) {
|
||||
client.legacyCommands = new Collection();
|
||||
function loadCommands() {
|
||||
const slashCommands = new Collection();
|
||||
const legacyCommands = new Collection();
|
||||
const commandFolders = fs.readdirSync(path.join(__dirname, '..', 'commands'));
|
||||
|
||||
for (const folder of commandFolders) {
|
||||
const commandFiles = fs.readdirSync(path.join(__dirname, '..', 'commands', folder)).filter(file => file.endsWith('.js'));
|
||||
for (const file of commandFiles) {
|
||||
try {
|
||||
const command = require(path.join(__dirname, '..', 'commands', folder, file));
|
||||
|
||||
const filePath = path.join(__dirname, '..', 'commands', folder, file);
|
||||
const command = require(filePath);
|
||||
|
||||
// Load slash command part
|
||||
if (command.data && command.execute) {
|
||||
slashCommands.set(command.data.name, command);
|
||||
}
|
||||
|
||||
// Load legacy command part
|
||||
if (command.legacy) {
|
||||
const legacyCommand = command.legacy;
|
||||
client.legacyCommands.set(legacyCommand.name, legacyCommand);
|
||||
if (legacyCommand.aliases && legacyCommand.aliases.length > 0) {
|
||||
legacyCommand.aliases.forEach(alias => client.legacyCommands.set(alias, legacyCommand));
|
||||
legacyCommands.set(command.legacy.name, command.legacy);
|
||||
if (command.legacy.aliases) {
|
||||
command.legacy.aliases.forEach(alias => {
|
||||
legacyCommands.set(alias, command.legacy);
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Error loading legacy command from ${file}:`, error);
|
||||
logger.error(`Error loading command from ${file}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cacheManager.set('slashCommands', slashCommands);
|
||||
cacheManager.set('legacyCommands', legacyCommands);
|
||||
logger.info('All commands loaded and cached.');
|
||||
}
|
||||
|
||||
async function handleCommand(message) {
|
||||
@@ -35,7 +49,13 @@ async function handleCommand(message) {
|
||||
const args = message.content.slice(commandPrefix.length).trim().split(/\s+/);
|
||||
const commandName = args.shift().toLowerCase();
|
||||
|
||||
const command = message.client.legacyCommands.get(commandName);
|
||||
const legacyCommands = cacheManager.get('legacyCommands');
|
||||
if (!legacyCommands) {
|
||||
logger.error('Legacy commands not found in cache.');
|
||||
return;
|
||||
}
|
||||
|
||||
const command = legacyCommands.get(commandName);
|
||||
|
||||
if (!command) return;
|
||||
|
||||
@@ -52,4 +72,4 @@ async function handleCommand(message) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { loadLegacyCommands, handleCommand };
|
||||
module.exports = { loadCommands, handleCommand };
|
||||
|
Reference in New Issue
Block a user