file description module import type { HibikiClient } from "../classes/Client" ; import type { Command } from "../classes/Command" ; import type { Event } from "../classes/Event" ; import { readdirSync } from "fs" ; import path from "path" ; const COMMANDS_DIRECTORY = path . join ( __dirname , "../commands" ) ; const EVENTS_DIRECTORY = path . join ( __dirname , "../events" ) ; const LOGGERS_DIRECTORY = path . join ( __dirname , "../loggers" ) ; const fileTypes = /\.(js|ts)$/i ; function loadCommands ( path , bot ) { const files = readdirSync ( path , { withFileTypes : true } ) ; files . forEach ( ( file ) => { if ( file . isDirectory ( ) ) { return loadCommands ( `${ path }/${ file . name }` , bot ) ; } let command ; if ( ! fileTypes . test ( file . name ) ) return ; try { const importedCommand = require ( `${ path }/${ file . name }` ) ; command = importedCommand [ Object . keys ( importedCommand ) [ 0 ] ] ; } catch ( err ) { console . error ( err ) ; bot . log . error ( `Command ${ file . name } failed to load: ${ err }` ) ; } if ( ! command ) return ; const splitPath = path . split ( "/" ) ; const cmd = new command ( bot , file . name . split ( fileTypes ) [ 0 ] , splitPath [ splitPath . length - 1 ] ) as Command ; let cmdMissingKeys = false ; if ( ! cmd . requiredkeys . every ( ( k ) => ( Object . keys ( bot . config . keys ) . includes ( k ) && bot . config . keys [ k ] ) || bot . config . keys . botlists [ k ] ) ) { cmdMissingKeys = true ; } if ( ! cmdMissingKeys ) bot . commands . push ( cmd ) ; } ) ; } function loadEvents ( path , bot , logger = false ) { function subscribeEvents ( array = [ ] ) { array . forEach ( ( e ) => { e . events . forEach ( ( ev ) => { bot . on ( ev , ( ... eventParams ) => e . run ( ev , ... eventParams ) ) ; } ) ; } ) ; } const files = readdirSync ( path , { withFileTypes : true } ) ; files . forEach ( ( file ) => { if ( file . isDirectory ( ) ) return ; let event ; if ( ! fileTypes . test ( file . name ) ) return ; try { const importedEvent = require ( `${ path }/${ file . name }` ) ; event = importedEvent [ Object . keys ( importedEvent ) [ 0 ] ] ; } catch ( err ) { console . error ( err ) ; bot . log . error ( `Event ${ file . name } failed to load: ${ err }` ) ; } if ( ! event ) return ; ( logger ? bot . loggers : bot . events ) . push ( new event ( bot , file . name . split ( fileTypes ) [ 0 ] ) ) ; } ) ; subscribeEvents ( logger ? bot . loggers : bot . events ) ; } export function loadItems ( bot ) { loadCommands ( COMMANDS_DIRECTORY , bot ) ; loadEvents ( EVENTS_DIRECTORY , bot ) ; loadEvents ( LOGGERS_DIRECTORY , bot , true ) ; }