From f3a7f59824e8da0b8de52386e654baa851e24bcb Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 25 Feb 2017 15:29:32 -0500 Subject: [PATCH] Several improvements - Rename Guild#updateChannelPositions -> setChannelPositions - Allow Guild#setChannelPositions to take ChannelResolvables - Prioritise ClientDataResolver#resolveChannel's string case - Minor cleanup --- src/client/ClientDataResolver.js | 2 +- src/client/actions/GuildChannelsPositionUpdate.js | 4 +--- src/client/rest/RESTMethods.js | 10 +++++++++- src/client/voice/player/AudioPlayer.js | 2 +- src/structures/ClientUser.js | 13 +++++++------ src/structures/Guild.js | 14 +++++++------- typings | 2 +- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index 0a9dfd151..2d5ddf944 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -117,9 +117,9 @@ class ClientDataResolver { */ resolveChannel(channel) { if (channel instanceof Channel) return channel; + if (typeof channel === 'string') return this.client.channels.get(channel) || null; if (channel instanceof Message) return channel.channel; if (channel instanceof Guild) return channel.channels.get(channel.id) || null; - if (typeof channel === 'string') return this.client.channels.get(channel) || null; return null; } diff --git a/src/client/actions/GuildChannelsPositionUpdate.js b/src/client/actions/GuildChannelsPositionUpdate.js index 05043550c..8efa5798b 100644 --- a/src/client/actions/GuildChannelsPositionUpdate.js +++ b/src/client/actions/GuildChannelsPositionUpdate.js @@ -8,9 +8,7 @@ class GuildChannelsPositionUpdate extends Action { if (guild) { for (const partialChannel of data.channels) { const channel = guild.roles.get(partialChannel.id); - if (channel) { - channel.position = partialChannel.position; - } + if (channel) channel.position = partialChannel.position; } } diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index d41412a69..6b1e658c3 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -770,7 +770,15 @@ class RESTMethods { } updateChannelPositions(guildID, channels) { - return this.rest.makeRequest('patch', Constants.Endpoints.guildChannels(guildID), true, channels).then(() => + const data = new Array(channels.length); + for (let i = 0; i < channels.length; i++) { + data[i] = { + id: this.client.resolver.resolveChannelID(channels[i].channel), + position: channels[i].position, + }; + } + + return this.rest.makeRequest('patch', Constants.Endpoints.guildChannels(guildID), true, data).then(() => this.client.actions.GuildChannelsPositionUpdate.handle({ guild_id: guildID, channels, diff --git a/src/client/voice/player/AudioPlayer.js b/src/client/voice/player/AudioPlayer.js index bb8094a20..e54a4bd93 100644 --- a/src/client/voice/player/AudioPlayer.js +++ b/src/client/voice/player/AudioPlayer.js @@ -55,7 +55,7 @@ class AudioPlayer extends EventEmitter { * @type {?StreamDispatcher} */ get currentDispatcher() { - return (this.streams.last() || {}).dispatcher; + return this.streams.size > 0 ? this.streams.last().dispatcher || null : null; } destroy() { diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 7a6afaa25..fda331f2f 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -304,15 +304,15 @@ class ClientUser extends User { /** * An object containing either a user or access token, and an optional nickname * @typedef {Object} GroupDMRecipientOptions - * @property {UserResolvable|Snowflake} [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) + * @property {UserResolvable|Snowflake} [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) */ /** - * Create a group dm + * Creates a group DM * @param {GroupDMRecipientOptions[]} recipients The recipients * @returns {Promise} */ @@ -325,6 +325,7 @@ class ClientUser extends User { } /** + * Accepts an invite to join a guild * @param {Invite|string} invite Invite or code to accept * @returns {Promise} Joined guild */ diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 48e65f7d4..7d5000c00 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -639,20 +639,20 @@ class Guild { /** * The data needed for updating a channel's position. * @typedef {Object} ChannelPosition - * @property {Snowflake} id The channel being updated's unique id. - * @property {number} position The new position of the channel. + * @property {ChannelResolvable} channel Channel to update + * @property {number} position New position for the channel */ /** - * Updates this guild's channel positions as a batch. - * @param {Array} channelPositions Array of objects that defines which channel is going where. + * Batch-updates the guild's channels' positions. + * @param {ChannelPosition[]} channelPositions Channel positions to update * @returns {Promise} * @example - * guild.updateChannels([{ id: channelID, position: newChannelIndex }]) - * .then(guild => console.log(`Updated channels for ${guild.id}`)) + * guild.updateChannels([{ channel: channelID, position: newChannelIndex }]) + * .then(guild => console.log(`Updated channel positions for ${guild.id}`)) * .catch(console.error); */ - updateChannelPositions(channelPositions) { + setChannelPositions(channelPositions) { return this.client.rest.methods.updateChannelPositions(this.id, channelPositions); } diff --git a/typings b/typings index 997abfd2d..0acef9491 160000 --- a/typings +++ b/typings @@ -1 +1 @@ -Subproject commit 997abfd2d5b3fbc958f4cc11012a5fe41065aee8 +Subproject commit 0acef94913b3d466a74fa581dbb2cc2b2d64f526