Managers_UserManager.js

'user strict'
const Discord = require('discord.js')
const { UserCard } = require('../Structures/UserCard')
const { ErrorCodes } = require('../Errors/ErrorCodes')
const { BaseManager } = require('./BaseManager')
const { MissingValue, InvalidValue } = require('../Errors/LME')

/** @typedef {import('../../typings').UserEntry} UserEntry */

/**
 * Manages the users.
 * @extends {BaseManager<UserCard>}
 */
class UserManager extends BaseManager {
  /**
   * @param {Discord.Client} client - The Discord Client.
   * @param {UserEntry[]} iterable - Array of users.
   */
  constructor (client, iterable) {
    super(client, UserCard)

    if (!iterable) throw new MissingValue(ErrorCodes.MissingArgument, 'iterable')

    if (!Array.isArray(iterable)) throw new InvalidValue(ErrorCodes.InvalidValue, 'iterable', 'an array')

    for (const item of iterable) {
      this._add(item)
    }
  }

  /**
   * Add a user to the cache.
   * @param {UserEntry} data The user to add.
   * @returns {UserCard} - The added user.
   * @private
   * @ignore
   */
  _add (data) {
    if (typeof data !== 'object') throw new TypeError('User must be an object.')
    const existing = this.cache.has(data.id)
    if (existing) return existing

    // eslint-disable-next-line new-cap
    const entry = new this.holds(this.client, data)

    this.cache.set(entry.id, entry)

    return entry
  }
}

module.exports = { UserManager }