-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
CommandStore.ts
83 lines (69 loc) · 2.73 KB
/
CommandStore.ts
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
76
77
78
79
80
81
82
83
import { AliasStore } from '@sapphire/pieces';
import { allGuildIdsToFetchCommandsFor, registries } from '../utils/application-commands/ApplicationCommandRegistries';
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
import { Command } from './Command';
/**
* Stores all Command pieces
* @since 1.0.0
*/
export class CommandStore extends AliasStore<Command> {
public constructor() {
super(Command, { name: 'commands' });
}
/**
* Get all the command categories.
*/
public get categories(): string[] {
const categories = new Set(this.map((command) => command.category));
categories.delete(null);
return [...categories] as string[];
}
public override unload(name: string | Command) {
const piece = this.resolve(name);
// Remove the aliases from the store
for (const nameOrId of piece.applicationCommandRegistry.chatInputCommands) {
const aliasedPiece = this.aliases.get(nameOrId);
if (aliasedPiece === piece) {
this.aliases.delete(nameOrId);
}
}
for (const nameOrId of piece.applicationCommandRegistry.contextMenuCommands) {
const aliasedPiece = this.aliases.get(nameOrId);
if (aliasedPiece === piece) {
this.aliases.delete(nameOrId);
}
}
// Remove the registry from the application command registries
registries.delete(piece.name);
return super.unload(name);
}
public override async loadAll() {
await super.loadAll();
// If we don't have an application, that means this was called on login...
if (!this.container.client.application) return;
// super.loadAll() currently deletes all application command registries while unloading old pieces,
// re-register application commands to ensure allGuildIdsToFetchCommandsFor has new guild ids for getNeededRegistryParameters
for (const command of this.values()) {
if (command.registerApplicationCommands) {
try {
await command.registerApplicationCommands(command.applicationCommandRegistry);
} catch (error) {
emitRegistryError(error, command);
}
}
}
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(allGuildIdsToFetchCommandsFor);
for (const command of this.values()) {
// eslint-disable-next-line @typescript-eslint/dot-notation
await command.applicationCommandRegistry['runAPICalls'](applicationCommands, globalCommands, guildCommands);
// Reinitialize the aliases
for (const nameOrId of command.applicationCommandRegistry.chatInputCommands) {
this.aliases.set(nameOrId, command);
}
for (const nameOrId of command.applicationCommandRegistry.contextMenuCommands) {
this.aliases.set(nameOrId, command);
}
}
}
}