@hyperneutrino/djs-lite 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.ts +41 -73
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -124,7 +124,7 @@ async function importAll(
124
124
  if (file.isDirectory()) return;
125
125
 
126
126
  const absolutePath = path.resolve(file.parentPath, file.name);
127
- const relativePath = path.relative(path.resolve(directory), absolutePath);
127
+ const relativePath = path.join(directory, path.relative(path.resolve(directory), absolutePath));
128
128
 
129
129
  const { default: item } = await import(absolutePath);
130
130
 
@@ -159,7 +159,7 @@ export async function loadSubcommandsAndGroups(directory: string): Promise<{
159
159
  options: (ApplicationCommandSubGroupData | ApplicationCommandSubCommandData)[];
160
160
  handler: Handler<ChatInputCommandInteraction>;
161
161
  }> {
162
- if (!(await fs.exists(directory))) throw new Error(`Loading subcomands/groups failed: ${directory} is required but could not be found.`);
162
+ if (!(await fs.exists(directory))) throw new Error(`Loading subcommands/groups failed: ${directory} is required but could not be found.`);
163
163
 
164
164
  const options: (ApplicationCommandSubGroupData | ApplicationCommandSubCommandData)[] = [];
165
165
  const handlers = new Map<string, Handler<ChatInputCommandInteraction>>();
@@ -188,8 +188,6 @@ export async function loadSubcommandsAndGroups(directory: string): Promise<{
188
188
  }
189
189
 
190
190
  export async function loadCommands(client: Client<true>, directory: string, guildId?: string) {
191
- const files = await fs.readdir(path.resolve(directory), { recursive: false, withFileTypes: true });
192
-
193
191
  const commandData: (ChatInputApplicationCommandData | UserApplicationCommandData | MessageApplicationCommandData)[] = [];
194
192
 
195
193
  const slashCommandHandlers = new Map<string, Handler<ChatInputCommandInteraction>>();
@@ -198,37 +196,28 @@ export async function loadCommands(client: Client<true>, directory: string, guil
198
196
 
199
197
  const slashCommandAutocompletes = new Map<string, Handler<AutocompleteInteraction>>();
200
198
 
201
- await Promise.all(
202
- files.map(async (file) => {
203
- if (file.isDirectory()) return;
204
-
205
- const absolutePath = path.resolve(file.parentPath, file.name);
206
- const relativePath = path.relative(path.resolve(directory), absolutePath);
207
-
208
- const { default: item } = await import(absolutePath);
199
+ await importAll({ directory, recursive: false }, async ({ file, relativePath, item }) => {
200
+ if (item instanceof SlashCommand) {
201
+ commandData.push({ ...item.data, type: ApplicationCommandType.ChatInput });
202
+ slashCommandHandlers.set(item.data.name, item.handler);
203
+ if (item.autocomplete) slashCommandAutocompletes.set(item.data.name, item.autocomplete);
204
+ } else if (item instanceof UserCommand) {
205
+ commandData.push({ ...item.data, type: ApplicationCommandType.User });
206
+ userCommandHandlers.set(item.data.name, item.handler);
207
+ } else if (item instanceof MessageCommand) {
208
+ commandData.push({ ...item.data, type: ApplicationCommandType.Message });
209
+ messageCommandHandlers.set(item.data.name, item.handler);
210
+ } else if (item instanceof SlashCommandWithSubcommands) {
211
+ const { options, handler } = await loadSubcommandsAndGroups(relativePath.replace(/\.[^/.]+$/, ""));
212
+ commandData.push({ ...item.data, options });
213
+ slashCommandHandlers.set(item.data.name, handler);
214
+ } else {
215
+ throw new Error(`Loading commands failed: export from ${relativePath} was not an instance of <Type>Command.`);
216
+ }
209
217
 
210
- if (item instanceof SlashCommand) {
211
- commandData.push({ ...item.data, type: ApplicationCommandType.ChatInput });
212
- slashCommandHandlers.set(item.data.name, item.handler);
213
- if (item.autocomplete) slashCommandAutocompletes.set(item.data.name, item.autocomplete);
214
- } else if (item instanceof UserCommand) {
215
- commandData.push({ ...item.data, type: ApplicationCommandType.User });
216
- userCommandHandlers.set(item.data.name, item.handler);
217
- } else if (item instanceof MessageCommand) {
218
- commandData.push({ ...item.data, type: ApplicationCommandType.Message });
219
- messageCommandHandlers.set(item.data.name, item.handler);
220
- } else if (item instanceof SlashCommandWithSubcommands) {
221
- const { options, handler } = await loadSubcommandsAndGroups(relativePath.replace(/\.[^/.]+$/, ""));
222
- commandData.push({ ...item.data, options });
223
- slashCommandHandlers.set(item.data.name, handler);
224
- } else {
225
- throw new Error(`Loading commands failed: export from ${relativePath} was not an instance of <Type>Command.`);
226
- }
227
-
228
- if (item.data.name !== file.name.replace(/.[^/.]+$/, ""))
229
- throw new Error(`Code style enforcement: name exported from ${relativePath} does not match the filename`);
230
- }),
231
- );
218
+ if (item.data.name !== file.name.replace(/.[^/.]+$/, ""))
219
+ throw new Error(`Code style enforcement: name exported from ${relativePath} does not match the filename`);
220
+ });
232
221
 
233
222
  client.on(Events.InteractionCreate, (interaction) => {
234
223
  if (interaction.isChatInputCommand()) slashCommandHandlers.get(interaction.commandName)?.(interaction);
@@ -248,8 +237,6 @@ export async function loadCommands(client: Client<true>, directory: string, guil
248
237
  }
249
238
 
250
239
  export async function loadInteractions(client: Client, directory: string, argumentSeparator: string = ":") {
251
- const files = await fs.readdir(path.resolve(directory), { recursive: true, withFileTypes: true });
252
-
253
240
  const modalHandlers = new Map<string, Handler<ModalSubmitInteraction>>();
254
241
  const buttonHandlers = new Map<string, Handler<ButtonInteraction>>();
255
242
  const stringHandlers = new Map<string, Handler<StringSelectMenuInteraction>>();
@@ -258,26 +245,18 @@ export async function loadInteractions(client: Client, directory: string, argume
258
245
  const mentionHandlers = new Map<string, Handler<MentionableSelectMenuInteraction>>();
259
246
  const channelHandlers = new Map<string, Handler<ChannelSelectMenuInteraction>>();
260
247
 
261
- await Promise.all(
262
- files.map(async (file) => {
263
- if (file.isDirectory()) return;
264
-
265
- const absolutePath = path.resolve(file.parentPath, file.name);
266
- const relativePath = path.relative(path.resolve(directory), absolutePath);
267
- const handlerKey = relativePath.replace(/\.[^/.]+$/, "").replace(/\\/g, "/");
268
-
269
- const { default: item } = await import(absolutePath).catch(() => null);
270
-
271
- if (item instanceof ModalHandler) modalHandlers.set(handlerKey, item.handler);
272
- else if (item instanceof ButtonHandler) buttonHandlers.set(handlerKey, item.handler);
273
- else if (item instanceof StringSelectHandler) stringHandlers.set(handlerKey, item.handler);
274
- else if (item instanceof UserSelectHandler) userHandlers.set(handlerKey, item.handler);
275
- else if (item instanceof RoleSelectHandler) roleHandlers.set(handlerKey, item.handler);
276
- else if (item instanceof MentionSelectHandler) mentionHandlers.set(handlerKey, item.handler);
277
- else if (item instanceof ChannelSelectHandler) channelHandlers.set(handlerKey, item.handler);
278
- else throw new Error(`Loading interactions failed: export from ${relativePath} was not an instance of <InteractionType>Handler.`);
279
- }),
280
- );
248
+ await importAll({ directory, recursive: true }, async ({ file, relativePath, item }) => {
249
+ const handlerKey = relativePath.replace(/\.[^/.]+$/, "").replace(/\\/g, "/");
250
+
251
+ if (item instanceof ModalHandler) modalHandlers.set(handlerKey, item.handler);
252
+ else if (item instanceof ButtonHandler) buttonHandlers.set(handlerKey, item.handler);
253
+ else if (item instanceof StringSelectHandler) stringHandlers.set(handlerKey, item.handler);
254
+ else if (item instanceof UserSelectHandler) userHandlers.set(handlerKey, item.handler);
255
+ else if (item instanceof RoleSelectHandler) roleHandlers.set(handlerKey, item.handler);
256
+ else if (item instanceof MentionSelectHandler) mentionHandlers.set(handlerKey, item.handler);
257
+ else if (item instanceof ChannelSelectHandler) channelHandlers.set(handlerKey, item.handler);
258
+ else throw new Error(`Loading interactions failed: export from ${relativePath} was not an instance of <InteractionType>Handler.`);
259
+ });
281
260
 
282
261
  client.on(Events.InteractionCreate, (interaction) => {
283
262
  if (!interaction.isMessageComponent() && !interaction.isModalSubmit()) return;
@@ -296,25 +275,14 @@ export async function loadInteractions(client: Client, directory: string, argume
296
275
  }
297
276
 
298
277
  export async function loadEvents(client: Client, directory: string, recursive: boolean = false) {
299
- const files = await fs.readdir(path.resolve(directory), { recursive, withFileTypes: true });
300
278
  const handlers: Partial<{ [K in keyof ClientEvents]: ((...args: ClientEvents[K]) => unknown)[] }> = {};
301
279
 
302
- await Promise.all(
303
- files.map(async (file) => {
304
- if (file.isDirectory()) return;
305
-
306
- const absolutePath = path.resolve(file.parentPath, file.name);
307
-
308
- const { default: item } = await import(absolutePath);
309
-
310
- if (item instanceof EventHandler) (handlers[item.event as keyof ClientEvents] ??= []).push(item.handler);
311
- else {
312
- throw new Error(
313
- `Loading events failed: export from ${path.relative(path.resolve(directory), absolutePath)} was not an instance of EventHandler<T>.`,
314
- );
315
- }
316
- }),
317
- );
280
+ await importAll({ directory, recursive }, async ({ relativePath, item }) => {
281
+ if (item instanceof EventHandler) (handlers[item.event as keyof ClientEvents] ??= []).push(item.handler);
282
+ else {
283
+ throw new Error(`Loading events failed: export from ${relativePath} was not an instance of EventHandler<T>.`);
284
+ }
285
+ });
318
286
 
319
287
  Object.entries(handlers).forEach(([key, handlers]) => client.on(key, (...args) => handlers.forEach((handler) => (handler as any)(...args))));
320
288
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hyperneutrino/djs-lite",
3
3
  "private": false,
4
- "version": "1.4.0",
4
+ "version": "1.4.1",
5
5
  "module": "index.ts",
6
6
  "type": "module",
7
7
  "devDependencies": {