From 0f9741bb14e903998c1f2775670d7ee8264d4513 Mon Sep 17 00:00:00 2001 From: Programmix Date: Mon, 23 May 2016 12:28:17 -0700 Subject: [PATCH 1/2] Update channel improvements Also documented two methods for voice connections. (pause and resume) --- docs/docs_client.rst | 19 +++- docs/docs_serverchannel.rst | 6 ++ docs/docs_voiceconnection.rst | 10 ++ lib/Client/InternalClient.js | 185 +++++++++++--------------------- lib/Structures/ServerChannel.js | 4 + lib/Structures/TextChannel.js | 4 - src/Client/InternalClient.js | 150 ++++++++++---------------- src/Structures/ServerChannel.js | 4 + src/Structures/TextChannel.js | 16 ++- 9 files changed, 166 insertions(+), 232 deletions(-) diff --git a/docs/docs_client.rst b/docs/docs_client.rst index 6cd61c2f9..cb4b0481d 100644 --- a/docs/docs_client.rst +++ b/docs/docs_client.rst @@ -534,7 +534,7 @@ Sets the name and topic of a channel - **error** - error if any occurred setChannelUserLimit(channel, limit, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sets the user limit of a voice channel @@ -544,7 +544,7 @@ Sets the user limit of a voice channel - **error** - error if any occurred setChannelBitrate(channel, bitrate, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sets the bitrate of a voice channel @@ -553,6 +553,21 @@ Sets the bitrate of a voice channel - **callback** - `function` taking the following: - **error** - error if any occurred +updateChannel(channel, data, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Updates the settings of a channel + +- **channel** - A `Channel Resolvable`_ +- **details** - `object` containing any of the following: + - **name** - `String`, the new name of channel + - **topic** - `String`, the new topic of the channel (`TextChannel`_ only) + - **position** - `Number`, the new position of the channel + - **userLimit** - `Number`, the new user limit of the channel (`VoiceChannel`_ only) + - **bitrate** - `Number`, the new bitrate (in kb/s) of the channel (`VoiceChannel`_ only) +- **callback** - `function` taking the following: + - **error** - error if any occurred + startTyping(channel, `callback`) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/docs_serverchannel.rst b/docs/docs_serverchannel.rst index 6cd8a54a1..13bfb5c52 100644 --- a/docs/docs_serverchannel.rst +++ b/docs/docs_serverchannel.rst @@ -51,3 +51,9 @@ mention() ~~~~~~~~~ Returns a `string` that can be used in discord messages to mention a channel. `serverChannel.toString()` defaults to this. + +update(data, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +| **Shortcut of** ``client.updateChannel(channel, data, callback)`` +| **See** client.updateChannel_ diff --git a/docs/docs_voiceconnection.rst b/docs/docs_voiceconnection.rst index 237d8f404..544c58913 100644 --- a/docs/docs_voiceconnection.rst +++ b/docs/docs_voiceconnection.rst @@ -120,6 +120,16 @@ getVolume() Returns the current volume. 1.0 is normal, 0.5 is half as loud, 2.0 is twice as loud. +pause() +~~~~~~~ + +Pauses the current connection's audio. + +resume() +~~~~~~~~ + +Resumes the current connection's audio. + stopPlaying() ~~~~~~~~~~~~~ diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 0c083efa1..571ca2673 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -1543,152 +1543,95 @@ var InternalClient = (function () { //def setChannelTopic InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) { - var _this37 = this; - var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1]; - return this.resolver.resolveChannel(chann).then(function (channel) { - if (channel.type !== "text") { - return Promise.reject(new Error("Channel must be a text channel")); - } + topic = topic || ""; - _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: channel.position, - topic: topic - }).then(function (res) { - return channel.topic = res.topic; - }); - }); + return this.updateChannel(channel, { topic: topic }); }; //def setChannelName - InternalClient.prototype.setChannelName = function setChannelName(chann) { - var _this38 = this; + InternalClient.prototype.setChannelName = function setChannelName(channel, name) { + name = name || "unnamed-channel"; - var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; - - return this.resolver.resolveChannel(chann).then(function (channel) { - _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: name, - position: channel.position, - topic: channel.topic, - user_limit: channel.userLimit, - bitrate: channel.bitrate - }).then(function (res) { - return channel.name = res.name; - }); - }); + return this.updateChannel(channel, { name: name }); }; //def setChannelNameAndTopic - InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) { - var _this39 = this; + InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(channel) { + name = name || "unnamed-channel"; + topic = topic || ""; - var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; - var topic = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2]; - - return this.resolver.resolveChannel(chann).then(function (channel) { - if (channel.type !== "text") { - return Promise.reject(new Error("Channel must be a text channel")); - } - - _this39.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: name, - position: channel.position, - topic: topic - }).then(function (res) { - channel.name = res.name; - channel.topic = res.topic; - }); - }); + return this.updateChannel(channel, { name: name, topic: topic }); }; //def setChannelPosition - InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) { - var _this40 = this; + InternalClient.prototype.setChannelPosition = function setChannelPosition(channel, position) { + position = position || 0; - var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; - - return this.resolver.resolveChannel(chann).then(function (channel) { - _this40.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: position, - topic: channel.topic, - user_limit: channel.userLimit, - bitrate: channel._bitrate - }).then(function (res) { - return channel.position = res.position; - }); - }); + return this.updateChannel(channel, { position: position }); }; //def setChannelUserLimit InternalClient.prototype.setChannelUserLimit = function setChannelUserLimit(channel, limit) { - var _this41 = this; - limit = limit || 0; // default 0 = no limit - if (limit < 0) { - return Promise.reject(new Error("User limit cannot be less than 0")); - } - - if (limit > 99) { - return Promise.reject(new Error("User limit cannot be greater than 99")); - } - - return this.resolver.resolveChannel(channel).then(function (channel) { - if (channel.type !== "voice") { - return Promise.reject(new Error("Channel must be a voice channel")); - } - - return _this41.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: channel.position, - user_limit: limit, - bitrate: channel._bitrate - }).then(function (res) { - return channel.userLimit = limit; - }); - }); + return this.updateChannel(channel, { userLimit: limit }); }; //def setChannelBitrate InternalClient.prototype.setChannelBitrate = function setChannelBitrate(channel, kbitrate) { - var _this42 = this; - kbitrate = kbitrate || 64; // default 64kbps - if (kbitrate < 8 || kbitrate > 96) { - return Promise.reject(new Error("Bitrate must be between 8-96kbps")); - } - - return this.resolver.resolveChannel(channel).then(function (channel) { - if (channel.type !== "voice") { - return Promise.reject(new Error("Channel must be a voice channel")); - } - - return _this42.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - user_limit: channel.userLimit, - position: channel.position, - bitrate: kbitrate * 1000 // in bps - }).then(function (res) { - channel.bitrate = kbitrate; - channel._bitrate = kbitrate * 1000; - }); - }); + return this.updateChannel(channel, { bitrate: kbitrate }); }; //def updateChannel - InternalClient.prototype.updateChannel = function updateChannel(chann, data) { - return this.setChannelNameAndTopic(chann, data.name, data.topic); + InternalClient.prototype.updateChannel = function updateChannel(channel, data) { + var _this37 = this; + + return this.resolver.resolveChannel(channel).then(function (channel) { + if (!channel) { + return Promise.reject(new Error("Failed to resolve channel")); + } + + data = { + name: data.name || channel.name, + topic: data.topic || channel.topic, + position: data.position || channel.position, + user_limit: data.userLimit || channel.userLimit, + bitrate: data.bitrate || channel.bitrate + }; + + if (data.position < 0) { + return Promise.reject(new Error("Position cannot be less than 0")); + } + + if (data.user_limit < 0 || data.user_limit > 99) { + return Promise.reject(new Error("User limit must be between 0-99")); + } + + if (data.kbitrate < 8 || data.kbitrate > 96) { + return Promise.reject(new Error("Bitrate must be between 8-96kbps")); + } + + data.bitrate *= 1000; // convert to bits before sending + + return _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, data).then(function (res) { + channel.name = data.name; + channel.topic = data.topic; + channel.position = data.position; + channel.userLimit = data.user_limit; + channel.bitrate = Math.ceil(data.bitrate / 1000); + channel._bitrate = data.bitrate; + }); + }); }; //def addFriend @@ -1737,7 +1680,7 @@ var InternalClient = (function () { }; InternalClient.prototype.createWS = function createWS(url) { - var _this43 = this; + var _this38 = this; var self = this; var client = self.client; @@ -1771,14 +1714,14 @@ var InternalClient = (function () { this.websocket.onclose = function (code) { self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this43.client.options.autoReconnect); + self.disconnected(_this38.client.options.autoReconnect); }; this.websocket.onerror = function (e) { client.emit("error", e); self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this43.client.options.autoReconnect); + self.disconnected(_this38.client.options.autoReconnect); }; this.websocket.onmessage = function (e) { @@ -1807,11 +1750,11 @@ var InternalClient = (function () { self.user = self.users.add(new _StructuresUser2["default"](data.user, client)); - _this43.forceFetchCount = {}; - _this43.forceFetchQueue = []; - _this43.forceFetchLength = 1; - _this43.autoReconnectInterval = 1000; - _this43.sessionID = data.session_id; + _this38.forceFetchCount = {}; + _this38.forceFetchQueue = []; + _this38.forceFetchLength = 1; + _this38.autoReconnectInterval = 1000; + _this38.sessionID = data.session_id; data.guilds.forEach(function (server) { if (!server.unavailable) { @@ -2254,7 +2197,7 @@ var InternalClient = (function () { data.id = data.id || user.id; data.avatar = data.avatar || user.avatar; data.discriminator = data.discriminator || user.discriminator; - _this43.email = data.email || _this43.email; + _this38.email = data.email || _this38.email; var presenceUser = new _StructuresUser2["default"](data, client); @@ -2396,7 +2339,7 @@ var InternalClient = (function () { break; case _Constants.PacketType.FRIEND_ADD: - if (_this43.user.bot) { + if (_this38.user.bot) { return; } if (data.type === 1) { @@ -2427,7 +2370,7 @@ var InternalClient = (function () { } break; case _Constants.PacketType.FRIEND_REMOVE: - if (_this43.user.bot) { + if (_this38.user.bot) { return; } var user = self.friends.get("id", data.id); diff --git a/lib/Structures/ServerChannel.js b/lib/Structures/ServerChannel.js index 4385adaed..cf29f9334 100644 --- a/lib/Structures/ServerChannel.js +++ b/lib/Structures/ServerChannel.js @@ -139,6 +139,10 @@ var ServerChannel = (function (_Channel) { return this.client.setChannelPosition.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); }; + ServerChannel.prototype.update = function update() { + return this.client.updateChannel.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); + }; + return ServerChannel; })(_Channel3["default"]); diff --git a/lib/Structures/TextChannel.js b/lib/Structures/TextChannel.js index 876640cbc..6046de835 100644 --- a/lib/Structures/TextChannel.js +++ b/lib/Structures/TextChannel.js @@ -43,10 +43,6 @@ var TextChannel = (function (_ServerChannel) { return this.client.setChannelNameAndTopic.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); }; - TextChannel.prototype.update = function update() { - return this.client.updateChannel.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); - }; - TextChannel.prototype.sendMessage = function sendMessage() { return this.client.sendMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); }; diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index c51584cfc..710738e0d 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -1301,127 +1301,87 @@ export default class InternalClient { //def setChannelTopic setChannelTopic(chann, topic = "") { - return this.resolver.resolveChannel(chann) - .then(channel => { - if (channel.type !== "text") { - return Promise.reject(new Error("Channel must be a text channel")); - } + topic = topic || ""; - this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: channel.position, - topic: topic - }) - .then(res => channel.topic = res.topic) - }); + return this.updateChannel(channel, {topic: topic}); } //def setChannelName - setChannelName(chann, name = "discordjs_is_the_best") { - return this.resolver.resolveChannel(chann) - .then(channel => { - this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: name, - position: channel.position, - topic: channel.topic, - user_limit: channel.userLimit, - bitrate: channel.bitrate - }) - .then(res => channel.name = res.name) - }); + setChannelName(channel, name) { + name = name || "unnamed-channel"; + + return this.updateChannel(channel, {name: name}); } //def setChannelNameAndTopic - setChannelNameAndTopic(chann, name = "discordjs_is_the_best", topic = "") { - return this.resolver.resolveChannel(chann) - .then(channel => { - if (channel.type !== "text") { - return Promise.reject(new Error("Channel must be a text channel")); - } + setChannelNameAndTopic(channel) { + name = name || "unnamed-channel"; + topic = topic || ""; - this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: name, - position: channel.position, - topic: topic - }) - .then(res => { - channel.name = res.name; - channel.topic = res.topic; - }) - }); + return this.updateChannel(channel, {name: name, topic: topic}); } //def setChannelPosition - setChannelPosition(chann, position = 0) { - return this.resolver.resolveChannel(chann) - .then(channel => { - this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: position, - topic: channel.topic, - user_limit: channel.userLimit, - bitrate: channel._bitrate - }) - .then(res => channel.position = res.position) - }); + setChannelPosition(channel, position) { + position = position || 0; + + return this.updateChannel(channel, {position: position}); } //def setChannelUserLimit setChannelUserLimit(channel, limit) { limit = limit || 0; // default 0 = no limit - if (limit < 0) { - return Promise.reject(new Error("User limit cannot be less than 0")); - } - - if (limit > 99) { - return Promise.reject(new Error("User limit cannot be greater than 99")); - } - - return this.resolver.resolveChannel(channel).then((channel) => { - if (channel.type !== "voice") { - return Promise.reject(new Error("Channel must be a voice channel")); - } - - return this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - position: channel.position, - user_limit: limit, - bitrate: channel._bitrate - }) - .then(res => channel.userLimit = limit); - }); + return this.updateChannel(channel, {userLimit: limit}) } //def setChannelBitrate setChannelBitrate(channel, kbitrate) { kbitrate = kbitrate || 64; // default 64kbps - if (kbitrate < 8 || kbitrate > 96) { - return Promise.reject(new Error("Bitrate must be between 8-96kbps")); - } - - return this.resolver.resolveChannel(channel).then(channel => { - if (channel.type !== "voice") { - return Promise.reject(new Error("Channel must be a voice channel")); - } - - return this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { - name: channel.name, - user_limit: channel.userLimit, - position: channel.position, - bitrate: kbitrate * 1000 // in bps - }) - .then(res => { - channel.bitrate = kbitrate; - channel._bitrate = kbitrate * 1000; - }); - }); + return this.updateChannel(channel, {bitrate: kbitrate}); } //def updateChannel - updateChannel(chann, data) { - return this.setChannelNameAndTopic(chann, data.name, data.topic); + updateChannel(channel, data) { + return this.resolver.resolveChannel(channel).then(channel => { + if (!channel) { + return Promise.reject(new Error("Failed to resolve channel")); + } + + data = { + name: data.name || channel.name, + topic: data.topic || channel.topic, + position: data.position || channel.position, + user_limit: data.userLimit || channel.userLimit, + bitrate: data.bitrate || channel.bitrate + } + + if (data.position < 0) { + return Promise.reject(new Error("Position cannot be less than 0")); + } + + if (data.user_limit < 0 || data.user_limit > 99) { + return Promise.reject(new Error("User limit must be between 0-99")); + } + + if (data.kbitrate < 8 || data.kbitrate > 96) { + return Promise.reject(new Error("Bitrate must be between 8-96kbps")); + } + + + data.bitrate *= 1000; // convert to bits before sending + + return this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, data) + .then(res => { + channel.name = data.name; + channel.topic = data.topic; + channel.position = data.position; + channel.userLimit = data.user_limit; + channel.bitrate = Math.ceil(data.bitrate / 1000); + channel._bitrate = data.bitrate; + }); + }); } //def addFriend diff --git a/src/Structures/ServerChannel.js b/src/Structures/ServerChannel.js index 79e4fdf9c..70aeb0d1f 100644 --- a/src/Structures/ServerChannel.js +++ b/src/Structures/ServerChannel.js @@ -82,4 +82,8 @@ export default class ServerChannel extends Channel{ setPosition(){ return this.client.setChannelPosition.apply(this.client, reg(this, arguments)); } + + update(){ + return this.client.updateChannel.apply(this.client, reg(this, arguments)); + } } diff --git a/src/Structures/TextChannel.js b/src/Structures/TextChannel.js index e9036bc08..a5a60f7eb 100644 --- a/src/Structures/TextChannel.js +++ b/src/Structures/TextChannel.js @@ -26,15 +26,11 @@ export default class TextChannel extends ServerChannel{ return this.client.setChannelNameAndTopic.apply(this.client, reg(this, arguments)); } - update(){ - return this.client.updateChannel.apply(this.client, reg(this, arguments)); - } - sendMessage(){ return this.client.sendMessage.apply(this.client, reg(this, arguments)); } - send() { + send(){ return this.client.sendMessage.apply(this.client, reg(this, arguments)); } @@ -42,23 +38,23 @@ export default class TextChannel extends ServerChannel{ return this.client.sendTTSMessage.apply(this.client, reg(this, arguments)); } - sendTTS() { + sendTTS(){ return this.client.sendTTSMessage.apply(this.client, reg(this, arguments)); } - sendFile() { + sendFile(){ return this.client.sendFile.apply(this.client, reg(this, arguments)); } - getLogs() { + getLogs(){ return this.client.getChannelLogs.apply(this.client, reg(this, arguments)); } - startTyping() { + startTyping(){ return this.client.startTyping.apply(this.client, reg(this, arguments)); } - stopTyping() { + stopTyping(){ return this.client.stopTyping.apply(this.client, reg(this, arguments)); } } From 16eb38a99ea3ba5e40ef8163c373c0d3450119f0 Mon Sep 17 00:00:00 2001 From: Programmix Date: Wed, 25 May 2016 07:47:18 -0700 Subject: [PATCH 2/2] Quirky logic fix --- src/Client/InternalClient.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 710738e0d..552bc333b 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -1352,9 +1352,9 @@ export default class InternalClient { data = { name: data.name || channel.name, topic: data.topic || channel.topic, - position: data.position || channel.position, - user_limit: data.userLimit || channel.userLimit, - bitrate: data.bitrate || channel.bitrate + position: (data.position ? data.position : channel.position), + user_limit: (data.userLimit ? data.userLimit : channel.userLimit), + bitrate: (data.bitrate ? data.bitrate : channel.bitrate) } if (data.position < 0) {