File Loading Expanded
This guide expands on the behaviours described in Understanding Components.
Finding Components
You can pass file or directory paths to the client components
option. Any directories will be read recursively to find all the files. Any ignored files or files that don’t have a supported extension will be ignored/skipped.
For example, if we set the components
to be src/components
the highlighted files will be loaded and checked for components:
- package.json
Directorysrc/
- index.js
Directorycomponents/
- some-component.js
- assignment.odt
- example.ts
- _living-on-the-edge.js
Directorydemo/
- task.ts
- krabby-patty-formula.txt
- …
Exports
After a file is loaded by JellyCommands we’ll read all the exports to find components. The name of the export is ignored, and the only the immediate value is checked. For example:
import { command } from 'jellycommands';
// This will be foundexport const test = command();
// so will thisexport const test2 = command();
// this tooexport default command();
export const test3 = [ // This won't be found command()]
export default { // neither will this test}
Path Resolution
Paths are “resolved” to the current working directory (cwd), from which you run your bot. This is in previous examples we can do src/components
and it works. Since we’re running the bot from the same parent directory as src
. However, this can cause portability problems with your bot. You can’t run it from different directories without your components failing to be found.
You can fix this by passing in an absolute path to your components. That means that we don’t have to try and work one out for you. The best way to get this is to use a relative path to your current file (index.js
). Fortunately modern versions of node provide import.meta.dirname
to do this.
import { JellyCommands } from 'jellycommands';import { IntentsBitField } from 'discord.js';import { join } from 'node:path';
const client = new JellyCommands({ clientOptions: { intents: [IntentsBitField.Flags.Guilds], },
components: ['src/components'], components: [join(import.meta.dirname, './components')],});
client.login();
Ignored Files
Any file name that starts with a _
will be completely ignored. We consider this an escape hatch and don’t recommend using it, since any modules with side effects great enough to require this should be moved or rewritten.
File Extensions
By default JellyCommands will load .ts
and .js
files, if you need to change this you can use the fs.extensions
option. This will overwrite the defaults, so you’ll need to specify all extensions you want to support. For example:
import { JellyCommands } from 'jellycommands';
const client = new JellyCommands({ fs: { extensions: ['.js', '.ts', ...], },});