From bea16630527c3f631e03e557a6326c8383ef9294 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 23 May 2016 10:53:52 +1000 Subject: [PATCH] Set bitrate for voice channels support (#363) * Set bitrate for voice channels * Docs for bitrate settings and values --- docs/docs_client.rst | 10 ++++++++ docs/docs_voicechannel.rst | 5 ++++ lib/Client/Client.js | 8 ++++++ lib/Client/InternalClient.js | 45 +++++++++++++++++++++++++--------- lib/Structures/VoiceChannel.js | 6 +++++ src/Client/Client.js | 6 +++++ src/Client/InternalClient.js | 21 ++++++++++++++++ src/Structures/VoiceChannel.js | 6 +++++ 8 files changed, 96 insertions(+), 11 deletions(-) diff --git a/docs/docs_client.rst b/docs/docs_client.rst index 9dc65f531..6cd61c2f9 100644 --- a/docs/docs_client.rst +++ b/docs/docs_client.rst @@ -543,6 +543,16 @@ Sets the user limit of a voice channel - **callback** - `function` taking the following: - **error** - error if any occurred +setChannelBitrate(channel, bitrate, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the bitrate of a voice channel + +- **channel** - A `Channel Resolvable`_ +- **bitrate** - A `Number`, bitrate (in kb/s) (8 - 96) +- **callback** - `function` taking the following: + - **error** - error if any occurred + startTyping(channel, `callback`) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/docs_voicechannel.rst b/docs/docs_voicechannel.rst index 0c4c389ae..1d3e32ea6 100644 --- a/docs/docs_voicechannel.rst +++ b/docs/docs_voicechannel.rst @@ -23,6 +23,11 @@ userLimit The maximum amount of users that can connect to the voice channel. If it's 0, there is no limit +bitrate +~~~~~~~~ + +The bitrate of the voice channel (in kb/s). + Functions --------- diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 803703b40..53491966f 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -977,6 +977,14 @@ var Client = (function (_EventEmitter) { return this.internal.setChannelUserLimit(channel, limit).then(dataCallback(callback), errorCallback(callback)); }; + // def setChannelBitrate + + Client.prototype.setChannelBitrate = function setChannelBitrate(channel, kbitrate) { + var callback = arguments.length <= 2 || arguments[2] === undefined ? function () /*err, {}*/{} : arguments[2]; + + return this.internal.setChannelBitrate(channel, kbitrate).then(dataCallback(callback), errorCallback(callback)); + }; + // def updateChannel Client.prototype.updateChannel = function updateChannel(channel, data) { diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index a10bad2a7..b0d94ad24 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -1638,6 +1638,29 @@ var InternalClient = (function () { }); }; + //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 () { + return channel.bitrate = kbitrate; + }); + }); + }; + //def updateChannel InternalClient.prototype.updateChannel = function updateChannel(chann, data) { @@ -1690,7 +1713,7 @@ var InternalClient = (function () { }; InternalClient.prototype.createWS = function createWS(url) { - var _this42 = this; + var _this43 = this; var self = this; var client = self.client; @@ -1724,14 +1747,14 @@ var InternalClient = (function () { this.websocket.onclose = function (code) { self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this42.client.options.autoReconnect); + self.disconnected(_this43.client.options.autoReconnect); }; this.websocket.onerror = function (e) { client.emit("error", e); self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this42.client.options.autoReconnect); + self.disconnected(_this43.client.options.autoReconnect); }; this.websocket.onmessage = function (e) { @@ -1760,11 +1783,11 @@ var InternalClient = (function () { self.user = self.users.add(new _StructuresUser2["default"](data.user, client)); - _this42.forceFetchCount = {}; - _this42.forceFetchQueue = []; - _this42.forceFetchLength = 1; - _this42.autoReconnectInterval = 1000; - _this42.sessionID = data.session_id; + _this43.forceFetchCount = {}; + _this43.forceFetchQueue = []; + _this43.forceFetchLength = 1; + _this43.autoReconnectInterval = 1000; + _this43.sessionID = data.session_id; data.guilds.forEach(function (server) { if (!server.unavailable) { @@ -2207,7 +2230,7 @@ var InternalClient = (function () { data.id = data.id || user.id; data.avatar = data.avatar || user.avatar; data.discriminator = data.discriminator || user.discriminator; - _this42.email = data.email || _this42.email; + _this43.email = data.email || _this43.email; var presenceUser = new _StructuresUser2["default"](data, client); @@ -2349,7 +2372,7 @@ var InternalClient = (function () { break; case _Constants.PacketType.FRIEND_ADD: - if (_this42.user.bot) { + if (_this43.user.bot) { return; } if (data.type === 1) { @@ -2380,7 +2403,7 @@ var InternalClient = (function () { } break; case _Constants.PacketType.FRIEND_REMOVE: - if (_this42.user.bot) { + if (_this43.user.bot) { return; } var user = self.friends.get("id", data.id); diff --git a/lib/Structures/VoiceChannel.js b/lib/Structures/VoiceChannel.js index 23bed8632..ab9b11afd 100644 --- a/lib/Structures/VoiceChannel.js +++ b/lib/Structures/VoiceChannel.js @@ -27,6 +27,8 @@ var VoiceChannel = (function (_ServerChannel) { _ServerChannel.call(this, data, client, server); this.members = data.members || new _UtilCache2["default"](); this.userLimit = data.user_limit || 0; + this._bitrate = data.bitrate || 64000; // incase somebody wants to access the bps value??? + this.bitrate = Math.round(this._bitrate / 1000); // store as kbps } VoiceChannel.prototype.join = function join() { @@ -39,6 +41,10 @@ var VoiceChannel = (function (_ServerChannel) { return this.client.setChannelUserLimit.apply(this.client, [this, arguments]); }; + VoiceChannel.prototype.setBitrate = function setBitrate() { + return this.client.setChannelBitrate.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments)); + }; + return VoiceChannel; })(_ServerChannel3["default"]); diff --git a/src/Client/Client.js b/src/Client/Client.js index 7a763e335..0f19f718f 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -989,6 +989,12 @@ export default class Client extends EventEmitter { .then(dataCallback(callback), errorCallback(callback)); } + // def setChannelBitrate + setChannelBitrate(channel, kbitrate, callback = (/*err, {}*/) => { }) { + return this.internal.setChannelBitrate(channel, kbitrate) + .then(dataCallback(callback), errorCallback(callback)); + } + // def updateChannel updateChannel(channel, data, callback = (/*err, {}*/) => { }) { return this.internal.updateChannel(channel, data) diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 752f738f1..417bfe8f7 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -1374,6 +1374,27 @@ export default class InternalClient { }); } + //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(() => channel.bitrate = kbitrate); + }); + } + //def updateChannel updateChannel(chann, data) { return this.setChannelNameAndTopic(chann, data.name, data.topic); diff --git a/src/Structures/VoiceChannel.js b/src/Structures/VoiceChannel.js index 2416f164e..23477a34c 100644 --- a/src/Structures/VoiceChannel.js +++ b/src/Structures/VoiceChannel.js @@ -9,6 +9,8 @@ export default class VoiceChannel extends ServerChannel{ super(data, client, server); this.members = data.members || new Cache(); this.userLimit = data.user_limit || 0; + this._bitrate = data.bitrate || 64000; // incase somebody wants to access the bps value??? + this.bitrate = Math.round(this._bitrate / 1000); // store as kbps } join(callback = function () { }) { @@ -18,4 +20,8 @@ export default class VoiceChannel extends ServerChannel{ setUserLimit() { return this.client.setChannelUserLimit.apply(this.client, [this, arguments]); } + + setBitrate() { + return this.client.setChannelBitrate.apply(this.client, reg(this, arguments)); + } }