71 lines
1.9 kB
1
import {
2
APPLICATION_ID,
3
Commands,
4
LegacyCommand,
5
RegisteredCommand
6
} from "@moonlight-mod/types/coreExtensions/commands";
7
8
type LegacyCommands = Record<string, LegacyCommand>;
9
let legacyCommands: LegacyCommands | undefined;
10
let queuedLegacyCommands: Record<string, LegacyCommand> | null = {};
11
12
const registeredCommands: RegisteredCommand[] = [];
13
14
export function _getLegacyCommands(commands: LegacyCommands) {
15
legacyCommands = commands;
16
if (queuedLegacyCommands != null) {
17
for (const [key, value] of Object.entries(queuedLegacyCommands)) {
18
legacyCommands[key] = value;
19
}
20
queuedLegacyCommands = null;
21
}
22
}
23
24
export const commands: Commands = {
25
registerCommand(command) {
26
const registered: RegisteredCommand = {
27
...command,
28
untranslatedName: command.id,
29
displayName: command.id,
30
applicationId: APPLICATION_ID,
31
untranslatedDescription: command.description,
32
displayDescription: command.description,
33
options: command.options?.map((o) => ({
34
...o,
35
displayName: o.name,
36
displayDescription: o.description
37
}))
38
};
39
registeredCommands.push(registered);
40
},
41
42
registerLegacyCommand(id, command) {
43
if (command.match) {
44
if (command.match instanceof RegExp) {
45
command.match = this.anyScopeRegex(command.match);
46
} else if (command.match.regex && typeof command.match !== "function") {
47
command.match = this.anyScopeRegex(command.match.regex);
48
}
49
}
50
51
if (!legacyCommands) {
52
queuedLegacyCommands![id] = command;
53
} else {
54
legacyCommands[id] = command;
55
}
56
},
57
58
anyScopeRegex(regex) {
59
const out = function (str: string) {
60
return regex.exec(str);
61
};
62
out.regex = regex;
63
return out;
64
},
65
66
_getCommands() {
67
return [...registeredCommands];
68
}
69
};
70
71
export default commands;
72