fix(User): Profile data (API) change

- Add GuildMember bio, banner, ...
This commit is contained in:
March 7th 2022-08-09 10:42:30 +07:00
parent 81f4e0bd2d
commit de5fe927a3
4 changed files with 86 additions and 7 deletions

View File

@ -97,6 +97,33 @@ class GuildMember extends Base {
} }
} }
_ProfilePatch(data) {
if ('accent_color' in data) {
/**
* The member's accent color
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?number}
*/
this.accentColor = data.accent_color;
}
if ('banner' in data) {
/**
* The member's banner hash
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?string}
*/
this.banner = data.banner;
}
if ('bio' in data) {
/**
* The member's biography (About me)
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?string}
*/
this.bio = data.bio;
}
}
_clone() { _clone() {
const clone = super._clone(); const clone = super._clone();
clone._roles = this._roles.slice(); clone._roles = this._roles.slice();
@ -170,6 +197,21 @@ class GuildMember extends Base {
return this.client.rest.cdn.GuildMemberAvatar(this.guild.id, this.id, this.avatar, format, size, dynamic); return this.client.rest.cdn.GuildMemberAvatar(this.guild.id, this.id, this.avatar, format, size, dynamic);
} }
/**
* A link to the user's banner.
* <info>This method will throw an error if called before the user is force fetched Profile.
* See {@link GuildMember#banner} for more info</info>
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
bannerURL({ format, size, dynamic } = {}) {
if (typeof this.banner === 'undefined') {
throw new Error('USER_BANNER_NOT_FETCHED');
}
if (!this.banner) return null;
return this.client.rest.cdn.GuildMemberBanner(this.guild.id, this.id, this.banner, format, size, dynamic);
}
/** /**
* A link to the member's guild avatar if they have one. * A link to the member's guild avatar if they have one.
* Otherwise, a link to their {@link User#displayAvatarURL} will be returned. * Otherwise, a link to their {@link User#displayAvatarURL} will be returned.
@ -489,6 +531,8 @@ class GuildMember extends Base {
this.joinedTimestamp === member.joinedTimestamp && this.joinedTimestamp === member.joinedTimestamp &&
this.nickname === member.nickname && this.nickname === member.nickname &&
this.avatar === member.avatar && this.avatar === member.avatar &&
this.accentColor === member.accentColor &&
this.bio === member.bio &&
this.pending === member.pending && this.pending === member.pending &&
this.communicationDisabledUntilTimestamp === member.communicationDisabledUntilTimestamp && this.communicationDisabledUntilTimestamp === member.communicationDisabledUntilTimestamp &&
(this._roles === member._roles || (this._roles === member._roles ||

View File

@ -6,7 +6,7 @@ const ClientApplication = require('./ClientApplication');
const VoiceState = require('./VoiceState'); const VoiceState = require('./VoiceState');
const TextBasedChannel = require('./interfaces/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { Error } = require('../errors'); const { Error } = require('../errors');
const { RelationshipTypes } = require('../util/Constants'); const { RelationshipTypes, NitroType } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil'); const SnowflakeUtil = require('../util/SnowflakeUtil');
const UserFlags = require('../util/UserFlags'); const UserFlags = require('../util/UserFlags');
@ -41,11 +41,13 @@ class User extends Base {
/** /**
* Accounts connected to this user * Accounts connected to this user
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?ConnectionAccount[]} * @type {?ConnectionAccount[]}
*/ */
this.connectedAccounts = []; this.connectedAccounts = [];
/** /**
* Time that User has nitro (Unix Timestamp) * Time that User has nitro (Unix Timestamp)
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?number} * @type {?number}
* @readonly * @readonly
*/ */
@ -58,12 +60,14 @@ class User extends Base {
this.premiumGuildSince = null; this.premiumGuildSince = null;
/** /**
* About me (User) * About me (User)
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {?string} * @type {?string}
* @readonly * @readonly
*/ */
this.bio = null; this.bio = null;
/** /**
* This user is on the same servers as Client User * This user is on the same servers as Client User
* <info>The user must be force fetched for this property to be present or be updated</info>
* @type {Collection<Snowflake, Object>} * @type {Collection<Snowflake, Object>}
* @readonly * @readonly
*/ */
@ -216,8 +220,23 @@ class User extends Base {
this.premiumGuildSince = date.getTime(); this.premiumGuildSince = date.getTime();
} }
if ('bio' in data.user) { if ('bio' in data.user_profile || 'bio' in data.user) {
this.bio = data.user.bio; this.bio = data.user_profile || data.user.bio;
}
if ('premium_type' in data) {
const nitro = NitroType[data.premium_type];
/**
* Nitro type of the user.
* @type {NitroType}
*/
this.nitroType = nitro ?? `UNKNOWN_${data.premium_type}`;
}
if ('guild_member_profile' in data && 'guild_member' in data) {
const guild = this.client.guilds.cache.get(data.guild_member_profile.guild_id);
const member = guild?.members._add(data.guild_member);
member._ProfilePatch(data.guild_member_profile);
} }
this.mutualGuilds = new Collection(data.mutual_guilds.map(obj => [obj.id, obj])); this.mutualGuilds = new Collection(data.mutual_guilds.map(obj => [obj.id, obj]));
@ -226,11 +245,22 @@ class User extends Base {
/** /**
* Get profile from Discord, if client is in a server with the target. * Get profile from Discord, if client is in a server with the target.
* @type {User} * @type {User}
* @param {Snowflake | null} guildId The guild id to get the profile from
* @returns {Promise<User>} * @returns {Promise<User>}
*/ */
async getProfile() { async getProfile(guildId) {
if (this.client.bot) throw new Error('INVALID_BOT_METHOD'); if (this.client.bot) throw new Error('INVALID_BOT_METHOD');
const data = await this.client.api.users(this.id).profile.get(); const query = guildId
? {
with_mutual_guilds: true,
guild_id: guildId,
}
: {
with_mutual_guilds: true,
};
const data = await this.client.api.users(this.id).profile.get({
query,
});
this._ProfilePatch(data); this._ProfilePatch(data);
return this; return this;
} }
@ -436,7 +466,8 @@ class User extends Base {
this.avatar === user.avatar && this.avatar === user.avatar &&
this.flags?.bitfield === user.flags?.bitfield && this.flags?.bitfield === user.flags?.bitfield &&
this.banner === user.banner && this.banner === user.banner &&
this.accentColor === user.accentColor this.accentColor === user.accentColor &&
this.bio === user.bio
); );
} }

View File

@ -169,6 +169,10 @@ exports.Endpoints = {
if (dynamic && hash.startsWith('a_')) format = 'gif'; if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/guilds/${guildId}/users/${memberId}/avatars/${hash}`, { format, size }); return makeImageUrl(`${root}/guilds/${guildId}/users/${memberId}/avatars/${hash}`, { format, size });
}, },
GuildMemberBanner: (guildId, memberId, hash, format = 'webp', size, dynamic = false) => {
if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/guilds/${guildId}/users/${memberId}/banners/${hash}`, { format, size });
},
Banner: (id, hash, format, size, dynamic = false) => { Banner: (id, hash, format, size, dynamic = false) => {
if (dynamic && hash.startsWith('a_')) format = 'gif'; if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size }); return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size });

2
typings/index.d.ts vendored
View File

@ -2938,7 +2938,7 @@ export class User extends PartialTextBasedChannel(Base) {
public unFriend(): Promise<User>; public unFriend(): Promise<User>;
public unBlock(): Promise<User>; public unBlock(): Promise<User>;
public setNote(note?: any): Promise<string>; public setNote(note?: any): Promise<string>;
public getProfile(): Promise<User>; public getProfile(guildId?: Snowflake): Promise<User>;
public toString(): UserMention; public toString(): UserMention;
public ring(): Promise<boolean>; public ring(): Promise<boolean>;
} }