-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAPI.ts
More file actions
75 lines (68 loc) · 1.99 KB
/
Copy pathcreateAPI.ts
File metadata and controls
75 lines (68 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { ClientEvents } from "discord.js";
import type {
ModuleRegistry,
NuitAPI,
NuitCommandInput,
NuitCommand,
NuitEventHandler,
NuitEventOptions,
NuitModuleKind,
BaseCtx,
ModuleConfigField,
} from "./api";
function attachModule<T extends ModuleConfigField>(field: T, moduleName: string): T & { module: string } {
return {
...field,
module: moduleName,
};
}
export function createAPI(
registry: ModuleRegistry,
moduleName: string,
kind: NuitModuleKind | null = null,
): NuitAPI {
const selfApi: NuitAPI = {
registerCommand(cmd: NuitCommandInput) {
const internal: NuitCommand = {
...cmd,
module: moduleName,
kind,
execute: (interaction, ctx: BaseCtx) =>
cmd.execute(interaction, ctx),
};
registry.commands.push(internal);
},
onEvent<K extends keyof ClientEvents>(
name: K,
handler: NuitEventHandler<K>,
options?: NuitEventOptions,
) {
registry.events.push({
module: moduleName,
name,
once: false,
guildScoped: options?.guildScoped ?? false,
handler,
} as ModuleRegistry["events"][number]);
},
onceEvent<K extends keyof ClientEvents>(
name: K,
handler: NuitEventHandler<K>,
options?: NuitEventOptions,
) {
registry.events.push({
module: moduleName,
name,
once: true,
guildScoped: options?.guildScoped ?? false,
handler,
} as ModuleRegistry["events"][number]);
},
registerConfig(config: ModuleConfigField[]) {
for (const field of config) {
registry.config.push(attachModule(field, moduleName));
}
},
};
return selfApi;
}