DudeThatsErin

register.js

Jan 13th, 2021
736
21 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path');
  2. const fs = require('fs').promises;
  3. const BaseCommand = require('./BaseCommand.js');
  4. const BaseEvent = require('./BaseEvent.js');
  5.  
  6. async function registerCommands(client, dir = '') {
  7.     const filePath = path.join(__dirname, dir);
  8.     console.log(filePath);
  9.     const files = await fs.readdir(filePath);
  10.     for (const file of files) {
  11.         const stat = await fs.lstat(path.join(filePath, file));
  12.         if(stat.isDirectory()) registerCommands(client, path.join(dir, file));
  13.         if(file.endsWith('.js')) {
  14.             const Command = require(path.join(filePath, file));
  15.             if(Command.prototype instanceof BaseCommand) {
  16.                 const cmd = new Command();
  17.                 client.commands.set(cmd.name, cmd);
  18.             }
  19.         }
  20.     }
  21. }
  22.  
  23. async function registerEvents(client, dir = '') {
  24.     const filePath = path.join(__dirname, dir);
  25.     console.log(filePath);
  26.     const files = await fs.readdir(filePath);
  27.     for (const file of files) {
  28.         const stat = await fs.lstat(path.join(filePath, file));
  29.         if(stat.isDirectory()) registerEvents(client, path.join(dir, file));
  30.         if(file.endsWith('.js')) {
  31.             const Event = require(path.join(filePath, file));
  32.             if(Event.prototype instanceof BaseEvent) {
  33.                 const event = new Event();
  34.                 client.on(event.name, event.run.bind(null, client));
  35.             }
  36.         }
  37.     }
  38. }
  39.  
  40. module.exports = { registerCommands, registerEvents }
RAW Paste Data