From 7ae7a9aa340ec672337b6ba49732eb094447f216 Mon Sep 17 00:00:00 2001 From: Jesse Bryan Date: Mon, 11 Jul 2016 14:12:27 -0500 Subject: [PATCH] Added a voiceSpeaking event, fired when a user in a voiceChannel starts or stops speaking. (#452) * Added an event for the voice speaking packet. * Updated the docs to reflect the voiceSpeaking event addition. * Fixed some spacing issues in the VoiceConnection.js file. * Moved the speaking boolean to the User object. --- docs/docs_client.rst | 13 +++++++++---- docs/docs_user.rst | 6 +++++- lib/Structures/User.js | 1 + lib/Voice/VoiceConnection.js | 18 ++++++++++++++++++ src/Structures/User.js | 3 ++- src/Voice/VoiceConnection.js | 36 +++++++++++++++++++++++++++--------- 6 files changed, 62 insertions(+), 15 deletions(-) mode change 100644 => 100755 docs/docs_client.rst mode change 100644 => 100755 docs/docs_user.rst mode change 100644 => 100755 src/Structures/User.js mode change 100644 => 100755 src/Voice/VoiceConnection.js diff --git a/docs/docs_client.rst b/docs/docs_client.rst old mode 100644 new mode 100755 index 733cc7451..9823ebb93 --- a/docs/docs_client.rst +++ b/docs/docs_client.rst @@ -1015,19 +1015,24 @@ Emitted when a note is updated. Supplies a User_ object (containing the updated voiceJoin ~~~~~~~~ -Emitted when a user joins a voice channel, supplies a VoiceChannel_ and a User_ +Emitted when a user joins a voice channel, supplies a VoiceChannel_ and a User_. voiceSwitch ~~~~~~~~~~~ -Emitted when a user switches voice channels, supplies the old VoiceChannel_, the new VoiceChannel_, and a User_ +Emitted when a user switches voice channels, supplies the old VoiceChannel_, the new VoiceChannel_, and a User_. voiceLeave ~~~~~~~~~~ -Emitted when a user leaves a voice channel, supplies a VoiceChannel_ and a User_ +Emitted when a user leaves a voice channel, supplies a VoiceChannel_ and a User_. voiceStateUpdate ~~~~~~~~~~ -Emitted when a user mutes/deafens, supplies a VoiceChannel_, User_, an object containing the old mute/selfMute/deaf/selfDeaf properties, and an object containing the new mute/selfMute/deaf/selfDeaf properties +Emitted when a user mutes/deafens, supplies a VoiceChannel_, User_, an object containing the old mute/selfMute/deaf/selfDeaf properties, and an object containing the new mute/selfMute/deaf/selfDeaf properties. + +voiceSpeaking +~~~~~~~~~~~ + +Emitted when a user starts or stops speaking, supplies a VoiceChannel_, and User_. The `speaking` property under the supplied User_ object can be used to determine whether the user started or stopped speaking. \ No newline at end of file diff --git a/docs/docs_user.rst b/docs/docs_user.rst old mode 100644 new mode 100755 index 0fc7fdc59..71082350e --- a/docs/docs_user.rst +++ b/docs/docs_user.rst @@ -87,12 +87,16 @@ createdAt A `Date` referring to when the user was created. - note ~~~~ The note of the user, `String`. +speaking +~~~~~~~~ + +A boolean that represents whether or not the user is speaking in a voice channel, default is `false`. + Functions --------- diff --git a/lib/Structures/User.js b/lib/Structures/User.js index 67f1cc3f9..f1179972a 100644 --- a/lib/Structures/User.js +++ b/lib/Structures/User.js @@ -40,6 +40,7 @@ var User = (function (_Equality) { this.note = data.note || null; this.voiceChannel = null; this.voiceState = {}; + this.speaking = false; } User.prototype.mention = function mention() { diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index 06e747c14..bf7d78464 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -447,6 +447,24 @@ var VoiceConnection = (function (_EventEmitter) { self.mode = data.d.mode; self.emit("ready", self); + break; + case 5: + var user = self.server.members.get("id", data.d.user_id); + + if (user) { + var speaking = data.d.speaking; + var channel = user.voiceChannel; + + if (channel) { + user.speaking = speaking; + self.client.emit("voiceSpeaking", channel, user); + } else { + self.client.emit("warn", "channel doesn't exist even though SPEAKING expects them to"); + } + } else { + self.client.emit("warn", "user doesn't exist even though SPEAKING expects them to"); + } + break; } }); diff --git a/src/Structures/User.js b/src/Structures/User.js old mode 100644 new mode 100755 index cbdedd6c6..e09df9cf8 --- a/src/Structures/User.js +++ b/src/Structures/User.js @@ -4,7 +4,7 @@ import Equality from "../Util/Equality"; import {Endpoints} from "../Constants"; import {reg} from "../Util/ArgumentRegulariser"; -export default class User extends Equality{ +export default class User extends Equality { constructor(data, client){ super(); this.client = client; @@ -22,6 +22,7 @@ export default class User extends Equality{ this.note = data.note || null; this.voiceChannel = null; this.voiceState = {}; + this.speaking = false; } get createdAt() { diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js old mode 100644 new mode 100755 index 724048550..ee9981ab8 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -231,9 +231,9 @@ export default class VoiceConnection extends EventEmitter { // options is the callback callback = options; } - if (typeof options !== "object") { - options = {}; - } + if (typeof options !== "object") { + options = {}; + } options.volume = options.volume !== undefined ? options.volume : this.getVolume(); return new Promise((resolve, reject) => { this.encoder @@ -260,9 +260,9 @@ export default class VoiceConnection extends EventEmitter { // options is the callback callback = options; } - if (typeof options !== "object") { - options = {}; - } + if (typeof options !== "object") { + options = {}; + } options.volume = options.volume !== undefined ? options.volume : this.getVolume(); return new Promise((resolve, reject) => { this.encoder @@ -290,9 +290,9 @@ export default class VoiceConnection extends EventEmitter { // volume is the callback callback = volume; } - if (!ffmpegOptions instanceof Array) { - ffmpegOptions = []; - } + if (!ffmpegOptions instanceof Array) { + ffmpegOptions = []; + } var volume = volume !== undefined ? volume : this.getVolume(); return new Promise((resolve, reject) => { this.encoder @@ -402,6 +402,24 @@ export default class VoiceConnection extends EventEmitter { self.mode = data.d.mode; self.emit("ready", self); + break; + case 5: + var user = self.server.members.get("id", data.d.user_id); + + if (user){ + var speaking = data.d.speaking; + var channel = user.voiceChannel; + + if (channel){ + user.speaking = speaking; + self.client.emit("voiceSpeaking", channel, user); + } else { + self.client.emit("warn", "channel doesn't exist even though SPEAKING expects them to"); + } + } else { + self.client.emit("warn", "user doesn't exist even though SPEAKING expects them to"); + } + break; } });