Uhm excuse me, but what the fuck

This commit is contained in:
Dennis Postma 2024-09-30 22:56:17 +02:00
parent 4f9a1bc879
commit da8ef9fa65
3 changed files with 67 additions and 66 deletions

View File

@ -61,43 +61,34 @@ class CommandManager {
} }
private async loadCommands() { private async loadCommands() {
const commandsDir = path.join(process.cwd(), 'src', 'commands') const commandsDir = path.join(process.cwd(), 'src', 'commands');
commandLogger.info(`Loading commands from: ${commandsDir}`);
try { try {
const files: string[] = await fs.promises.readdir(commandsDir) const files = await fs.promises.readdir(commandsDir, { withFileTypes: true });
for (const file of files) { for (const file of files) {
if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
continue;
}
const fullPath = path.join(commandsDir, file.name);
const commandName = path.basename(file.name, path.extname(file.name));
try { try {
const extension = path.extname(file) const module = await import(fullPath);
const commandName = path.basename(file, extension) if (typeof module.default !== 'function') {
commandLogger.warn(`Unrecognized export in ${file.name}`);
let commandPath: string continue;
commandPath = path.join(commandsDir, `${commandName}.js`)
if (config.ENV === 'development') {
commandPath = path.join(commandsDir, `${commandName}.ts`)
} }
if (!fs.existsSync(commandPath)) { this.registerCommand(commandName, module.default);
commandLogger.warn(`Command file not found: ${commandPath}`)
continue
}
// Use dynamic import
const CommandModule = await import(commandPath)
const CommandClass = CommandModule.default
if (!CommandClass || typeof CommandClass !== 'function') {
commandLogger.warn(`Invalid command class in file: ${commandPath}`)
continue
}
this.registerCommand(commandName, CommandClass)
} catch (error) { } catch (error) {
commandLogger.error(`Failed to load command: ${file}: ${error}`) commandLogger.error(`Error loading command ${file.name}: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
} catch (error) { } catch (error) {
commandLogger.error(`Failed to read commands directory: ${error}`) commandLogger.error(`Failed to read commands directory: ${error instanceof Error ? error.message : String(error)}`);
} }
} }

View File

@ -49,41 +49,41 @@ class QueueManager {
} }
private async processJob(job: Job) { private async processJob(job: Job) {
const { jobName, params, socketId } = job.data const { jobName, params, socketId } = job.data;
const jobsDir = path.join(process.cwd(), 'src', 'jobs');
const extension = config.ENV === 'development' ? '.ts' : '.js';
const jobPath = path.join(jobsDir, `${jobName}${extension}`);
queueLogger.info(`Processing job: ${jobName}`);
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`);
return;
}
try { try {
const jobsDir = path.join(process.cwd(), 'src', 'jobs') const module = await import(jobPath);
const extension = config.ENV === 'development' ? '.ts' : '.js' if (typeof module.default !== 'function') {
const jobPath = path.join(jobsDir, `${jobName}${extension}`) queueLogger.warn(`Unrecognized export in ${jobName}`);
return;
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`)
return
} }
const JobModule = await import(jobPath) const JobClass = module.default;
const JobClass = JobModule.default const jobInstance = new JobClass(params);
if (!JobClass || typeof JobClass !== 'function') {
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
return
}
const jobInstance = new JobClass(params)
if (socketId && this.io) { if (socketId && this.io) {
const socket = this.io.sockets.sockets.get(socketId) const socket = this.io.sockets.sockets.get(socketId);
if (socket) { if (socket) {
await jobInstance.execute(this.io, socket) await jobInstance.execute(this.io, socket);
} else { } else {
queueLogger.warn(`Socket not found for job: ${socketId}`) queueLogger.warn(`Socket not found for job: ${socketId}`);
await jobInstance.execute(this.io) await jobInstance.execute(this.io);
} }
} else { } else {
await jobInstance.execute(this.io) await jobInstance.execute(this.io);
} }
} catch (error: any) { } catch (error) {
queueLogger.error(`Error processing job ${jobName}: ${error.message}`) queueLogger.error(`Error processing job ${jobName}: ${error instanceof Error ? error.message : String(error)}`);
} }
} }

View File

@ -95,27 +95,37 @@ export class Server {
} }
private async loadEventHandlers(dir: string, socket: TSocket) { private async loadEventHandlers(dir: string, socket: TSocket) {
const files: Dirent[] = await fs.promises.readdir(dir, { withFileTypes: true }) try {
const files = await fs.promises.readdir(dir, { withFileTypes: true });
for (const file of files) { for (const file of files) {
const fullPath = path.join(dir, file.name) const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket);
continue;
}
if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
continue;
}
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket)
} else if (file.isFile() && (file.name.endsWith('.ts') || file.name.endsWith('.js'))) {
try { try {
const module = await import(fullPath) const module = await import(fullPath);
if (typeof module.default === 'function') { if (typeof module.default !== 'function') {
const EventClass = module.default appLogger.warn(`Unrecognized export in ${file.name}`);
const eventInstance = new EventClass(this.io, socket) continue;
eventInstance.listen()
} else {
appLogger.warn(`Unrecognized export in ${file.name}`)
} }
} catch (error: any) {
appLogger.error(`Error loading event handler ${file.name}: ${error.message}`) const EventClass = module.default;
const eventInstance = new EventClass(this.io, socket);
eventInstance.listen();
} catch (error) {
appLogger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
} catch (error) {
appLogger.error(`Error reading directory ${dir}: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
} }