Skip to content

Commit 0e6d59a

Browse files
favnavladfrangu
andauthoredApr 12, 2023
feat: make the Application Command Registries status listeners optional (#627)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
1 parent cff5355 commit 0e6d59a

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed
 

‎src/lib/SapphireClient.ts

+12
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ export interface SapphireClientOptions {
106106
*/
107107
enableLoaderTraceLoggings?: boolean;
108108

109+
/**
110+
* If Sapphire should load the pre-included application command registries status listeners that log the status of registering application commands to the {@link SapphireClient.logger} instance.
111+
* This includes the events {@link Events.ApplicationCommandRegistriesInitialising} and {@link Events.ApplicationCommandRegistriesRegistered}.
112+
* @since 4.4.0
113+
* @default true
114+
*/
115+
loadApplicationCommandRegistriesStatusListeners?: boolean;
116+
109117
/**
110118
* If Sapphire should load the pre-included error event listeners that log any encountered errors to the {@link SapphireClient.logger} instance
111119
* @since 1.0.0
@@ -299,6 +307,10 @@ export class SapphireClient<Ready extends boolean = boolean> extends Client<Read
299307

300308
const optionalListenersPath = join(__dirname, '..', 'optional-listeners');
301309

310+
if (options.loadApplicationCommandRegistriesStatusListeners !== false) {
311+
this.stores.get('listeners').registerPath(join(optionalListenersPath, 'application-command-registries-listeners'));
312+
}
313+
302314
if (options.loadDefaultErrorListeners !== false) {
303315
this.stores.get('listeners').registerPath(join(optionalListenersPath, 'error-listeners'));
304316
}

‎src/lib/types/Events.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ export const Events = {
205205
*/
206206
CommandApplicationCommandRegistryError: 'commandApplicationCommandRegistryError' as const,
207207

208+
/**
209+
* Emitted when the application command registries are being initialized.
210+
*/
211+
ApplicationCommandRegistriesInitialising: 'applicationCommandRegistriesInitialising' as const,
212+
208213
/**
209214
* Emitted once the application command registries have been initialized.
210215
* @param {Map<string, ApplicationCommandRegistry>} registries The initialised registries
@@ -566,7 +571,8 @@ declare module 'discord.js' {
566571

567572
[SapphireEvents.ListenerError]: [error: unknown, payload: ListenerErrorPayload];
568573
[SapphireEvents.CommandApplicationCommandRegistryError]: [error: unknown, command: Command];
569-
[SapphireEvents.ApplicationCommandRegistriesRegistered]: [registries: Map<string, ApplicationCommandRegistry>];
574+
[SapphireEvents.ApplicationCommandRegistriesInitialising]: [];
575+
[SapphireEvents.ApplicationCommandRegistriesRegistered]: [registries: Map<string, ApplicationCommandRegistry>, timeTaken: number];
570576
[SapphireEvents.ApplicationCommandRegistriesBulkOverwriteError]: [error: unknown, guildId: string | null];
571577

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

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export function getDefaultBehaviorWhenNotIdentical() {
4747
}
4848

4949
export async function handleRegistryAPICalls() {
50+
container.client.emit(Events.ApplicationCommandRegistriesInitialising);
51+
5052
const commandStore = container.stores.get('commands');
5153

5254
for (const command of commandStore.values()) {
@@ -70,6 +72,8 @@ export async function handleRegistryAPICalls() {
7072
}
7173

7274
export async function handleBulkOverwrite(commandStore: CommandStore, applicationCommands: ApplicationCommandManager) {
75+
const now = Date.now();
76+
7377
// Map registries by guild, global, etc
7478
const foundGlobalCommands: BulkOverwriteData[] = [];
7579
const foundGuildCommands: Record<string, BulkOverwriteData[]> = {};
@@ -164,13 +168,15 @@ export async function handleBulkOverwrite(commandStore: CommandStore, applicatio
164168
}
165169
}
166170

167-
container.client.emit(Events.ApplicationCommandRegistriesRegistered, registries);
171+
container.client.emit(Events.ApplicationCommandRegistriesRegistered, registries, Date.now() - now);
168172
}
169173

170174
async function handleAppendOrUpdate(
171175
commandStore: CommandStore,
172176
{ applicationCommands, globalCommands, guildCommands }: Awaited<ReturnType<typeof getNeededRegistryParameters>>
173177
) {
178+
const now = Date.now();
179+
174180
for (const registry of registries.values()) {
175181
// eslint-disable-next-line @typescript-eslint/dot-notation
176182
await registry['runAPICalls'](applicationCommands, globalCommands, guildCommands);
@@ -188,7 +194,7 @@ async function handleAppendOrUpdate(
188194
}
189195
}
190196

191-
container.client.emit(Events.ApplicationCommandRegistriesRegistered, registries);
197+
container.client.emit(Events.ApplicationCommandRegistriesRegistered, registries, Date.now() - now);
192198
}
193199

194200
interface BulkOverwriteData {

‎src/listeners/CoreReady.ts

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ export class CoreEvent extends Listener {
1010
public async run() {
1111
this.container.client.id ??= this.container.client.user?.id ?? null;
1212

13-
this.container.logger.info(`ApplicationCommandRegistries: Initializing...`);
14-
15-
const now = Date.now();
1613
await handleRegistryAPICalls();
17-
const diff = Date.now() - now;
18-
19-
this.container.logger.info(`ApplicationCommandRegistries: Took ${diff.toLocaleString()}ms to initialize.`);
2014
}
2115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Listener } from '../../lib/structures/Listener';
2+
import { Events } from '../../lib/types/Events';
3+
4+
export class CoreEvent extends Listener<typeof Events.ApplicationCommandRegistriesInitialising> {
5+
public constructor(context: Listener.Context) {
6+
super(context, { event: Events.ApplicationCommandRegistriesInitialising, once: true });
7+
}
8+
9+
public run() {
10+
this.container.logger.info('ApplicationCommandRegistries: Initializing...');
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Listener } from '../../lib/structures/Listener';
2+
import { Events } from '../../lib/types/Events';
3+
import type { ApplicationCommandRegistry } from '../../lib/utils/application-commands/ApplicationCommandRegistry';
4+
5+
export class CoreEvent extends Listener<typeof Events.ApplicationCommandRegistriesRegistered> {
6+
public constructor(context: Listener.Context) {
7+
super(context, { event: Events.ApplicationCommandRegistriesRegistered, once: true });
8+
}
9+
10+
public run(_registries: Map<string, ApplicationCommandRegistry>, timeTaken: number) {
11+
this.container.logger.info(`ApplicationCommandRegistries: Took ${timeTaken.toLocaleString()}ms to initialize.`);
12+
}
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.