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

Code before:
          this.client.removeListener(Events.GuildMembersChunk, handler);
          this.client.decrementMaxListeners();
          resolve(users && !Array.isArray(users) && fetchedMembers.size ? fetchedMembers.first() : fetchedMembers);
        }
      };

      const rateLimitHandler = payload => {
        if (payload.t === GatewayDispatchEvents.RateLimited && payload.d.meta.nonce === nonce) {
          clearTimeout(timeout);
          this.client.ws.off(WebSocketShardEvents.Dispatch, rateLimitHandler);
          this.client.removeListener(Events.GuildMembersChunk, handler);
          this.client.decrementMaxListeners();
          reject(new DiscordjsError(ErrorCodes.GatewayRequestRateLimited, payload.d));
        }
      };

      this.client.ws.on(WebSocketShardEvents.Dispatch, rateLimitHandler);

      this.client.incrementMaxListeners();
      this.client.on(Events.GuildMembersChunk, handler);

      this.guild.client.ws.send(this.guild.shardId, {
        op: GatewayOpcodes.RequestGuildMembers,
        // eslint-disable-next-line id-length
        d: {
          guild_id: this.guild.id,
          presences,
          user_ids: users,
          query,
          nonce,
          limit,
        },
      });
    });
  }

  /**
   * Fetches the client user as a GuildMember of the guild.
   *
   * @param {BaseFetchOptions} [options] The options for fetching the member
   * @returns {Promise<GuildMember>}
   */
  async fetchMe(options) {
    return this.fetch({ ...options, user: this.client.user.id });
  }

  /**
   * Options used for searching guild members.
   *
   * @typedef {Object} GuildSearchMembersOptions
   * @property {string} query Filter members whose username or nickname start with this query

Review comment (suggestion):
Shouldn't there be a `/src/client/websocket/handlers/RATE_LIMITED.js` for this websocket event so that
- end users can listen to it directly and
- you can use it here directly instead of having to filter the full dispatch event again 

Same applies for the backport where it would replace the usage of listening to `Events.Raw`

Code after:
          fetchedMembers.set(member.id, member);
        }

        if (members.size < 1_000 || (limit && fetchedMembers.size >= limit) || index === chunk.count) {
          cleanup();
          resolve(users && !Array.isArray(users) && fetchedMembers.size ? fetchedMembers.first() : fetchedMembers);
        }
      };

      const requestData = {
        guild_id: this.guild.id,
        presences,
        user_ids: users,
        query,
        nonce,
        limit,
      };

      const rateLimitHandler = payload => {
        if (payload.t === GatewayDispatchEvents.RateLimited && payload.d.meta.nonce === nonce) {
          cleanup();
          reject(new GatewayRateLimitError(payload.d, requestData));
        }
      };

      this.client.ws.on(WebSocketShardEvents.Dispatch, rateLimitHandler);

      this.client.incrementMaxListeners();
      this.client.on(Events.GuildMembersChunk, handler);

      this.guild.client.ws.send(this.guild.shardId, {
        op: GatewayOpcodes.RequestGuildMembers,
        // eslint-disable-next-line id-length
        d: requestData,
      });
    });
  }

  /**
   * Fetches the client user as a GuildMember of the guild.
   *
   * @param {BaseFetchOptions} [options] The options for fetching the member
   * @returns {Promise<GuildMember>}
   */
  async fetchMe(options) {
    return this.fetch({ ...options, user: this.client.user.id });
  }

  /**
   * Options used for searching guild members.
   *