refactor(handlers): extract generic loadModules from loaders#1
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the handlers to centralize dynamic module loading into a reusable loadModules helper, reducing duplicated filesystem/import logic across event and command loaders.
Changes:
- Added a generic
loadModules<T>helper insrc/handlers/base-loader.ts. - Refactored event loading to use
loadModulesinstead of inline directory scanning/import logic. - Refactored slash command loading to use
loadModulesand then register the validated commands.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/handlers/event-handler.ts | Replaces inline event directory scanning/import with loadModules. |
| src/handlers/command-handler.ts | Replaces inline slash command scanning/import with loadModules and registers results. |
| src/handlers/base-loader.ts | Introduces the shared dynamic loader used by handlers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| isValid: (obj: unknown) => obj is T, | ||
| ): Promise<T[]> { | ||
| const items: T[] = []; | ||
| const dirPath = dirname(join(__dirname, '..', directory)); |
Comment on lines
+24
to
+28
| const moduleFiles = files.filter( | ||
| (file) => | ||
| (file.endsWith('.js') || file.endsWith('.ts')) && !file.endsWith('.d.ts'), | ||
| ); | ||
| for (const file of files) { |
Comment on lines
1
to
5
| import { readdir } from 'node:fs/promises'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
| import type { ClientEvents } from 'discord.js'; | ||
| import type { Event } from '@/types/event.js'; |
Comment on lines
+2
to
6
| import { dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { slashCommands } from '@/registries/slash-registry.js'; | ||
| import { logger } from '@/lib/logger.js'; | ||
| import { loadModules } from './base-loader.js'; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.