mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
Add VoiceConnection.disconnect([reason]);
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -40,6 +40,9 @@ class ClientVoiceManager {
|
||||
const { channel, token, sessionID, endpoint, resolve, reject } = pendingRequest;
|
||||
const voiceConnection = new VoiceConnection(this, channel, token, sessionID, endpoint, resolve, reject);
|
||||
this.connections.set(guildID, voiceConnection);
|
||||
voiceConnection.on('disconnected', () => {
|
||||
this.connections.delete(guildID);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const VoiceConnectionWebSocket = require('./VoiceConnectionWebSocket');
|
||||
const VoiceConnectionUDPClient = require('./VoiceConnectionUDPClient');
|
||||
const Constants = require('../../util/Constants');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/**
|
||||
@@ -60,6 +61,38 @@ class VoiceConnection extends EventEmitter {
|
||||
_onError(e) {
|
||||
this._reject(e);
|
||||
this.emit('error', e);
|
||||
this._shutdown(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the Client from the Voice Channel
|
||||
* @param {string} [reason='user requested'] the reason of the disconnection
|
||||
* @returns {null}
|
||||
*/
|
||||
disconnect(reason = 'user requested') {
|
||||
this.manager.client.ws.send({
|
||||
op: Constants.OPCodes.VOICE_STATE_UPDATE,
|
||||
d: {
|
||||
guild_id: this.channel.guild.id,
|
||||
channel_id: null,
|
||||
self_mute: false,
|
||||
self_deaf: false,
|
||||
},
|
||||
});
|
||||
return this._shutdown(reason);
|
||||
}
|
||||
|
||||
_onClose(e) {
|
||||
return this._shutdown(e);
|
||||
}
|
||||
|
||||
_shutdown(e) {
|
||||
this.ready = false;
|
||||
this.websocket._shutdown();
|
||||
if (this.udp) {
|
||||
this.udp._shutdown();
|
||||
}
|
||||
this.emit('disconnected', e);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,9 +102,11 @@ class VoiceConnection extends EventEmitter {
|
||||
*/
|
||||
bindListeners() {
|
||||
this.websocket.on('error', err => this._onError(err));
|
||||
this.websocket.on('close', err => this._onClose(err));
|
||||
this.websocket.on('ready-for-udp', data => {
|
||||
this.udp = new VoiceConnectionUDPClient(this, data);
|
||||
this.udp.on('error', err => this._onError(err));
|
||||
this.udp.on('close', err => this._onClose(err));
|
||||
});
|
||||
this.websocket.on('ready', () => {
|
||||
this.ready = true;
|
||||
|
||||
@@ -32,6 +32,19 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
_shutdown() {
|
||||
if (this.udpSocket) {
|
||||
try {
|
||||
this.udpSocket.close();
|
||||
} catch (err) {
|
||||
if (err.message !== 'Not running') {
|
||||
this.emit('error', err);
|
||||
}
|
||||
}
|
||||
this.udpSocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
connectUDP(address) {
|
||||
this.udpIP = address;
|
||||
this.udpSocket = udp.createSocket('udp4');
|
||||
@@ -64,6 +77,10 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
||||
this.emit('error', { error, message });
|
||||
});
|
||||
|
||||
this.udpSocket.on('close', error => {
|
||||
this.emit('close', error);
|
||||
});
|
||||
|
||||
const blankMessage = new Buffer(70);
|
||||
blankMessage.writeUIntBE(this.data.ssrc, 0, 4);
|
||||
this.send(blankMessage);
|
||||
|
||||
@@ -12,6 +12,8 @@ class VoiceConnectionWebSocket extends EventEmitter {
|
||||
this.ws = new WebSocket(`wss://${endpoint}`, null, { rejectUnauthorized: false });
|
||||
this.ws.onopen = () => this._onOpen();
|
||||
this.ws.onmessage = e => this._onMessage(e);
|
||||
this.ws.onclose = e => this._onClose(e);
|
||||
this.heartbeat = null;
|
||||
}
|
||||
|
||||
send(data) {
|
||||
@@ -20,6 +22,14 @@ class VoiceConnectionWebSocket extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
_shutdown() {
|
||||
if (this.ws) {
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
}
|
||||
clearInterval(this.heartbeat);
|
||||
}
|
||||
|
||||
_onOpen() {
|
||||
this.send({
|
||||
op: Constants.OPCodes.DISPATCH,
|
||||
@@ -32,8 +42,20 @@ class VoiceConnectionWebSocket extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
_onClose(e) {
|
||||
this.emit('close', e);
|
||||
}
|
||||
|
||||
_onError(e) {
|
||||
throw e;
|
||||
this.emit('error', e);
|
||||
}
|
||||
|
||||
_setHeartbeat(interval) {
|
||||
this.heartbeat = setInterval(() => {
|
||||
this.send({
|
||||
op: Constants.VoiceOPCodes.HEARTBEAT,
|
||||
});
|
||||
}, interval);
|
||||
}
|
||||
|
||||
_onMessage(event) {
|
||||
@@ -46,6 +68,7 @@ class VoiceConnectionWebSocket extends EventEmitter {
|
||||
|
||||
switch (packet.op) {
|
||||
case Constants.VoiceOPCodes.READY:
|
||||
this._setHeartbeat(packet.d.heartbeat_interval);
|
||||
this.emit('ready-for-udp', packet.d);
|
||||
break;
|
||||
case Constants.VoiceOPCodes.SESSION_DESCRIPTION:
|
||||
|
||||
@@ -127,7 +127,6 @@ class WebSocketManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
eventClose(event) {
|
||||
console.log('close', event.code);
|
||||
if (event.code === 4004) {
|
||||
throw Constants.Errors.BAD_LOGIN;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user