modmail/structure/Registry.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-06-18 15:41:57 +02:00
const { Collection } = require("discord.js");
const path = require('path');
const fs = require('fs');
class Registry {
2021-10-22 09:35:04 +02:00
constructor (client) {
2021-06-18 15:41:57 +02:00
this.client = client;
this.commands = new Collection();
}
2021-10-22 09:35:04 +02:00
find (name) {
2021-06-19 15:06:20 +02:00
2021-06-20 13:12:23 +02:00
return this.commands.find((c) => c.name === name.toLowerCase() || c.aliases?.includes(name.toLowerCase()));
2021-06-19 15:06:20 +02:00
}
2021-10-22 09:35:04 +02:00
loadCommands () {
2021-06-18 15:41:57 +02:00
const commandsDir = path.join(process.cwd(), 'structure', 'commands');
const files = fs.readdirSync(commandsDir);
for (const file of files) {
const commandPath = path.join(commandsDir, file);
const commandClass = require(commandPath);
if (typeof commandClass !== 'function') {
delete require.cache[commandPath];
continue;
}
const command = new commandClass(this.client);
2022-01-03 19:23:55 +01:00
if (this.commands.has(command.name)) this.client.logger.warn(`Command by name ${command.name} already exists, skipping duplicate at path ${commandPath}`);
2021-06-18 15:41:57 +02:00
else this.commands.set(command.name, command);
}
}
}
module.exports = Registry;