Skip to content

Commit c97ea12

Browse files
committed
✨ Allow functions to be exported instead of instance
1 parent cd4d69f commit c97ea12

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default new DiscordCommand({
8080
});
8181
```
8282

83+
You may export a function that returns the DiscordCommand instance.
84+
8385
In the `DiscordCommand` class you have to pass an object with the following arguments:
8486

8587
| Argument | Description | Required | Type |
@@ -109,6 +111,8 @@ export default new DiscordEvent("messageCreate", message => {
109111
});
110112
```
111113

114+
You may export a function that returns the DiscordEvent instance.
115+
112116
`"messageCreate"` can be changed for any event name just like the `message` variable can be changed to the incoming data of the event.
113117

114118
#### `modules` folder
@@ -133,6 +137,8 @@ import { DiscordModule } from "discord-module-loader";
133137
export default new DiscordModule("<module name>");
134138
```
135139

140+
You may export a function that returns the DiscordModule instance.
141+
136142
In the `DiscordModule` class you only have to specify the name you want the module to have.
137143

138144
#### `guilds` folder
@@ -157,6 +163,8 @@ import { DiscordGuild } from "discord-module-loader";
157163
export default new DiscordGuild("<guildId>");
158164
```
159165

166+
You may export a function that returns the DiscordGuild instance.
167+
160168
In the `DiscordGuild` class you only have the specify the guildId wherein the commands, events, and modules should work.
161169

162170
#### `index` file

src/classes/ModuleLoader.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export default class DiscordModuleLoader {
8282
if (!existsSync(resolve(dir, folder, "index.js")))
8383
throw new Error(`Couldn't find index.js in ${folder}`);
8484

85-
const guild = (await import(resolve(dir, folder, "index.js"))).default;
85+
let guild = (await import(resolve(dir, folder, "index.js"))).default;
86+
87+
if (typeof guild === "function") guild = await guild();
8688

8789
if (!(guild instanceof DiscordGuild))
8890
throw new Error(`Guild ${folder} is not an Guild.`);
@@ -141,7 +143,9 @@ export default class DiscordModuleLoader {
141143
if (!existsSync(resolve(dir, folder, "index.js")))
142144
throw new Error(`Couldn't find index.js in ${folder}`);
143145

144-
const module = (await import(resolve(dir, folder, "index.js"))).default;
146+
let module = (await import(resolve(dir, folder, "index.js"))).default;
147+
148+
if (typeof module === "function") module = await module();
145149

146150
if (!(module instanceof DiscordModule))
147151
throw new Error(`Module ${folder} is not an Module`);
@@ -194,7 +198,9 @@ export default class DiscordModuleLoader {
194198

195199
const returnEvents: [string, DiscordEvent<any>][] = [];
196200
for (const file of events) {
197-
const event = (await import(resolve(dir, file))).default;
201+
let event = (await import(resolve(dir, file))).default;
202+
203+
if (typeof event === "function") event = await event();
198204

199205
if (!(event instanceof DiscordEvent))
200206
throw new Error(`Event ${file} is not an Event`);
@@ -244,7 +250,9 @@ export default class DiscordModuleLoader {
244250

245251
const returnCommands: [string, DiscordCommand][] = [];
246252
for (const file of commands) {
247-
const command = (await import(resolve(dir, file))).default;
253+
let command = (await import(resolve(dir, file))).default;
254+
255+
if (typeof command === "function") command = await command();
248256

249257
if (!(command instanceof DiscordCommand))
250258
throw new Error(`Command ${file} is not a Command`);

0 commit comments

Comments
 (0)