Skip to content

Commit

Permalink
fix(computeDifferences): do not compute dm_permissions for guild-on…
Browse files Browse the repository at this point in the history
…ly commands (#519)
  • Loading branch information
vladfrangu committed Sep 1, 2022
1 parent 0dc0b89 commit 645df81
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 60 deletions.
10 changes: 5 additions & 5 deletions src/lib/utils/application-commands/ApplicationCommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class ApplicationCommandRegistry {
}

this.debug(`Checking if command "${commandName}" is identical with global ${type} command with id "${globalCommand.id}"`);
await this.handleCommandPresent(globalCommand, builtData, behaviorIfNotEqual);
await this.handleCommandPresent(globalCommand, builtData, behaviorIfNotEqual, null);
} else if (registerOptions.registerCommandIfMissing ?? true) {
this.debug(`Creating new global ${type} command with name "${commandName}"`);
await this.createMissingCommand(commandsManager, builtData, type);
Expand Down Expand Up @@ -306,7 +306,7 @@ export class ApplicationCommandRegistry {
break;
}

await this.handleCommandPresent(existingGuildCommand, builtData, behaviorIfNotEqual);
await this.handleCommandPresent(existingGuildCommand, builtData, behaviorIfNotEqual, guildId);
} else if (registerOptions.registerCommandIfMissing ?? true) {
this.debug(`Creating new guild ${type} command with name "${commandName}" for guild "${guildId}"`);
await this.createMissingCommand(commandsManager, builtData, type, guildId);
Expand All @@ -320,15 +320,15 @@ export class ApplicationCommandRegistry {
applicationCommand: ApplicationCommand,
apiData: InternalAPICall['builtData'],
behaviorIfNotEqual: RegisterBehavior,
guildId?: string
guildId: string | null
) {
let differences: CommandDifference[] = [];

if (behaviorIfNotEqual === RegisterBehavior.VerboseOverwrite) {
const now = Date.now();

// Step 0: compute differences
differences = getCommandDifferences(convertApplicationCommandToApiData(applicationCommand), apiData);
differences = getCommandDifferences(convertApplicationCommandToApiData(applicationCommand), apiData, guildId !== null);

const later = Date.now() - now;
this.debug(`Took ${later}ms to process differences via computing differences`);
Expand All @@ -349,7 +349,7 @@ export class ApplicationCommandRegistry {
const now = Date.now();

// Step 0: compute differences
const areThereDifferences = getCommandDifferencesFast(convertApplicationCommandToApiData(applicationCommand), apiData);
const areThereDifferences = getCommandDifferencesFast(convertApplicationCommandToApiData(applicationCommand), apiData, guildId !== null);

const later = Date.now() - now;
this.debug(`Took ${later}ms to process differences via fast compute differences`);
Expand Down
20 changes: 14 additions & 6 deletions src/lib/utils/application-commands/computeDifferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,24 @@ type APIApplicationCommandChoosableAndAutocompletableTypes = APIApplicationComma
/**
* @returns `true` if there are differences, `false` otherwise
*/
export function getCommandDifferencesFast(existingCommand: RESTPostAPIApplicationCommandsJSONBody, apiData: InternalAPICall['builtData']) {
for (const _ of getCommandDifferences(existingCommand, apiData)) {
export function getCommandDifferencesFast(
existingCommand: RESTPostAPIApplicationCommandsJSONBody,
apiData: InternalAPICall['builtData'],
guildCommand: boolean
) {
for (const _ of getCommandDifferences(existingCommand, apiData, guildCommand)) {
// Return immediately on first difference found (also means we skip all other checks)
return true;
}

return false;
}

export function getCommandDifferences(existingCommand: RESTPostAPIApplicationCommandsJSONBody, apiData: InternalAPICall['builtData']) {
export function getCommandDifferences(
existingCommand: RESTPostAPIApplicationCommandsJSONBody,
apiData: InternalAPICall['builtData'],
guildCommand: boolean
) {
const differences: CommandDifference[] = [];

if (existingCommand.type !== ApplicationCommandType.ChatInput && existingCommand.type) {
Expand All @@ -75,8 +83,8 @@ export function getCommandDifferences(existingCommand: RESTPostAPIApplicationCom
});
}

// Check dmPermission
if ((existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
// Check dmPermission only for non-guild commands
if (!guildCommand && (existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
differences.push({
key: 'dmPermission',
original: String(existingCommand.dm_permission ?? true),
Expand Down Expand Up @@ -159,7 +167,7 @@ export function getCommandDifferences(existingCommand: RESTPostAPIApplicationCom
}

// Check dmPermission
if ((existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
if (!guildCommand && (existingCommand.dm_permission ?? true) !== (casted.dm_permission ?? true)) {
differences.push({
key: 'dmPermission',
original: String(existingCommand.dm_permission ?? true),
Expand Down
Loading

0 comments on commit 645df81

Please sign in to comment.