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

Code before:

    const data = await this.client.rest.get(Routes.guildMember(this.guild.id, user));
    return this._add(data, cache);
  }

  async _fetchMany({
    limit = 0,
    withPresences: presences,
    users,
    query: initialQuery,
    time = 120e3,
    nonce = DiscordSnowflake.generate().toString(),
  } = {}) {
    if (nonce.length > 32) throw new DiscordjsRangeError(ErrorCodes.MemberFetchNonceLength);

    const query = initialQuery ?? (users ? undefined : '');

    return new Promise((resolve, reject) => {
      const fetchedMembers = new Collection();
      let index = 0;

      const cleanup = () => {
        /* eslint-disable no-use-before-define */
        clearTimeout(timeout);

        this.client.ws.off(WebSocketShardEvents.Dispatch, rateLimitHandler);
        this.client.removeListener(Events.GuildMembersChunk, handler);
        this.client.decrementMaxListeners();
        /* eslint-enable no-use-before-define */
      };

      const timeout = setTimeout(() => {
        cleanup();
        reject(new DiscordjsError(ErrorCodes.GuildMembersTimeout));
      }, time).unref();

      const handler = (members, _, chunk) => {
        if (chunk.nonce !== nonce) return;

        timeout.refresh();
        index++;
        for (const member of members.values()) {
          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);
        }
      };


Review comment (suggestion):
```suggestion
        this.client.ws.removeListener(WebSocketShardEvents.Dispatch, rateLimitHandler);
```
Consistency I guess...

Code after:

    const data = await this.client.rest.get(Routes.guildMember(this.guild.id, user));
    return this._add(data, cache);
  }

  async _fetchMany({
    limit = 0,
    withPresences: presences,
    users,
    query: initialQuery,
    time = 120e3,
    nonce = DiscordSnowflake.generate().toString(),
  } = {}) {
    if (nonce.length > 32) throw new DiscordjsRangeError(ErrorCodes.MemberFetchNonceLength);

    const query = initialQuery ?? (users ? undefined : '');

    return new Promise((resolve, reject) => {
      const fetchedMembers = new Collection();
      let index = 0;

      const cleanup = () => {
        /* eslint-disable no-use-before-define */
        clearTimeout(timeout);

        this.client.ws.removeListener(WebSocketShardEvents.Dispatch, rateLimitHandler);
        this.client.removeListener(Events.GuildMembersChunk, handler);
        this.client.decrementMaxListeners();
        /* eslint-enable no-use-before-define */
      };

      const timeout = setTimeout(() => {
        cleanup();
        reject(new DiscordjsError(ErrorCodes.GuildMembersTimeout));
      }, time).unref();

      const handler = (members, _, chunk) => {
        if (chunk.nonce !== nonce) return;

        timeout.refresh();
        index++;
        for (const member of members.values()) {
          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);
        }
      };