{"size": 964, "ext": "js", "max_stars_repo_path": "src/stores/UserStore.js", "max_stars_repo_name": "fratik/discord.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/stores/UserStore.js", "max_issues_repo_name": "fratik/discord.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/stores/UserStore.js", "max_forks_repo_name": "fratik/discord.js", "max_forks_repo_forks_event_min_datetime": "2018-11-20T21:13:45.000Z", "max_forks_repo_forks_event_max_datetime": "2018-11-20T21:13:45.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.3529411765, "max_line_length": 91, "alphanum_fraction": 0.6597510373}
const DataStore = require('./DataStore');
const User = require('../structures/User');

/**
 * A data store to store User models.
 * @extends {DataStore}
 */
class UserStore extends DataStore {
  create(data, cache = true) {
    const existing = this.get(data.id);
    if (existing) return existing;

    const user = new User(this.client, data);
    if (cache) this.set(user.id, user);
    return user;
  }

  /**
   * Obtains a user from Discord, or the user cache if it's already available.
   * <warn>This is only available when using a bot account.</warn>
   * @param {Snowflake} id ID of the user
   * @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
   * @returns {Promise<User>}
   */
  fetch(id, cache = true) {
    const existing = this.get(id);
    if (existing) return Promise.resolve(existing);

    return this.client.api.users(id).get().then(data => this.create(data, cache));
  }
}

module.exports = UserStore;
