From abfda7c3cc68dd436953abd438fd42ab59a76209 Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Fri, 4 Aug 2017 09:17:46 +0200 Subject: [PATCH] Fixed ClientUser#createGroupDM on user accounts and added some more GroupDMChannel stuff (#1747) * ClientUser#createGroupDM now works like the docs states on user accounts * Added GroupDMChannel#setIcon and fixed null handling for the channel name * Added an s * Don't resolve when icon is falsy and removed useless name trimming * Removed now unnecessary name constant * vscode being great * Added GroupDMChannel#iconURL --- src/structures/ClientUser.js | 3 +-- src/structures/GroupDMChannel.js | 32 +++++++++++++++++++++++++++++++- src/util/Constants.js | 4 ++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index aa7c8edb0..8f75a5522 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -345,7 +345,6 @@ class ClientUser extends User { * An object containing either a user or access token, and an optional nickname. * @typedef {Object} GroupDMRecipientOptions * @property {UserResolvable} [user] User to add to the Group DM - * (only available if a user is creating the DM) * @property {string} [accessToken] Access token to use to add a user to the Group DM * (only available if a bot is creating the DM) * @property {string} [nick] Permanent nickname (only available if a bot is creating the DM) @@ -365,7 +364,7 @@ class ClientUser extends User { if (r.nick) o[r.user ? r.user.id : r.id] = r.nick; return o; }, {}), - } : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u)) }; + } : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user || u.id)) }; return this.client.api.users('@me').channels.post({ data }) .then(res => new GroupDMChannel(this.client, res)); } diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 8c16257b4..9060fa932 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -1,6 +1,7 @@ const Channel = require('./Channel'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const Collection = require('../util/Collection'); +const Constants = require('../util/Constants'); /* { type: 3, @@ -103,6 +104,18 @@ class GroupDMChannel extends Channel { return this.client.users.get(this.ownerID); } + /** + * Gets the URL to this Group DM's icon + * @param {Object} [options={}] Options for the icon url + * @param {string} [options.format='webp'] One of `webp`, `png`, `jpg` + * @param {number} [options.size=128] One of `128`, '256', `512`, `1024`, `2048` + * @returns {?string} + */ + iconURL({ format, size } = {}) { + if (!this.icon) return null; + return Constants.Endpoints.CDN(this.client.options.http.cdn).GDMIcon(this.id, this.icon, format, size); + } + /** * Whether this channel equals another channel. It compares all properties, so for most operations * it is advisable to just compare `channel.id === channel2.id` as it is much faster and is often @@ -133,12 +146,29 @@ class GroupDMChannel extends Channel { edit(data, reason) { return this.client.api.channels[this.id].patch({ data: { - name: (data.name || this.name).trim(), + icon: data.icon, + name: data.name === null ? null : data.name || this.name, }, reason, }).then(() => this); } + /** + * Sets a new icon for this Group DM. + * @param {Base64Resolvable} icon The new icon of this Group DM + * @returns {Promise} + */ + setIcon(icon) { + if (typeof icon === 'string' && icon.startsWith('data:')) { + return this.edit({ icon }); + } else if (!icon) { + return this.edit({ icon: null }); + } else { + return this.client.resolver.resolveBuffer(icon) + .then(data => this.edit({ icon: this.client.resolver.resolveBase64(data) })); + } + } + /** * Sets a new name for this Group DM. * @param {string} name New name for this Group DM diff --git a/src/util/Constants.js b/src/util/Constants.js index cbf6c6a73..9c11f9e85 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -119,6 +119,10 @@ exports.Endpoints = { checkImage({ size, format }); return `${root}/app-icons/${clientID}/${hash}.${format}${size ? `?size=${size}` : ''}`; }, + GDMIcon: (channelID, hash, format = 'webp', size) => { + checkImage({ size, format }); + return `${root}/channel-icons/${channelID}/${hash}.${format}${size ? `?size=${size}` : ''}`; + }, Splash: (guildID, hash, format = 'webp', size) => { checkImage({ size, format }); return `${root}/splashes/${guildID}/${hash}.${format}${size ? `?size=${size}` : ''}`;