Skip to content

Commit b4c252e

Browse files
authoredFeb 5, 2023
fix(Command): support BulkOverwrite for reloads (#600)
1 parent ee8be2e commit b4c252e

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
 

‎src/lib/structures/Command.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ArgumentStream, Lexer, Parser, type IUnorderedStrategy } from '@sapphire/lexure';
2-
import { AliasPiece, type AliasPieceJSON, type AliasStore } from '@sapphire/pieces';
2+
import { AliasPiece, type AliasPieceJSON } from '@sapphire/pieces';
33
import { isNullish, type Awaitable, type NonNullObject } from '@sapphire/utilities';
44
import {
55
ChatInputCommandInteraction,
@@ -11,13 +11,14 @@ import {
1111
type Snowflake
1212
} from 'discord.js';
1313
import { Args } from '../parsers/Args';
14-
import { BucketScope } from '../types/Enums';
15-
import { acquire } from '../utils/application-commands/ApplicationCommandRegistries';
14+
import { BucketScope, RegisterBehavior } from '../types/Enums';
15+
import { acquire, getDefaultBehaviorWhenNotIdentical, handleBulkOverwrite } from '../utils/application-commands/ApplicationCommandRegistries';
1616
import type { ApplicationCommandRegistry } from '../utils/application-commands/ApplicationCommandRegistry';
1717
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
1818
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
1919
import { PreconditionContainerArray, type PreconditionEntryResolvable } from '../utils/preconditions/PreconditionContainerArray';
2020
import { FlagUnorderedStrategy, type FlagStrategyOptions } from '../utils/strategies/FlagUnorderedStrategy';
21+
import type { CommandStore } from './CommandStore';
2122

2223
export class Command<PreParseReturn = Args, O extends Command.Options = Command.Options> extends AliasPiece<O> {
2324
/**
@@ -248,7 +249,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
248249

249250
public override async reload() {
250251
// Remove the aliases from the command store
251-
const store = this.store as AliasStore<this>;
252+
const store = this.store as CommandStore;
252253
const registry = this.applicationCommandRegistry;
253254

254255
for (const nameOrId of registry.chatInputCommands) {
@@ -293,6 +294,12 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
293294
}
294295
}
295296

297+
// If the default behavior is set to bulk overwrite, handle it as such and return.
298+
if (getDefaultBehaviorWhenNotIdentical() === RegisterBehavior.BulkOverwrite) {
299+
await handleBulkOverwrite(store, this.container.client.application!.commands);
300+
return;
301+
}
302+
296303
// Re-initialize the store and the API data (insert in the store handles the register method)
297304
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(updatedRegistry.guildIdsToFetch);
298305

‎src/lib/structures/CommandStore.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { AliasStore } from '@sapphire/pieces';
2-
import { allGuildIdsToFetchCommandsFor, registries } from '../utils/application-commands/ApplicationCommandRegistries';
2+
import { RegisterBehavior } from '../types/Enums';
3+
import {
4+
allGuildIdsToFetchCommandsFor,
5+
getDefaultBehaviorWhenNotIdentical,
6+
handleBulkOverwrite,
7+
registries
8+
} from '../utils/application-commands/ApplicationCommandRegistries';
39
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
410
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
511
import { Command } from './Command';
@@ -64,6 +70,12 @@ export class CommandStore extends AliasStore<Command> {
6470
}
6571
}
6672

73+
// If the default behavior is set to bulk overwrite, handle it as such and return.
74+
if (getDefaultBehaviorWhenNotIdentical() === RegisterBehavior.BulkOverwrite) {
75+
await handleBulkOverwrite(this, this.container.client.application.commands);
76+
return;
77+
}
78+
6779
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(allGuildIdsToFetchCommandsFor);
6880

6981
for (const command of this.values()) {

‎src/lib/utils/application-commands/ApplicationCommandRegistries.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function handleRegistryAPICalls() {
6969
await handleAppendOrUpdate(commandStore, params);
7070
}
7171

72-
async function handleBulkOverwrite(commandStore: CommandStore, applicationCommands: ApplicationCommandManager) {
72+
export async function handleBulkOverwrite(commandStore: CommandStore, applicationCommands: ApplicationCommandManager) {
7373
// Map registries by guild, global, etc
7474
const foundGlobalCommands: BulkOverwriteData[] = [];
7575
const foundGuildCommands: Record<string, BulkOverwriteData[]> = {};
@@ -109,7 +109,7 @@ async function handleBulkOverwrite(commandStore: CommandStore, applicationComman
109109
registry.globalCommandId = id;
110110
registry.addChatInputCommandIds(id);
111111

112-
// idHints are useless, and any manually added id or names could end up not being valid anymore if you use bulk overwrites
112+
// idHints are useless, and any manually added id or names could end up not being valid any longer if you use bulk overwrites
113113
// That said, this might be an issue, so we might need to do it like `handleAppendOrUpdate`
114114
commandStore.aliases.set(id, piece);
115115
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.