Repository: discordjs/discord.js
File: packages/discord.js/src/managers/ApplicationCommandManager.js (JavaScript)

Code before:
   * // Fetch a single command
   * client.application.commands.fetch('123456789012345678')
   *   .then(command => console.log(`Fetched command ${command.name}`))
   *   .catch(console.error);
   * @example
   * // Fetch all commands
   * client.application.commands.fetch()
   *   .then(commands => console.log(`Fetched ${commands.size} commands`))
   *   .catch(console.error);
   * @example
   * // Fetch all commands in a guild
   * guild.commands.fetch()
   *   .then(commands => console.log(`Fetched ${commands.size} commands`))
   *   .catch(console.error);
   * @example
   * // Fetch a single command without checking cache
   * guild.commands.fetch({ id: '123456789012345678', force: true })
   *   .then(command => console.log(`Fetched command ${command.name}`))
   *   .catch(console.error)
   */
  async fetch(options) {
    if (!options) return this._fetchMany();

    const { cache, force, guildId, id, locale, withLocalizations } = options;

    if (typeof options === 'string' || typeof id === 'string') {
      return this._fetchSingle({ cache, force, guildId, id: id ?? options });
    }

    return this._fetchMany({ cache, guildId, locale, withLocalizations });
  }

  async _fetchSingle({ cache, force = false, guildId, id }) {
    if (!force) {
      const existing = this.cache.get(id);
      if (existing) return existing;
    }

    const command = await this.client.rest.get(this.commandPath({ id, guildId }));
    return this._add(command, cache);
  }

  async _fetchMany({ cache, guildId, locale, withLocalizations } = {}) {
    const data = await this.client.rest.get(this.commandPath({ guildId }), {
      headers: {
        'X-Discord-Locale': locale,
      },
      query: makeURLSearchParams({ with_localizations: withLocalizations }),
    });

    return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection());

Review comment (suggestion):
This might technically work but, if an id is passed instead of an object we'll be destructuring all these properties from a string.
I'd rather check if `typeof options === 'string'` before this and handle accordingly.

Code after:
   * // Fetch a single command
   * client.application.commands.fetch('123456789012345678')
   *   .then(command => console.log(`Fetched command ${command.name}`))
   *   .catch(console.error);
   * @example
   * // Fetch all commands
   * client.application.commands.fetch()
   *   .then(commands => console.log(`Fetched ${commands.size} commands`))
   *   .catch(console.error);
   * @example
   * // Fetch all commands in a guild
   * guild.commands.fetch()
   *   .then(commands => console.log(`Fetched ${commands.size} commands`))
   *   .catch(console.error);
   * @example
   * // Fetch a single command without checking cache
   * guild.commands.fetch({ id: '123456789012345678', force: true })
   *   .then(command => console.log(`Fetched command ${command.name}`))
   *   .catch(console.error)
   */
  async fetch(options) {
    if (!options) return this._fetchMany();

    if (typeof options === 'string') return this._fetchSingle({ id: options });

    const { cache, force, guildId, id, locale, withLocalizations } = options;

    if (id) return this._fetchSingle({ cache, force, guildId, id });

    return this._fetchMany({ cache, guildId, locale, withLocalizations });
  }

  async _fetchSingle({ cache, force = false, guildId, id }) {
    if (!force) {
      const existing = this.cache.get(id);
      if (existing) return existing;
    }

    const command = await this.client.rest.get(this.commandPath({ id, guildId }));
    return this._add(command, cache);
  }

  async _fetchMany({ cache, guildId, locale, withLocalizations } = {}) {
    const data = await this.client.rest.get(this.commandPath({ guildId }), {
      headers: {
        'X-Discord-Locale': locale,
      },
      query: makeURLSearchParams({ with_localizations: withLocalizations }),
    });

    return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection());