Skip to content

Commit 7c32b44

Browse files
minemaloxkyranet
andauthored
feat: add CoreApplicationCommandRegistriesBulkOverwrite event listener (#855)
This event listener is used to log when bulk overwrite is successful. By default is is automatically loaded, it can be overwritten to log something else or null by creating a custom listener with the same name. Co-authored-by: Aura <kyradiscord@gmail.com>
1 parent 1bfb00a commit 7c32b44

5 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/lib/types/Events.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
type ChatInputCommandInteraction,
77
type ContextMenuCommandInteraction,
88
type Interaction,
9-
type Message
9+
type Message,
10+
type Collection,
11+
type Snowflake
1012
} from 'discord.js';
1113
import type { UserError } from '../errors/UserError';
1214
import type { Command } from '../structures/Command';
@@ -219,6 +221,13 @@ export const Events = {
219221
*/
220222
ApplicationCommandRegistriesRegistered: 'applicationCommandRegistriesRegistered' as const,
221223

224+
/**
225+
* Emitted when handling the command registries in bulk overwrite mode.
226+
* @param {Collection<Snowflake, ApplicationCommand>} result The result of the bulk overwrite
227+
* @param {string|null} guildId The guild id where the bulk overwrite was applied, null if global
228+
*/
229+
ApplicationCommandRegistriesBulkOverwrite: 'applicationCommandRegistriesBulkOverwrite' as const,
230+
222231
/**
223232
* Emitted when an error is encountered when handling the command registries in bulk overwrite mode.
224233
* @param {*} error The error that was thrown
@@ -606,6 +615,7 @@ declare module 'discord.js' {
606615
[SapphireEvents.CommandApplicationCommandRegistryError]: [error: unknown, command: Command];
607616
[SapphireEvents.ApplicationCommandRegistriesInitialising]: [];
608617
[SapphireEvents.ApplicationCommandRegistriesRegistered]: [registries: Map<string, ApplicationCommandRegistry>, timeTaken: number];
618+
[SapphireEvents.ApplicationCommandRegistriesBulkOverwrite]: [result: Collection<Snowflake, ApplicationCommand>, guildId: string | null];
609619
[SapphireEvents.ApplicationCommandRegistriesBulkOverwriteError]: [error: unknown, guildId: string | null];
610620

611621
[SapphireEvents.PreMessageParsed]: [message: Message];

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Events } from '../../types/Events';
1010
import { ApplicationCommandRegistry } from './ApplicationCommandRegistry';
1111
import { getNeededRegistryParameters } from './getNeededParameters';
1212
import { emitBulkOverwriteError, emitPerRegistryError } from './registriesErrors';
13-
import { bulkOverwriteDebug, bulkOverwriteInfo, bulkOverwriteWarn } from './registriesLog';
13+
import { bulkOverwriteDebug, bulkOverwriteWarn } from './registriesLog';
1414

1515
export let defaultBehaviorWhenNotIdentical = RegisterBehavior.Overwrite;
1616
export let defaultGuildIds: ApplicationCommandRegistry.RegisterOptions['guildIds'] = undefined;
@@ -183,7 +183,7 @@ async function handleBulkOverwriteGlobalCommands(
183183
}
184184
}
185185

186-
bulkOverwriteInfo(`Successfully overwrote global application commands. The application now has ${result.size} global commands`);
186+
container.client.emit(Events.ApplicationCommandRegistriesBulkOverwrite, result, null);
187187
} catch (error) {
188188
if (error instanceof Error && error.name === 'AbortError') throw error;
189189

@@ -236,9 +236,7 @@ async function handleBulkOverwriteGuildCommands(
236236
}
237237
}
238238

239-
bulkOverwriteInfo(
240-
`Successfully overwrote guild application commands for guild ${guildId}. The application now has ${result.size} guild commands for guild ${guildId}`
241-
);
239+
container.client.emit(Events.ApplicationCommandRegistriesBulkOverwrite, result, guildId);
242240
} catch (error) {
243241
if (error instanceof Error && error.name === 'AbortError') throw error;
244242

src/lib/utils/application-commands/registriesLog.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { container } from '@sapphire/pieces';
22

3-
export function bulkOverwriteInfo(message: string, ...other: unknown[]) {
4-
container.logger.info(`ApplicationCommandRegistries(BulkOverwrite) ${message}`, ...other);
5-
}
6-
73
export function bulkOverwriteError(message: string, ...other: unknown[]) {
84
container.logger.error(`ApplicationCommandRegistries(BulkOverwrite) ${message}`, ...other);
95
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Listener } from '../../lib/structures/Listener';
2+
import { Events } from '../../lib/types/Events';
3+
import type { ApplicationCommand, Collection, Snowflake } from 'discord.js';
4+
5+
export class CoreListener extends Listener<typeof Events.ApplicationCommandRegistriesBulkOverwrite> {
6+
public constructor(context: Listener.LoaderContext) {
7+
super(context, { event: Events.ApplicationCommandRegistriesBulkOverwrite });
8+
}
9+
10+
public run(result: Collection<Snowflake, ApplicationCommand>, guildId: string | null) {
11+
if (guildId) {
12+
this.container.logger.info(
13+
`ApplicationCommandRegistries(BulkOverwrite) Successfully overwrote guild application commands for guild ${guildId}. The application now has ${result.size} guild commands for guild ${guildId}`
14+
);
15+
} else {
16+
this.container.logger.info(
17+
`ApplicationCommandRegistries(BulkOverwrite) Successfully overwrote global application commands. The application now has ${result.size} global commands`
18+
);
19+
}
20+
}
21+
}

src/optional-listeners/application-command-registries-listeners/_load.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { container } from '@sapphire/pieces';
22
import { CoreListener as CoreApplicationCommandRegistriesInitialising } from './CoreApplicationCommandRegistriesInitialising';
33
import { CoreListener as CoreApplicationCommandRegistriesRegistered } from './CoreApplicationCommandRegistriesRegistered';
4+
import { CoreListener as CoreApplicationCommandRegistriesBulkOverwrite } from './CoreApplicationCommandRegistriesBulkOverwrite';
45

56
export function loadApplicationCommandRegistriesListeners() {
67
const store = 'listeners' as const;
@@ -14,4 +15,9 @@ export function loadApplicationCommandRegistriesListeners() {
1415
piece: CoreApplicationCommandRegistriesRegistered,
1516
store
1617
});
18+
void container.stores.loadPiece({
19+
name: 'CoreApplicationCommandRegistriesBulkOverwrite',
20+
piece: CoreApplicationCommandRegistriesBulkOverwrite,
21+
store
22+
});
1723
}

0 commit comments

Comments
 (0)