Skip to content

Commit

Permalink
feat: add missing props and fetch options
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Apr 3, 2022
1 parent 5599c24 commit 460d809
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
15 changes: 12 additions & 3 deletions packages/discord.js/src/managers/ApplicationCommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class ApplicationCommandManager extends CachedManager {
* Options used to fetch Application Commands from Discord
* @typedef {BaseFetchOptions} FetchApplicationCommandOptions
* @property {Snowflake} [guildId] The guild's id to fetch commands for, for when the guild is not cached
* @property {LocaleString} [locale] The locale to use when fetching this command
* @property {boolean} [withLocalizations] Whether or not to fetch all localization data
*/

/**
Expand All @@ -92,19 +94,26 @@ class ApplicationCommandManager extends CachedManager {
* .then(commands => console.log(`Fetched ${commands.size} commands`))
* .catch(console.error);
*/
async fetch(id, { guildId, cache = true, force = false } = {}) {
async fetch(id, { guildId, cache = true, force = false, locale, withLocalizations } = {}) {
const fetchOptions = {
headers: {
'X-Discord-Locale': locale,
},
query: withLocalizations ? new URLSearchParams({ with_localizations: withLocalizations }) : undefined,
};

if (typeof id === 'object') {
({ guildId, cache = true } = id);
} else if (id) {
if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
const command = await this.client.rest.get(this.commandPath({ id, guildId }));
const command = await this.client.rest.get(this.commandPath({ id, guildId }), fetchOptions);
return this._add(command, cache);
}

const data = await this.client.rest.get(this.commandPath({ guildId }));
const data = await this.client.rest.get(this.commandPath({ guildId }), fetchOptions);
return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection());
}

Expand Down
50 changes: 46 additions & 4 deletions packages/discord.js/src/structures/ApplicationCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class ApplicationCommand extends Base {
this.nameLocalizations ??= null;
}

if ('name_localized' in data) {
/**
* The localized name for this command
* @type {?Object<string, string>}
*/
this.nameLocalized = data.name_localized;
} else {
this.nameLocalized ??= null;
}

if ('description' in data) {
/**
* The description of this command
Expand All @@ -83,13 +93,23 @@ class ApplicationCommand extends Base {
if ('description_localizations' in data) {
/**
* The description localizations for this command
* @type {?Object<string, string>}
* @type {?string}
*/
this.descriptionLocalizations = data.description_localizations;
} else {
this.descriptionLocalizations ??= null;
}

if ('description_localized' in data) {
/**
* The localized description for this command
* @type {?string}
*/
this.descriptionLocalized = data.description_localized;
} else {
this.descriptionLocalized ??= null;
}

if ('options' in data) {
/**
* The options of this command
Expand Down Expand Up @@ -175,7 +195,7 @@ class ApplicationCommand extends Base {
* {@link ApplicationCommandOptionType.String}, {@link ApplicationCommandOptionType.Integer} or
* {@link ApplicationCommandOptionType.Number} option
* @property {boolean} [required] Whether the option is required
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOptionChoiceData[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
* @property {ChannelType[]} [channelTypes] When the option type is channel,
* the allowed types of channels that can be selected
Expand All @@ -185,6 +205,18 @@ class ApplicationCommand extends Base {
* {@link ApplicationCommandOptionType.Number} option
*/

/**
* @typedef {Object} ApplicationCommandOptionChoiceData
* @property {string} name The name of the choice
* @property {Object<string, string>} [nameLocalizations] The localized names for this choice
* @property {string|number} value The value of the choice
*/

/**
* @param {ApplicationCommandOptionChoiceData} ApplicationCommandOptionChoice
* @property {string} [nameLocalized] The localized name for this choice
*/

/**
* Edits this application command.
* @param {ApplicationCommandData} data The data to update the command with
Expand Down Expand Up @@ -407,7 +439,11 @@ class ApplicationCommand extends Base {
* @typedef {Object} ApplicationCommandOption
* @property {ApplicationCommandOptionType} type The type of the option
* @property {string} name The name of the option
* @property {Object<string, string>} [nameLocalizations] The localizations for the option name
* @property {string} [nameLocalized] The localized name for this option
* @property {string} description The description of the option
* @property {Object<string, string>} [descriptionLocalizations] The localizations for the option description
* @property {string} [descriptionLocalized] The localized description for this option
* @property {boolean} [required] Whether the option is required
* @property {boolean} [autocomplete] Whether the autocomplete interaction is enabled for a
* {@link ApplicationCommandOptionType.String}, {@link ApplicationCommandOptionType.Integer} or
Expand All @@ -426,13 +462,14 @@ class ApplicationCommand extends Base {
* A choice for an application command option.
* @typedef {Object} ApplicationCommandOptionChoice
* @property {string} name The name of the choice
* @property {Object<string, string>} nameLocalizations The localized names for this choice
* @property {string} [nameLocalized] The localized name for this choice
* @property {Object<string, string>} [nameLocalizations] The localized names for this choice
* @property {string|number} value The value of the choice
*/

/**
* Transforms an {@link ApplicationCommandOptionData} object into something that can be used with the API.
* @param {ApplicationCommandOptionData} option The option to transform
* @param {ApplicationCommandOptionData|ApplicationCommandOption} option The option to transform
* @param {boolean} [received] Whether this option has been received from Discord
* @returns {APIApplicationCommandOption}
* @private
Expand All @@ -442,13 +479,17 @@ class ApplicationCommand extends Base {
const minValueKey = received ? 'minValue' : 'min_value';
const maxValueKey = received ? 'maxValue' : 'max_value';
const nameLocalizationsKey = received ? 'nameLocalizations' : 'name_localizations';
const nameLocalizedKey = received ? 'nameLocalized' : 'name_localized';
const descriptionLocalizationsKey = received ? 'descriptionLocalizations' : 'description_localizations';
const descriptionLocalizedKey = received ? 'descriptionLocalized' : 'description_localized';
return {
type: option.type,
name: option.name,
[nameLocalizationsKey]: option.nameLocalizations ?? option.name_localizations,
[nameLocalizedKey]: option.nameLocalized ?? option.name_localized,
description: option.description,
[descriptionLocalizationsKey]: option.descriptionLocalizations ?? option.description_localizations,
[descriptionLocalizedKey]: option.descriptionLocalized ?? option.description_localized,
required:
option.required ??
(option.type === ApplicationCommandOptionType.Subcommand ||
Expand All @@ -458,6 +499,7 @@ class ApplicationCommand extends Base {
autocomplete: option.autocomplete,
choices: option.choices?.map(choice => ({
name: choice.name,
[nameLocalizedKey]: choice.nameLocalized ?? choice.name_localized,
[nameLocalizationsKey]: choice.nameLocalizations ?? choice.name_localizations,
value: choice.value,
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AutocompleteInteraction extends Interaction {

/**
* Sends results for the autocomplete of this interaction.
* @param {ApplicationCommandOptionChoice[]} options The options for the autocomplete
* @param {ApplicationCommandOptionChoiceData[]} options The options for the autocomplete
* @returns {Promise<void>}
* @example
* // respond to autocomplete interaction
Expand Down
22 changes: 16 additions & 6 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import {
APIEmbedFooter,
APIEmbedImage,
LocalizationMap,
LocaleString,
} from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -289,13 +290,15 @@ export class ApplicationCommand<PermissionsFetchType = {}> extends Base {
public defaultPermission: boolean;
public description: string;
public descriptionLocalizations: LocalizationMap | null;
public descriptionLocalized: string | null;
public guild: Guild | null;
public guildId: Snowflake | null;
public get manager(): ApplicationCommandManager;
public id: Snowflake;
public name: string;
public nameLocalizations: LocalizationMap | null;
public options: ApplicationCommandOption[];
public nameLocalized: string | null;
public options: (ApplicationCommandOption & { nameLocalized?: string; descriptionLocalized?: string })[];
public permissions: ApplicationCommandPermissionsManager<
PermissionsFetchType,
PermissionsFetchType,
Expand Down Expand Up @@ -865,7 +868,7 @@ export class AutocompleteInteraction<Cached extends CacheType = CacheType> exten
public inGuild(): this is AutocompleteInteraction<'raw' | 'cached'>;
public inCachedGuild(): this is AutocompleteInteraction<'cached'>;
public inRawGuild(): this is AutocompleteInteraction<'raw'>;
public respond(options: ApplicationCommandOptionChoice[]): Promise<void>;
public respond(options: ApplicationCommandOptionChoiceData[]): Promise<void>;
}

export class CommandInteractionOptionResolver<Cached extends CacheType = CacheType> {
Expand Down Expand Up @@ -926,7 +929,7 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
public getMessage(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['message']>;
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
public getFocused(getFull: true): ApplicationCommandOptionChoice;
public getFocused(getFull: true): ApplicationCommandOptionChoiceData;
public getFocused(getFull?: boolean): string | number;
}

Expand Down Expand Up @@ -3481,13 +3484,13 @@ export interface ApplicationCommandAutocompleteOption extends Omit<BaseApplicati

export interface ApplicationCommandChoicesData extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoice[];
choices?: ApplicationCommandOptionChoiceData[];
autocomplete?: false;
}

export interface ApplicationCommandChoicesOption extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: Exclude<CommandOptionChoiceResolvableType, ApplicationCommandOptionType>;
choices?: ApplicationCommandOptionChoice[];
choices?: ApplicationCommandOptionChoiceData[];
autocomplete?: false;
}

Expand Down Expand Up @@ -3557,12 +3560,16 @@ export type ApplicationCommandOption =
| ApplicationCommandAttachmentOption
| ApplicationCommandSubCommand;

export interface ApplicationCommandOptionChoice {
export interface ApplicationCommandOptionChoiceData {
name: string;
nameLocalizations?: LocalizationMap;
value: string | number;
}

export interface ApplicationCommandOptionChoice extends ApplicationCommandOptionChoiceData {
nameLocalized?: string;
}

export interface ApplicationCommandPermissionData {
id: Snowflake;
type: ApplicationCommandPermissionType;
Expand Down Expand Up @@ -4207,6 +4214,8 @@ export interface EscapeMarkdownOptions {

export interface FetchApplicationCommandOptions extends BaseFetchOptions {
guildId?: Snowflake;
locale?: LocaleString;
withLocalizations?: boolean;
}

export interface FetchArchivedThreadOptions {
Expand Down Expand Up @@ -5346,6 +5355,7 @@ export {
InviteTargetType,
Locale,
LocalizationMap,
LocaleString,
MessageType,
MessageFlags,
OAuth2Scopes,
Expand Down

0 comments on commit 460d809

Please sign in to comment.