Skip to content

Commit 55b26ad

Browse files
authoredMar 28, 2023
fix(ApplicationCommandRegistries): emit event if user is listening for bulk overwrites errors (#622)
1 parent d11824f commit 55b26ad

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed
 

‎src/lib/structures/Command.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { Args } from '../parsers/Args';
1414
import { BucketScope, RegisterBehavior } from '../types/Enums';
1515
import { acquire, getDefaultBehaviorWhenNotIdentical, handleBulkOverwrite } from '../utils/application-commands/ApplicationCommandRegistries';
1616
import type { ApplicationCommandRegistry } from '../utils/application-commands/ApplicationCommandRegistry';
17-
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
1817
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
18+
import { emitPerRegistryError } from '../utils/application-commands/registriesErrors';
1919
import { PreconditionContainerArray, type PreconditionEntryResolvable } from '../utils/preconditions/PreconditionContainerArray';
2020
import { FlagUnorderedStrategy, type FlagStrategyOptions } from '../utils/strategies/FlagUnorderedStrategy';
2121
import type { CommandStore } from './CommandStore';
@@ -293,7 +293,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
293293
try {
294294
await updatedPiece.registerApplicationCommands(updatedRegistry);
295295
} catch (err) {
296-
emitRegistryError(err, updatedPiece);
296+
emitPerRegistryError(err, updatedPiece);
297297
// No point on continuing
298298
return;
299299
}

‎src/lib/structures/CommandStore.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
handleBulkOverwrite,
77
registries
88
} from '../utils/application-commands/ApplicationCommandRegistries';
9-
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
109
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
10+
import { emitPerRegistryError } from '../utils/application-commands/registriesErrors';
1111
import { Command } from './Command';
1212

1313
/**
@@ -65,7 +65,7 @@ export class CommandStore extends AliasStore<Command> {
6565
try {
6666
await command.registerApplicationCommands(command.applicationCommandRegistry);
6767
} catch (error) {
68-
emitRegistryError(error, command);
68+
emitPerRegistryError(error, command);
6969
}
7070
}
7171
}

‎src/lib/types/Events.ts

+8
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ export const Events = {
211211
*/
212212
ApplicationCommandRegistriesRegistered: 'applicationCommandRegistriesRegistered' as const,
213213

214+
/**
215+
* Emitted when an error is encountered when handling the command registries in bulk overwrite mode.
216+
* @param {*} error The error that was thrown
217+
* @param {string|null} guildId The guild id where the error was thrown
218+
*/
219+
ApplicationCommandRegistriesBulkOverwriteError: 'applicationCommandRegistriesBulkOverwriteError' as const,
220+
214221
// Piece store?
215222
/**
216223
* Emitted after a piece is loaded.
@@ -560,6 +567,7 @@ declare module 'discord.js' {
560567
[SapphireEvents.ListenerError]: [error: unknown, payload: ListenerErrorPayload];
561568
[SapphireEvents.CommandApplicationCommandRegistryError]: [error: unknown, command: Command];
562569
[SapphireEvents.ApplicationCommandRegistriesRegistered]: [registries: Map<string, ApplicationCommandRegistry>];
570+
[SapphireEvents.ApplicationCommandRegistriesBulkOverwriteError]: [error: unknown, guildId: string | null];
563571

564572
[SapphireEvents.PreMessageParsed]: [message: Message];
565573
[SapphireEvents.MentionPrefixOnly]: [message: Message];

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type { CommandStore } from '../../structures/CommandStore';
66
import { RegisterBehavior } from '../../types/Enums';
77
import { Events } from '../../types/Events';
88
import { ApplicationCommandRegistry } from './ApplicationCommandRegistry';
9-
import { emitRegistryError } from './emitRegistryError';
109
import { getNeededRegistryParameters } from './getNeededParameters';
11-
import { bulkOverwriteDebug, bulkOverwriteError, bulkOverwriteInfo, bulkOverwriteWarn } from './registriesLog';
10+
import { emitBulkOverwriteError, emitPerRegistryError } from './registriesErrors';
11+
import { bulkOverwriteDebug, bulkOverwriteInfo, bulkOverwriteWarn } from './registriesLog';
1212

1313
export let defaultBehaviorWhenNotIdentical = RegisterBehavior.Overwrite;
1414

@@ -54,7 +54,7 @@ export async function handleRegistryAPICalls() {
5454
try {
5555
await command.registerApplicationCommands(command.applicationCommandRegistry);
5656
} catch (error) {
57-
emitRegistryError(error, command);
57+
emitPerRegistryError(error, command);
5858
}
5959
}
6060
}
@@ -121,7 +121,7 @@ export async function handleBulkOverwrite(commandStore: CommandStore, applicatio
121121

122122
bulkOverwriteInfo(`Successfully overwrote global application commands. The application now has ${result.size} global commands`);
123123
} catch (error) {
124-
bulkOverwriteError(`Failed to overwrite global application commands`, error);
124+
emitBulkOverwriteError(error, null);
125125
}
126126

127127
// Handle guild commands
@@ -160,9 +160,11 @@ export async function handleBulkOverwrite(commandStore: CommandStore, applicatio
160160
`Successfully overwrote guild application commands for guild ${guildId}. The application now has ${result.size} guild commands for guild ${guildId}`
161161
);
162162
} catch (error) {
163-
bulkOverwriteError(`Failed to overwrite guild application commands for guild ${guildId}`, error);
163+
emitBulkOverwriteError(error, guildId);
164164
}
165165
}
166+
167+
container.client.emit(Events.ApplicationCommandRegistriesRegistered, registries);
166168
}
167169

168170
async function handleAppendOrUpdate(

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

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { container } from '@sapphire/pieces';
2+
import type { Command } from '../../structures/Command';
3+
import { Events } from '../../types/Events';
4+
import { bulkOverwriteError } from './registriesLog';
5+
6+
/**
7+
* Opinionatedly logs the encountered registry error.
8+
* @param error The emitted error
9+
* @param command The command which had the error
10+
*/
11+
export function emitPerRegistryError(error: unknown, command: Command<any, any>) {
12+
const { name, location } = command;
13+
const { client, logger } = container;
14+
15+
if (client.listenerCount(Events.CommandApplicationCommandRegistryError)) {
16+
client.emit(Events.CommandApplicationCommandRegistryError, error, command);
17+
} else {
18+
logger.error(
19+
`Encountered error while handling the command application command registry for command "${name}" at path "${location.full}"`,
20+
error
21+
);
22+
}
23+
}
24+
25+
/**
26+
* Opinionatedly logs any bulk overwrite registries error.
27+
* @param error The emitted error
28+
* @param guildId The guild id in which the error was caused
29+
*/
30+
export function emitBulkOverwriteError(error: unknown, guildId: string | null) {
31+
const { client } = container;
32+
33+
if (client.listenerCount(Events.ApplicationCommandRegistriesBulkOverwriteError)) {
34+
client.emit(Events.ApplicationCommandRegistriesBulkOverwriteError, error, guildId);
35+
} else if (guildId) {
36+
bulkOverwriteError(`Failed to overwrite guild application commands for guild ${guildId}`, error);
37+
} else {
38+
bulkOverwriteError(`Failed to overwrite global application commands`, error);
39+
}
40+
}

0 commit comments

Comments
 (0)
Please sign in to comment.