@hyperneutrino/djs-lite 1.0.1 → 1.0.2
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.
- package/index.ts +26 -0
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
type BaseApplicationCommandData,
|
|
9
9
|
type ChatInputApplicationCommandData,
|
|
10
10
|
type ChatInputCommandInteraction,
|
|
11
|
+
type ClientEvents,
|
|
11
12
|
type MessageApplicationCommandData,
|
|
12
13
|
type MessageContextMenuCommandInteraction,
|
|
13
14
|
type UserApplicationCommandData,
|
|
@@ -43,6 +44,16 @@ export class SlashCommand extends Command<ChatInputApplicationCommandData & { ty
|
|
|
43
44
|
export class UserCommand extends Command<UserApplicationCommandData, UserContextMenuCommandInteraction> {}
|
|
44
45
|
export class MessageCommand extends Command<MessageApplicationCommandData, MessageContextMenuCommandInteraction> {}
|
|
45
46
|
|
|
47
|
+
export class EventHandler<T extends keyof ClientEvents> {
|
|
48
|
+
event: T;
|
|
49
|
+
handler: (...args: ClientEvents[T]) => unknown;
|
|
50
|
+
|
|
51
|
+
constructor({ event, handler }: { event: T; handler: (...args: ClientEvents[T]) => unknown }) {
|
|
52
|
+
this.event = event;
|
|
53
|
+
this.handler = handler;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
46
57
|
export async function loadCommands(client: Client<true>, directory: string) {
|
|
47
58
|
const files = await fs.readdir(path.resolve(directory), { recursive: false, withFileTypes: true });
|
|
48
59
|
|
|
@@ -111,3 +122,18 @@ export async function loadInteractions(client: Client<true>, directory: string,
|
|
|
111
122
|
handlers.get(path)?.(interaction, ...args);
|
|
112
123
|
});
|
|
113
124
|
}
|
|
125
|
+
|
|
126
|
+
export async function loadEvents(client: Client<true>, directory: string, recursive: boolean = false) {
|
|
127
|
+
const files = await fs.readdir(path.resolve(directory), { recursive, withFileTypes: true });
|
|
128
|
+
const handlers: Partial<{ [K in keyof ClientEvents]: ((...args: ClientEvents[K]) => unknown)[] }> = {};
|
|
129
|
+
|
|
130
|
+
await Promise.all(
|
|
131
|
+
files.map(async (file) => {
|
|
132
|
+
if (file.isDirectory()) return;
|
|
133
|
+
const { default: item } = await import(path.resolve(file.parentPath, file.name));
|
|
134
|
+
if (item instanceof EventHandler) (handlers[item.event as keyof ClientEvents] ??= []).push(item.handler);
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
Object.entries(handlers).forEach(([key, handlers]) => client.on(key, (...args) => handlers.forEach((handler) => (handler as any)(...args))));
|
|
139
|
+
}
|