Skip to content

Commit 61f2c07

Browse files
authored
fix: only fetch guild ids for commands that require them (#523)
* fix: only fetch guild ids for commands that require them * fix: check for null guilds * chore: requested change
1 parent 645df81 commit 61f2c07

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

src/lib/structures/Command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
267267
// Reset the registry's contents
268268
registry.chatInputCommands.clear();
269269
registry.contextMenuCommands.clear();
270+
registry.guildIdsToFetch.clear();
270271
registry['apiCalls'].length = 0;
271272

272273
// Reload the command
@@ -292,7 +293,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
292293
}
293294

294295
// Re-initialize the store and the API data (insert in the store handles the register method)
295-
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters();
296+
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(updatedRegistry.guildIdsToFetch);
296297

297298
// Handle the API calls
298299
// eslint-disable-next-line @typescript-eslint/dot-notation

src/lib/structures/CommandStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AliasStore } from '@sapphire/pieces';
2-
import { registries } from '../utils/application-commands/ApplicationCommandRegistries';
2+
import { allGuildIdsToFetchCommandsFor, registries } from '../utils/application-commands/ApplicationCommandRegistries';
33
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
44
import { Command } from './Command';
55

@@ -51,7 +51,7 @@ export class CommandStore extends AliasStore<Command> {
5151
// If we don't have an application, that means this was called on login...
5252
if (!this.container.client.application) return;
5353

54-
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters();
54+
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(allGuildIdsToFetchCommandsFor);
5555

5656
for (const command of this.values()) {
5757
// eslint-disable-next-line @typescript-eslint/dot-notation

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export let defaultBehaviorWhenNotIdentical = RegisterBehavior.Overwrite;
88

99
export const registries = new Map<string, ApplicationCommandRegistry>();
1010

11+
export const allGuildIdsToFetchCommandsFor = new Set<string>();
12+
1113
/**
1214
* Acquires a registry for a command by its name.
1315
* @param commandName The name of the command.
@@ -51,7 +53,7 @@ export async function handleRegistryAPICalls() {
5153
}
5254
}
5355

54-
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters();
56+
const { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(allGuildIdsToFetchCommandsFor);
5557

5658
for (const registry of registries.values()) {
5759
// eslint-disable-next-line @typescript-eslint/dot-notation

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
SlashCommandSubcommandsOnlyBuilder
66
} from '@discordjs/builders';
77
import { container } from '@sapphire/pieces';
8+
import { isNullishOrEmpty } from '@sapphire/utilities';
89
import {
910
ApplicationCommandType,
1011
RESTPostAPIChatInputApplicationCommandsJSONBody,
@@ -20,7 +21,7 @@ import type {
2021
UserApplicationCommandData
2122
} from 'discord.js';
2223
import { InternalRegistryAPIType, RegisterBehavior } from '../../types/Enums';
23-
import { getDefaultBehaviorWhenNotIdentical } from './ApplicationCommandRegistries';
24+
import { allGuildIdsToFetchCommandsFor, getDefaultBehaviorWhenNotIdentical } from './ApplicationCommandRegistries';
2425
import { CommandDifference, getCommandDifferences, getCommandDifferencesFast } from './computeDifferences';
2526
import { convertApplicationCommandToApiData, normalizeChatInputCommand, normalizeContextMenuCommand } from './normalizeInputs';
2627

@@ -29,6 +30,7 @@ export class ApplicationCommandRegistry {
2930

3031
public readonly chatInputCommands = new Set<string>();
3132
public readonly contextMenuCommands = new Set<string>();
33+
public readonly guildIdsToFetch = new Set<string>();
3234

3335
private readonly apiCalls: InternalAPICall[] = [];
3436

@@ -66,6 +68,13 @@ export class ApplicationCommandRegistry {
6668
}
6769
}
6870

71+
if (!isNullishOrEmpty(options?.guildIds)) {
72+
for (const id of options!.guildIds) {
73+
this.guildIdsToFetch.add(id);
74+
allGuildIdsToFetchCommandsFor.add(id);
75+
}
76+
}
77+
6978
return this;
7079
}
7180

@@ -93,6 +102,13 @@ export class ApplicationCommandRegistry {
93102
}
94103
}
95104

105+
if (!isNullishOrEmpty(options?.guildIds)) {
106+
for (const id of options!.guildIds) {
107+
this.guildIdsToFetch.add(id);
108+
allGuildIdsToFetchCommandsFor.add(id);
109+
}
110+
}
111+
96112
return this;
97113
}
98114

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { container } from '@sapphire/pieces';
22
import type { ApplicationCommand, ApplicationCommandManager, Collection } from 'discord.js';
33

4-
export async function getNeededRegistryParameters() {
4+
export async function getNeededRegistryParameters(guildIds: Set<string> = new Set()) {
55
const { client } = container;
66

77
const applicationCommands = client.application!.commands;
88
const globalCommands = await applicationCommands.fetch({ withLocalizations: true });
9-
const guildCommands = await fetchGuildCommands(applicationCommands);
9+
const guildCommands = await fetchGuildCommands(applicationCommands, guildIds);
1010

1111
return {
1212
applicationCommands,
@@ -15,10 +15,10 @@ export async function getNeededRegistryParameters() {
1515
};
1616
}
1717

18-
async function fetchGuildCommands(commands: ApplicationCommandManager) {
18+
async function fetchGuildCommands(commands: ApplicationCommandManager, guildIds: Set<string>) {
1919
const map = new Map<string, Collection<string, ApplicationCommand>>();
2020

21-
for (const [guildId, guild] of commands.client.guilds.cache.entries()) {
21+
for (const guildId of guildIds) {
2222
try {
2323
const guildCommands = await commands.fetch({ guildId, withLocalizations: true });
2424
map.set(guildId, guildCommands);
@@ -28,6 +28,7 @@ async function fetchGuildCommands(commands: ApplicationCommandManager) {
2828
if (preventFailedToFetchLogForGuilds === true) continue;
2929

3030
if (Array.isArray(preventFailedToFetchLogForGuilds) && !preventFailedToFetchLogForGuilds?.includes(guildId)) {
31+
const guild = container.client.guilds.resolve(guildId) ?? { name: 'Guild not in cache' };
3132
container.logger.warn(
3233
`ApplicationCommandRegistries: Failed to fetch guild commands for guild "${guild.name}" (${guildId}).`,
3334
'Make sure to authorize your application with the "applications.commands" scope in that guild.'

0 commit comments

Comments
 (0)