Files
discord.js/src/structures/UserProfile.js
Will Nelson cf7dcba1a5 Add toJSON methods (#1859)
* tojson things

* fix client

* ignore private properties

* remove extra property descriptors

* handle primitive flattening

* remove unused import

* add toJSON to collections

* reduce stateful props

* state

* allow custom prop names when flattening

* fix client

* fix build

* fix flatten docs

* remove guild.available, cleanup permissions, remove arbitrary id reduction

* fix util import

* add valueOf as needed, update member props

* fix incorrect merge

* update permissionoverwrites and permissions

remove serialization of permissions in PermissionOverwrites#toJSON.
change Permissions#toJSON to serialize permissions, by default excluding
admin checks.

* change Permissions#toJSON to return the primitive

* Permissions#toJSON explicitly return bitfield
2018-03-01 23:00:21 -06:00

84 lines
1.9 KiB
JavaScript

const Collection = require('../util/Collection');
const { UserFlags } = require('../util/Constants');
const UserConnection = require('./UserConnection');
const Base = require('./Base');
/**
* Represents a user's profile on Discord.
* @extends {Base}
*/
class UserProfile extends Base {
constructor(user, data) {
super(user.client);
/**
* The owner of the profile
* @type {User}
*/
this.user = user;
/**
* The guilds that the client user and the user share
* @type {Collection<Snowflake, Guild>}
*/
this.mutualGuilds = new Collection();
/**
* The user's connections
* @type {Collection<Snowflake, UserConnection>}
*/
this.connections = new Collection();
this._patch(data);
}
_patch(data) {
/**
* If the user has Discord Premium
* @type {boolean}
*/
this.premium = Boolean(data.premium_since);
/**
* The Bitfield of the users' flags
* @type {number}
* @private
*/
this._flags = data.user.flags;
/**
* The date since which the user has had Discord Premium
* @type {?Date}
*/
this.premiumSince = data.premium_since ? new Date(data.premium_since) : null;
for (const guild of data.mutual_guilds) {
if (this.client.guilds.has(guild.id)) {
this.mutualGuilds.set(guild.id, this.client.guilds.get(guild.id));
}
}
for (const connection of data.connected_accounts) {
this.connections.set(connection.id, new UserConnection(this.user, connection));
}
}
/**
* The flags the user has
* @type {UserFlags[]}
* @readonly
*/
get flags() {
const flags = [];
for (const [name, flag] of Object.entries(UserFlags)) {
if ((this._flags & flag) === flag) flags.push(name);
}
return flags;
}
toJSON() {
return super.toJSON({ flags: true });
}
}
module.exports = UserProfile;