From 586d652c168e79640e810eabe4c43d4359b82663 Mon Sep 17 00:00:00 2001 From: Cole Date: Mon, 12 Dec 2016 21:34:19 -0600 Subject: [PATCH] Quality of life changes (#968) * + Added function to get a user's default avatar + Added HOST constant to Constants + Added assets endpoint + Added quality of life function to get a user's avatar or default avatar + Added quality of life function to get member's nickname or username * * Fixed invocation of a getter. * Fixed lint issue * Made the API constant use the HOST constant for DRY-ness Changed DOC comment to be more descriptive * Update GuildMember.js --- src/structures/GuildMember.js | 9 +++++++++ src/structures/User.js | 20 ++++++++++++++++++++ src/util/Constants.js | 12 +++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index 70746c330..df74613b0 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -182,6 +182,15 @@ class GuildMember { return this.user.id; } + /** + * The nickname of the member, or their username if they don't have one + * @type {string} + * @readonly + */ + get displayName() { + return this.nickname || this.user.username; + } + /** * The overall set of permissions for the guild member, taking only roles into account * @type {EvaluatedPermissions} diff --git a/src/structures/User.js b/src/structures/User.js index 54c57ee08..6a7731f91 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -104,6 +104,26 @@ class User { return Constants.Endpoints.avatar(this.id, this.avatar); } + /** + * A link to the user's default avatar + * @type {string} + * @readonly + */ + get defaultAvatarURL() { + let defaultAvatars = Object.values(Constants.DefaultAvatars); + let defaultAvatar = this.discriminator % defaultAvatars.length; + return Constants.endpoints.assets(`${defaultAvatars[defaultAvatar]}.png`); + } + + /** + * A link to the user's avatar if they have one. Otherwise a link to their default avatar will be returned + * @type {string} + * @readonly + */ + get displayAvatarURL() { + return this.avatarURL || this.defaultAvatarURL; + } + /** * The note that is set for the user * This is only available when using a user account. diff --git a/src/util/Constants.js b/src/util/Constants.js index 3470a53e1..462593bda 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -71,7 +71,8 @@ exports.Errors = { }; const PROTOCOL_VERSION = exports.PROTOCOL_VERSION = 6; -const API = exports.API = `https://discordapp.com/api/v${PROTOCOL_VERSION}`; +const HOST = exports.HOST = `https://discordapp.com`; +const API = exports.API = `${HOST}/api/v${PROTOCOL_VERSION}`; const Endpoints = exports.Endpoints = { // general login: `${API}/auth/login`, @@ -80,6 +81,7 @@ const Endpoints = exports.Endpoints = { botGateway: `${API}/gateway/bot`, invite: (id) => `${API}/invite/${id}`, inviteLink: (id) => `https://discord.gg/${id}`, + assets: (asset) => `${HOST}/assets/${asset}`, CDN: 'https://cdn.discordapp.com', // users @@ -311,6 +313,14 @@ exports.MessageTypes = { 6: 'PINS_ADD', }; +exports.DefaultAvatars = { + BLURPLE: '6debd47ed13483642cf09e832ed0bc1b', + GREY: '322c936a8c8be1b803cd94861bdfa868', + GREEN: 'dd4dbc0016779df1378e7812eabaa04d', + ORANGE: '0e291f67c9274a1abdddeb3fd919cbaa', + RED: '1cbd08c76f8af6dddce02c5138971129', +}; + const PermissionFlags = exports.PermissionFlags = { CREATE_INSTANT_INVITE: 1 << 0, KICK_MEMBERS: 1 << 1,