Skip to content

Commit

Permalink
refactor: remove chatInputCommandOptions command option (#464)
Browse files Browse the repository at this point in the history
* refactor: remove `chatInputCommandOptions` command option

* chore: obligatory placeholder commit message

permalink: http://whatthecommit.com/8792f98b7cd2f6d20cd68b2a47935b17
  • Loading branch information
favna committed Jun 26, 2022
1 parent 189329a commit 60ce845
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 82 deletions.
71 changes: 11 additions & 60 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import * as Lexure from 'lexure';
import { Args } from '../parsers/Args';
import { BucketScope, RegisterBehavior } from '../types/Enums';
import { acquire, getDefaultBehaviorWhenNotIdentical } from '../utils/application-commands/ApplicationCommandRegistries';
import { acquire } from '../utils/application-commands/ApplicationCommandRegistries';
import type { ApplicationCommandRegistry } from '../utils/application-commands/ApplicationCommandRegistry';
import { emitRegistryError } from '../utils/application-commands/emitRegistryError';
import { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';
Expand Down Expand Up @@ -68,13 +68,6 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
*/
public readonly applicationCommandRegistry = acquire(this.name);

/**
* Options used to easily register chat input commands
* @since 3.0.0
* @private
*/
public readonly chatInputCommandOptions: CommandChatInputRegisterShortcut;

/**
* The lexer to be used for command parsing
* @since 1.0.0
Expand Down Expand Up @@ -122,11 +115,6 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.

this.preconditions = new PreconditionContainerArray(options.preconditions);
this.parseConstructorPreConditions(options);

this.chatInputCommandOptions = options.chatInputCommand ?? {
register: false,
behaviorWhenNotIdentical: getDefaultBehaviorWhenNotIdentical()
};
}

/**
Expand Down Expand Up @@ -227,33 +215,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
* Registers the application commands that should be handled by this command.
* @param registry This command's registry
*/
public registerApplicationCommands(registry: ApplicationCommandRegistry): Awaitable<void> {
if (this.chatInputCommandOptions.register) {
registry.registerChatInputCommand(
(builder) => {
builder.setName(this.name).setDescription(this.description);

if (Reflect.has(this.chatInputCommandOptions, 'defaultPermission')) {
builder.setDefaultPermission(this.chatInputCommandOptions.defaultPermission!);
}

if (this.chatInputCommandOptions.nameLocalizations) {
builder.setNameLocalizations(this.chatInputCommandOptions.nameLocalizations);
}

if (this.chatInputCommandOptions.descriptionLocalizations) {
builder.setDescriptionLocalizations(this.chatInputCommandOptions.descriptionLocalizations);
}
},
{
behaviorWhenNotIdentical: this.chatInputCommandOptions.behaviorWhenNotIdentical,
guildIds: this.chatInputCommandOptions.guildIds,
idHints: this.chatInputCommandOptions.idHints,
registerCommandIfMissing: true
}
);
}
}
public registerApplicationCommands?(registry: ApplicationCommandRegistry): Awaitable<void>;

/**
* Type-guard that ensures the command supports message commands by checking if the handler for it is present
Expand Down Expand Up @@ -318,13 +280,15 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.

const updatedRegistry = updatedPiece.applicationCommandRegistry;

// Rerun the registry
try {
await updatedPiece.registerApplicationCommands(updatedRegistry);
} catch (err) {
emitRegistryError(err, this);
// No point on continuing
return;
if (updatedPiece.registerApplicationCommands) {
// Rerun the registry
try {
await updatedPiece.registerApplicationCommands(updatedRegistry);
} catch (err) {
emitRegistryError(err, updatedPiece);
// No point on continuing
return;
}
}

// Re-initialize the store and the API data (insert in the store handles the register method)
Expand Down Expand Up @@ -740,19 +704,6 @@ export interface CommandOptions extends AliasPiece.Options, FlagStrategyOptions
* @default true
*/
typing?: boolean;
/**
* Shortcuts for registering simple chat input commands
*
* :::warn
*
* You should only use this if your command does not take in options, and is just a chat input one.
* Otherwise, please read the [guide about registering application commands](https://www.sapphirejs.dev/docs/Guide/commands/registering-application-commands) instead.
*
* :::
*
* @since 3.0.0
*/
chatInputCommand?: CommandChatInputRegisterShortcut;
}

export interface CommandChatInputRegisterShortcut {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export async function handleRegistryAPICalls() {
const commandStore = container.stores.get('commands');

for (const command of commandStore.values()) {
try {
await command.registerApplicationCommands(command.applicationCommandRegistry);
} catch (error) {
emitRegistryError(error, command);
if (command.registerApplicationCommands) {
try {
await command.registerApplicationCommands(command.applicationCommandRegistry);
} catch (error) {
emitRegistryError(error, command);
}
}
}

Expand Down
20 changes: 2 additions & 18 deletions src/lib/utils/application-commands/ApplicationCommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,8 @@ export class ApplicationCommandRegistry {
) {
// Early return for no API calls
if (this.apiCalls.length === 0) {
const { command } = this;

// If we have no command piece in store, then we simply return (can happen if the registry is used directly)
if (!command) {
this.trace('No API calls to run, and no command to register');
return;
}

// If this is set to true, we expected at least one command to be registered, and this seems to usually fix it for people using TS 🤷
// That, and we haven't had any JS users yet reporting issues with this, so we'll just leave it in for now
if (command.chatInputCommandOptions.register) {
this.warn(
'Expected command to have at least one command registered since chatInputCommandOptions.register is set to true, but none were actually registered.',
"If you're using TypeScript, please try clearing the compiled code output folder, re-building your project, and restarting."
);
} else {
this.trace('No API calls to run, and no command to register');
}
// If we have no API calls to do then we simply return (can happen if the registry is used directly)
this.trace('No API calls to run, and no command to register');

return;
}
Expand Down

0 comments on commit 60ce845

Please sign in to comment.