1
0
forked from noxious/server

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() {
const commandsDir = path.join(process.cwd(), 'src', 'commands')
const commandsDir = path.join(process.cwd(), 'src', 'commands');
commandLogger.info(`Loading commands from: ${commandsDir}`);
try {
const files: string[] = await fs.promises.readdir(commandsDir)
const files = await fs.promises.readdir(commandsDir, { withFileTypes: true });
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 {
const extension = path.extname(file)
const commandName = path.basename(file, extension)
let commandPath: string
commandPath = path.join(commandsDir, `${commandName}.js`)
if (config.ENV === 'development') {
commandPath = path.join(commandsDir, `${commandName}.ts`)
const module = await import(fullPath);
if (typeof module.default !== 'function') {
commandLogger.warn(`Unrecognized export in ${file.name}`);
continue;
}
if (!fs.existsSync(commandPath)) {
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)
this.registerCommand(commandName, module.default);
} 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) {
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) {
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 {
const jobsDir = path.join(process.cwd(), 'src', 'jobs')
const extension = config.ENV === 'development' ? '.ts' : '.js'
const jobPath = path.join(jobsDir, `${jobName}${extension}`)
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`)
return
const module = await import(jobPath);
if (typeof module.default !== 'function') {
queueLogger.warn(`Unrecognized export in ${jobName}`);
return;
}
const JobModule = await import(jobPath)
const JobClass = JobModule.default
if (!JobClass || typeof JobClass !== 'function') {
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
return
}
const jobInstance = new JobClass(params)
const JobClass = module.default;
const jobInstance = new JobClass(params);
if (socketId && this.io) {
const socket = this.io.sockets.sockets.get(socketId)
const socket = this.io.sockets.sockets.get(socketId);
if (socket) {
await jobInstance.execute(this.io, socket)
await jobInstance.execute(this.io, socket);
} else {
queueLogger.warn(`Socket not found for job: ${socketId}`)
await jobInstance.execute(this.io)
queueLogger.warn(`Socket not found for job: ${socketId}`);
await jobInstance.execute(this.io);
}
} else {
await jobInstance.execute(this.io)
await jobInstance.execute(this.io);
}
} catch (error: any) {
queueLogger.error(`Error processing job ${jobName}: ${error.message}`)
} catch (error) {
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) {
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) {
const fullPath = path.join(dir, file.name)
for (const file of files) {
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 {
const module = await import(fullPath)
if (typeof module.default === 'function') {
const EventClass = module.default
const eventInstance = new EventClass(this.io, socket)
eventInstance.listen()
} else {
appLogger.warn(`Unrecognized export in ${file.name}`)
const module = await import(fullPath);
if (typeof module.default !== 'function') {
appLogger.warn(`Unrecognized export in ${file.name}`);
continue;
}
} 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)}`);
}
}
}