Minor fixes for d/c handler and non-bot accounts

This commit is contained in:
Nicholas Tay
2016-04-05 12:22:50 +10:00
parent 78399a77ae
commit 5619e97a39
2 changed files with 198 additions and 180 deletions

View File

@@ -217,11 +217,15 @@ var InternalClient = (function () {
}; };
InternalClient.prototype.disconnected = function disconnected() { InternalClient.prototype.disconnected = function disconnected() {
var _this2 = this;
var forced = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0]; var forced = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
this.cleanIntervals(); this.cleanIntervals();
this.leaveVoiceChannel(); this.voiceConnections.forEach(function (vc) {
_this2.leaveVoiceChannel(vc);
});
if (this.client.options.revive && !forced) { if (this.client.options.revive && !forced) {
this.setup(); this.setup();
@@ -240,13 +244,13 @@ var InternalClient = (function () {
//def leaveVoiceChannel //def leaveVoiceChannel
InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel(chann) { InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel(chann) {
var _this2 = this; var _this3 = this;
if (this.user.bot) { if (this.user.bot) {
var leave = function leave(connection) { var leave = function leave(connection) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
connection.destroy(); connection.destroy();
_this2.voiceConnections.remove(connection); _this3.voiceConnections.remove(connection);
resolve(); resolve();
}); });
}; };
@@ -261,7 +265,7 @@ var InternalClient = (function () {
return Promise.reject(new Error("channel is not a voice channel!")); return Promise.reject(new Error("channel is not a voice channel!"));
} }
var connection = _this2.voiceConnections.get("voiceChannel", channel); var connection = _this3.voiceConnections.get("voiceChannel", channel);
if (!connection) { if (!connection) {
return Promise.reject(new Error("not connected to that voice channel")); return Promise.reject(new Error("not connected to that voice channel"));
} }
@@ -276,7 +280,7 @@ var InternalClient = (function () {
// preserve old functionality for non-bots // preserve old functionality for non-bots
if (this.voiceConnections[0]) { if (this.voiceConnections[0]) {
this.voiceConnections[0].destroy(); this.voiceConnections[0].destroy();
this.voiceConnections = []; this.voiceConnections.remove(this.voiceConnections[0]);
} }
return Promise.resolve(); return Promise.resolve();
} }
@@ -285,11 +289,11 @@ var InternalClient = (function () {
//def awaitResponse //def awaitResponse
InternalClient.prototype.awaitResponse = function awaitResponse(msg) { InternalClient.prototype.awaitResponse = function awaitResponse(msg) {
var _this3 = this; var _this4 = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
msg = _this3.resolver.resolveMessage(msg); msg = _this4.resolver.resolveMessage(msg);
if (!msg) { if (!msg) {
reject(new Error("message undefined")); reject(new Error("message undefined"));
@@ -298,18 +302,18 @@ var InternalClient = (function () {
var awaitID = msg.channel.id + msg.author.id; var awaitID = msg.channel.id + msg.author.id;
if (!_this3.messageAwaits[awaitID]) { if (!_this4.messageAwaits[awaitID]) {
_this3.messageAwaits[awaitID] = []; _this4.messageAwaits[awaitID] = [];
} }
_this3.messageAwaits[awaitID].push(resolve); _this4.messageAwaits[awaitID].push(resolve);
}); });
}; };
//def joinVoiceChannel //def joinVoiceChannel
InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) { InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) {
var _this4 = this; var _this5 = this;
return this.resolver.resolveChannel(chann).then(function (channel) { return this.resolver.resolveChannel(chann).then(function (channel) {
if (!channel) { if (!channel) {
@@ -321,7 +325,7 @@ var InternalClient = (function () {
} }
var joinSendWS = function joinSendWS() { var joinSendWS = function joinSendWS() {
_this4.sendWS({ _this5.sendWS({
op: 4, op: 4,
d: { d: {
"guild_id": channel.server.id, "guild_id": channel.server.id,
@@ -332,52 +336,59 @@ var InternalClient = (function () {
}); });
}; };
var joinVoice = new Promise(function (resolve, reject) { var joinVoice = function joinVoice() {
var session, return new Promise(function (resolve, reject) {
token, var session,
server = channel.server, token,
endpoint; server = channel.server,
endpoint;
var check = function check(m) { var check = function check(m) {
var data = JSON.parse(m); var data = JSON.parse(m);
if (data.d.guild_id !== server.id) return; // ensure it is the right server if (data.d.guild_id !== server.id) return; // ensure it is the right server
if (data.t === "VOICE_STATE_UPDATE") { if (data.t === "VOICE_STATE_UPDATE") {
session = data.d.session_id; session = data.d.session_id;
} else if (data.t === "VOICE_SERVER_UPDATE") { } else if (data.t === "VOICE_SERVER_UPDATE") {
token = data.d.token; token = data.d.token;
endpoint = data.d.endpoint; endpoint = data.d.endpoint;
var chan = new _VoiceVoiceConnection2["default"](channel, _this4.client, session, token, server, endpoint); var chan = new _VoiceVoiceConnection2["default"](channel, _this5.client, session, token, server, endpoint);
_this4.voiceConnections.add(chan); _this5.voiceConnections.add(chan);
chan.on("ready", function () { chan.on("ready", function () {
return resolve(chan); return resolve(chan);
}); });
chan.on("error", reject); chan.on("error", reject);
_this4.client.emit("debug", "removed temporary voice websocket listeners"); _this5.client.emit("debug", "removed temporary voice websocket listeners");
_this4.websocket.removeListener("message", check); _this5.websocket.removeListener("message", check);
} }
}; };
_this4.websocket.on("message", check); _this5.websocket.on("message", check);
joinSendWS(); joinSendWS();
}); });
};
if (!_this4.user.bot && _this4.voiceConnections.length > 0) // nonbot, one voiceconn only, just like last time just disconnect var existingServerConn = _this5.voiceConnections.get("server", channel.server); // same server connection
return _this4.leaveVoiceChannel(_this4.voiceConnections[0]).then(joinVoice); if (existingServerConn) {
joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler
return Promise.resolve(existingServerConn);
}
var existingServerConn = _this4.voiceConnections.get("server", channel.server); // same server connection if (!_this5.user.bot && _this5.voiceConnections.length > 0) {
if (existingServerConn) joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler // nonbot, one voiceconn only, just like last time just disconnect
return _this5.leaveVoiceChannel().then(joinVoice);
}
return joinVoice; return joinVoice();
}); });
}; };
// def forceFetchUsers // def forceFetchUsers
InternalClient.prototype.forceFetchUsers = function forceFetchUsers() { InternalClient.prototype.forceFetchUsers = function forceFetchUsers() {
var _this5 = this; var _this6 = this;
this.sendWS({ this.sendWS({
op: 8, op: 8,
@@ -414,7 +425,7 @@ var InternalClient = (function () {
if (!server) { if (!server) {
resolve(); resolve();
} else { } else {
_this5.chunkloaderCallback = resolve; _this6.chunkloaderCallback = resolve;
} }
}); });
}; };
@@ -422,7 +433,7 @@ var InternalClient = (function () {
// def createServer // def createServer
InternalClient.prototype.createServer = function createServer(name) { InternalClient.prototype.createServer = function createServer(name) {
var _this6 = this; var _this7 = this;
var region = arguments.length <= 1 || arguments[1] === undefined ? "london" : arguments[1]; var region = arguments.length <= 1 || arguments[1] === undefined ? "london" : arguments[1];
@@ -431,7 +442,7 @@ var InternalClient = (function () {
return this.apiRequest('post', _Constants.Endpoints.SERVERS, true, { name: name, region: region }).then(function (res) { return this.apiRequest('post', _Constants.Endpoints.SERVERS, true, { name: name, region: region }).then(function (res) {
// valid server, wait until it is cached // valid server, wait until it is cached
return waitFor(function () { return waitFor(function () {
return _this6.servers.get("id", res.id); return _this7.servers.get("id", res.id);
}); });
}); });
}; };
@@ -439,7 +450,7 @@ var InternalClient = (function () {
//def joinServer //def joinServer
InternalClient.prototype.joinServer = function joinServer(invite) { InternalClient.prototype.joinServer = function joinServer(invite) {
var _this7 = this; var _this8 = this;
invite = this.resolver.resolveInviteID(invite); invite = this.resolver.resolveInviteID(invite);
if (!invite) { if (!invite) {
@@ -449,7 +460,7 @@ var InternalClient = (function () {
return this.apiRequest("post", _Constants.Endpoints.INVITE(invite), true).then(function (res) { return this.apiRequest("post", _Constants.Endpoints.INVITE(invite), true).then(function (res) {
// valid server, wait until it is received via ws and cached // valid server, wait until it is received via ws and cached
return waitFor(function () { return waitFor(function () {
return _this7.servers.get("id", res.guild.id); return _this8.servers.get("id", res.guild.id);
}); });
}); });
}; };
@@ -457,7 +468,7 @@ var InternalClient = (function () {
//def updateServer //def updateServer
InternalClient.prototype.updateServer = function updateServer(server, name, region) { InternalClient.prototype.updateServer = function updateServer(server, name, region) {
var _this8 = this; var _this9 = this;
var server = this.resolver.resolveServer(server); var server = this.resolver.resolveServer(server);
if (!server) { if (!server) {
@@ -467,7 +478,7 @@ var InternalClient = (function () {
return this.apiRequest("patch", _Constants.Endpoints.SERVER(server.id), true, { name: name || server.name, region: region || server.region }).then(function (res) { return this.apiRequest("patch", _Constants.Endpoints.SERVER(server.id), true, { name: name || server.name, region: region || server.region }).then(function (res) {
// wait until the name and region are updated // wait until the name and region are updated
return waitFor(function () { return waitFor(function () {
return _this8.servers.get("name", res.name) ? _this8.servers.get("name", res.name).region === res.region ? _this8.servers.get("id", res.id) : false : false; return _this9.servers.get("name", res.name) ? _this9.servers.get("name", res.name).region === res.region ? _this9.servers.get("id", res.id) : false : false;
}); });
}); });
}; };
@@ -475,7 +486,7 @@ var InternalClient = (function () {
//def leaveServer //def leaveServer
InternalClient.prototype.leaveServer = function leaveServer(srv) { InternalClient.prototype.leaveServer = function leaveServer(srv) {
var _this9 = this; var _this10 = this;
var server = this.resolver.resolveServer(srv); var server = this.resolver.resolveServer(srv);
if (!server) { if (!server) {
@@ -498,17 +509,17 @@ var InternalClient = (function () {
var chan = _ref3; var chan = _ref3;
_this9.channels.remove(chan); _this10.channels.remove(chan);
} }
// remove server // remove server
_this9.servers.remove(server); _this10.servers.remove(server);
}); });
}; };
//def deleteServer //def deleteServer
InternalClient.prototype.deleteServer = function deleteServer(srv) { InternalClient.prototype.deleteServer = function deleteServer(srv) {
var _this10 = this; var _this11 = this;
var server = this.resolver.resolveServer(srv); var server = this.resolver.resolveServer(srv);
if (!server) { if (!server) {
@@ -531,10 +542,10 @@ var InternalClient = (function () {
var chan = _ref4; var chan = _ref4;
_this10.channels.remove(chan); _this11.channels.remove(chan);
} }
// remove server // remove server
_this10.servers.remove(server); _this11.servers.remove(server);
}); });
}; };
@@ -542,7 +553,7 @@ var InternalClient = (function () {
// email and password are optional // email and password are optional
InternalClient.prototype.loginWithToken = function loginWithToken(token, email, password) { InternalClient.prototype.loginWithToken = function loginWithToken(token, email, password) {
var _this11 = this; var _this12 = this;
this.state = _ConnectionState2["default"].LOGGED_IN; this.state = _ConnectionState2["default"].LOGGED_IN;
this.token = token; this.token = token;
@@ -550,7 +561,7 @@ var InternalClient = (function () {
this.password = password; this.password = password;
return this.getGateway().then(function (url) { return this.getGateway().then(function (url) {
_this11.createWS(url); _this12.createWS(url);
return token; return token;
}); });
}; };
@@ -558,14 +569,14 @@ var InternalClient = (function () {
// def login // def login
InternalClient.prototype.login = function login(email, password) { InternalClient.prototype.login = function login(email, password) {
var _this12 = this; var _this13 = this;
var client = this.client; var client = this.client;
if (!this.tokenCacher.done) { if (!this.tokenCacher.done) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
setTimeout(function () { setTimeout(function () {
_this12.login(email, password).then(resolve)["catch"](reject); _this13.login(email, password).then(resolve)["catch"](reject);
}, 20); }, 20);
}); });
} else { } else {
@@ -586,15 +597,15 @@ var InternalClient = (function () {
email: email, email: email,
password: password password: password
}).then(function (res) { }).then(function (res) {
_this12.client.emit("debug", "direct API login, cached token was unavailable"); _this13.client.emit("debug", "direct API login, cached token was unavailable");
var token = res.token; var token = res.token;
_this12.tokenCacher.setToken(email, password, token); _this13.tokenCacher.setToken(email, password, token);
return _this12.loginWithToken(token, email, password); return _this13.loginWithToken(token, email, password);
}, function (error) { }, function (error) {
_this12.websocket = null; _this13.websocket = null;
throw error; throw error;
})["catch"](function (error) { })["catch"](function (error) {
_this12.state = _ConnectionState2["default"].DISCONNECTED; _this13.state = _ConnectionState2["default"].DISCONNECTED;
client.emit("disconnected"); client.emit("disconnected");
throw error; throw error;
}); });
@@ -603,28 +614,28 @@ var InternalClient = (function () {
// def logout // def logout
InternalClient.prototype.logout = function logout() { InternalClient.prototype.logout = function logout() {
var _this13 = this; var _this14 = this;
if (this.state === _ConnectionState2["default"].DISCONNECTED || this.state === _ConnectionState2["default"].IDLE) { if (this.state === _ConnectionState2["default"].DISCONNECTED || this.state === _ConnectionState2["default"].IDLE) {
return Promise.reject(new Error("Client is not logged in!")); return Promise.reject(new Error("Client is not logged in!"));
} }
return this.apiRequest("post", _Constants.Endpoints.LOGOUT, true).then(function () { return this.apiRequest("post", _Constants.Endpoints.LOGOUT, true).then(function () {
if (_this13.websocket) { if (_this14.websocket) {
_this13.websocket.close(); _this14.websocket.close();
_this13.websocket = null; _this14.websocket = null;
} }
_this13.token = null; _this14.token = null;
_this13.email = null; _this14.email = null;
_this13.password = null; _this14.password = null;
_this13.state = _ConnectionState2["default"].DISCONNECTED; _this14.state = _ConnectionState2["default"].DISCONNECTED;
}); });
}; };
// def startPM // def startPM
InternalClient.prototype.startPM = function startPM(resUser) { InternalClient.prototype.startPM = function startPM(resUser) {
var _this14 = this; var _this15 = this;
var user = this.resolver.resolveUser(resUser); var user = this.resolver.resolveUser(resUser);
if (!user) { if (!user) {
@@ -634,7 +645,7 @@ var InternalClient = (function () {
return this.apiRequest("post", _Constants.Endpoints.USER_CHANNELS(this.user.id), true, { return this.apiRequest("post", _Constants.Endpoints.USER_CHANNELS(this.user.id), true, {
recipient_id: user.id recipient_id: user.id
}).then(function (res) { }).then(function (res) {
return _this14.private_channels.add(new _StructuresPMChannel2["default"](res, _this14.client)); return _this15.private_channels.add(new _StructuresPMChannel2["default"](res, _this15.client));
}); });
}; };
@@ -649,19 +660,19 @@ var InternalClient = (function () {
// def sendMessage // def sendMessage
InternalClient.prototype.sendMessage = function sendMessage(where, _content) { InternalClient.prototype.sendMessage = function sendMessage(where, _content) {
var _this15 = this; var _this16 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return this.resolver.resolveChannel(where).then(function (destination) { return this.resolver.resolveChannel(where).then(function (destination) {
//var destination; //var destination;
var content = _this15.resolver.resolveString(_content); var content = _this16.resolver.resolveString(_content);
return _this15.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content, content: content,
tts: options.tts tts: options.tts
}).then(function (res) { }).then(function (res) {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this15.client)); return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client));
}); });
}); });
}; };
@@ -669,7 +680,7 @@ var InternalClient = (function () {
// def deleteMessage // def deleteMessage
InternalClient.prototype.deleteMessage = function deleteMessage(_message) { InternalClient.prototype.deleteMessage = function deleteMessage(_message) {
var _this16 = this; var _this17 = this;
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
@@ -680,7 +691,7 @@ var InternalClient = (function () {
var chain = options.wait ? delay(options.wait) : Promise.resolve(); var chain = options.wait ? delay(options.wait) : Promise.resolve();
return chain.then(function () { return chain.then(function () {
return _this16.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); return _this17.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true);
}).then(function () { }).then(function () {
return message.channel.messages.remove(message); return message.channel.messages.remove(message);
}); });
@@ -689,7 +700,7 @@ var InternalClient = (function () {
// def updateMessage // def updateMessage
InternalClient.prototype.updateMessage = function updateMessage(msg, _content) { InternalClient.prototype.updateMessage = function updateMessage(msg, _content) {
var _this17 = this; var _this18 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
@@ -705,14 +716,14 @@ var InternalClient = (function () {
content: content, content: content,
tts: options.tts tts: options.tts
}).then(function (res) { }).then(function (res) {
return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this17.client)); return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this18.client));
}); });
}; };
// def sendFile // def sendFile
InternalClient.prototype.sendFile = function sendFile(where, _file, name) { InternalClient.prototype.sendFile = function sendFile(where, _file, name) {
var _this18 = this; var _this19 = this;
if (!name) { if (!name) {
if (_file instanceof String || typeof _file === "string") { if (_file instanceof String || typeof _file === "string") {
@@ -726,12 +737,12 @@ var InternalClient = (function () {
} }
return this.resolver.resolveChannel(where).then(function (channel) { return this.resolver.resolveChannel(where).then(function (channel) {
return _this18.resolver.resolveFile(_file).then(function (file) { return _this19.resolver.resolveFile(_file).then(function (file) {
return _this18.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { return _this19.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
name: name, name: name,
file: file file: file
}).then(function (res) { }).then(function (res) {
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this18.client)); return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this19.client));
}); });
}); });
}); });
@@ -740,7 +751,7 @@ var InternalClient = (function () {
// def getChannelLogs // def getChannelLogs
InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) { InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) {
var _this19 = this; var _this20 = this;
var limit = arguments.length <= 1 || arguments[1] === undefined ? 50 : arguments[1]; var limit = arguments.length <= 1 || arguments[1] === undefined ? 50 : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
@@ -748,21 +759,21 @@ var InternalClient = (function () {
return this.resolver.resolveChannel(_channel).then(function (channel) { return this.resolver.resolveChannel(_channel).then(function (channel) {
var qsObject = { limit: limit }; var qsObject = { limit: limit };
if (options.before) { if (options.before) {
var res = _this19.resolver.resolveMessage(options.before); var res = _this20.resolver.resolveMessage(options.before);
if (res) { if (res) {
qsObject.before = res.id; qsObject.before = res.id;
} }
} }
if (options.after) { if (options.after) {
var res = _this19.resolver.resolveMessage(options.after); var res = _this20.resolver.resolveMessage(options.after);
if (res) { if (res) {
qsObject.after = res.id; qsObject.after = res.id;
} }
} }
return _this19.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) { return _this20.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) {
return res.map(function (msg) { return res.map(function (msg) {
return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this19.client)); return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this20.client));
}); });
}); });
}); });
@@ -771,13 +782,13 @@ var InternalClient = (function () {
// def getBans // def getBans
InternalClient.prototype.getBans = function getBans(server) { InternalClient.prototype.getBans = function getBans(server) {
var _this20 = this; var _this21 = this;
server = this.resolver.resolveServer(server); server = this.resolver.resolveServer(server);
return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) { return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) {
return res.map(function (ban) { return res.map(function (ban) {
return _this20.users.add(new _StructuresUser2["default"](ban.user, _this20.client)); return _this21.users.add(new _StructuresUser2["default"](ban.user, _this21.client));
}); });
}); });
}; };
@@ -785,7 +796,7 @@ var InternalClient = (function () {
// def createChannel // def createChannel
InternalClient.prototype.createChannel = function createChannel(server, name) { InternalClient.prototype.createChannel = function createChannel(server, name) {
var _this21 = this; var _this22 = this;
var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2]; var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2];
@@ -797,23 +808,23 @@ var InternalClient = (function () {
}).then(function (res) { }).then(function (res) {
var channel; var channel;
if (res.type === "text") { if (res.type === "text") {
channel = new _StructuresTextChannel2["default"](res, _this21.client, server); channel = new _StructuresTextChannel2["default"](res, _this22.client, server);
} else { } else {
channel = new _StructuresVoiceChannel2["default"](res, _this21.client, server); channel = new _StructuresVoiceChannel2["default"](res, _this22.client, server);
} }
return server.channels.add(_this21.channels.add(channel)); return server.channels.add(_this22.channels.add(channel));
}); });
}; };
// def deleteChannel // def deleteChannel
InternalClient.prototype.deleteChannel = function deleteChannel(_channel) { InternalClient.prototype.deleteChannel = function deleteChannel(_channel) {
var _this22 = this; var _this23 = this;
return this.resolver.resolveChannel(_channel).then(function (channel) { return this.resolver.resolveChannel(_channel).then(function (channel) {
return _this22.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () { return _this23.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
channel.server.channels.remove(channel); channel.server.channels.remove(channel);
_this22.channels.remove(channel); _this23.channels.remove(channel);
}); });
}); });
}; };
@@ -851,7 +862,7 @@ var InternalClient = (function () {
// def moveMember // def moveMember
InternalClient.prototype.moveMember = function moveMember(user, channel) { InternalClient.prototype.moveMember = function moveMember(user, channel) {
var _this23 = this; var _this24 = this;
user = this.resolver.resolveUser(user); user = this.resolver.resolveUser(user);
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
@@ -861,7 +872,7 @@ var InternalClient = (function () {
if (channel.type !== "voice") { if (channel.type !== "voice") {
throw new Error("Can't moveMember into a non-voice channel"); throw new Error("Can't moveMember into a non-voice channel");
} else { } else {
return _this23.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) { return _this24.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) {
user.voiceChannel = channel; user.voiceChannel = channel;
return res; return res;
}); });
@@ -904,15 +915,15 @@ var InternalClient = (function () {
// def createRole // def createRole
InternalClient.prototype.createRole = function createRole(server, data) { InternalClient.prototype.createRole = function createRole(server, data) {
var _this24 = this; var _this25 = this;
server = this.resolver.resolveServer(server); server = this.resolver.resolveServer(server);
return this.apiRequest("post", _Constants.Endpoints.SERVER_ROLES(server.id), true).then(function (res) { return this.apiRequest("post", _Constants.Endpoints.SERVER_ROLES(server.id), true).then(function (res) {
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this24.client)); var role = server.roles.add(new _StructuresRole2["default"](res, server, _this25.client));
if (data) { if (data) {
return _this24.updateRole(role, data); return _this25.updateRole(role, data);
} }
return role; return role;
}); });
@@ -921,7 +932,7 @@ var InternalClient = (function () {
// def updateRole // def updateRole
InternalClient.prototype.updateRole = function updateRole(role, data) { InternalClient.prototype.updateRole = function updateRole(role, data) {
var _this25 = this; var _this26 = this;
role = this.resolver.resolveRole(role); role = this.resolver.resolveRole(role);
var server = this.resolver.resolveServer(role.server); var server = this.resolver.resolveServer(role.server);
@@ -958,7 +969,7 @@ var InternalClient = (function () {
} }
return this.apiRequest("patch", _Constants.Endpoints.SERVER_ROLES(server.id) + "/" + role.id, true, newData).then(function (res) { return this.apiRequest("patch", _Constants.Endpoints.SERVER_ROLES(server.id) + "/" + role.id, true, newData).then(function (res) {
return server.roles.update(role, new _StructuresRole2["default"](res, server, _this25.client)); return server.roles.update(role, new _StructuresRole2["default"](res, server, _this26.client));
}); });
}; };
@@ -975,7 +986,7 @@ var InternalClient = (function () {
//def addMemberToRole //def addMemberToRole
InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) { InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) {
var _this26 = this; var _this27 = this;
member = this.resolver.resolveUser(member); member = this.resolver.resolveUser(member);
@@ -992,7 +1003,7 @@ var InternalClient = (function () {
} }
} else { } else {
roles = roles.map(function (r) { roles = roles.map(function (r) {
return _this26.resolver.resolveRole(r); return _this27.resolver.resolveRole(r);
}); });
} }
@@ -1055,7 +1066,7 @@ var InternalClient = (function () {
//def removeMemberFromRole //def removeMemberFromRole
InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) { InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) {
var _this27 = this; var _this28 = this;
member = this.resolver.resolveUser(member); member = this.resolver.resolveUser(member);
@@ -1072,7 +1083,7 @@ var InternalClient = (function () {
} }
} else { } else {
roles = roles.map(function (r) { roles = roles.map(function (r) {
return _this27.resolver.resolveRole(r); return _this28.resolver.resolveRole(r);
}); });
} }
@@ -1113,7 +1124,7 @@ var InternalClient = (function () {
// def createInvite // def createInvite
InternalClient.prototype.createInvite = function createInvite(chanServ, options) { InternalClient.prototype.createInvite = function createInvite(chanServ, options) {
var _this28 = this; var _this29 = this;
return this.resolver.resolveChannel(chanServ).then(function (channel) { return this.resolver.resolveChannel(chanServ).then(function (channel) {
if (!options) { if (!options) {
@@ -1127,8 +1138,8 @@ var InternalClient = (function () {
options.xkcdpass = options.xkcd || false; options.xkcdpass = options.xkcd || false;
} }
return _this28.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) { return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) {
return new _StructuresInvite2["default"](res, _this28.channels.get("id", res.channel.id), _this28.client); return new _StructuresInvite2["default"](res, _this29.channels.get("id", res.channel.id), _this29.client);
}); });
}); });
}; };
@@ -1146,7 +1157,7 @@ var InternalClient = (function () {
//def getInvite //def getInvite
InternalClient.prototype.getInvite = function getInvite(invite) { InternalClient.prototype.getInvite = function getInvite(invite) {
var _this29 = this; var _this30 = this;
invite = this.resolver.resolveInviteID(invite); invite = this.resolver.resolveInviteID(invite);
if (!invite) { if (!invite) {
@@ -1154,11 +1165,11 @@ var InternalClient = (function () {
} }
return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) { return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) {
if (!_this29.channels.has("id", res.channel.id)) { if (!_this30.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this29.client); return new _StructuresInvite2["default"](res, null, _this30.client);
} }
return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) { return _this30.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this29.channels.get("id", res.channel.id), _this29.client); return new _StructuresInvite2["default"](res2, _this30.channels.get("id", res.channel.id), _this30.client);
}); });
}); });
}; };
@@ -1166,22 +1177,22 @@ var InternalClient = (function () {
//def getInvites //def getInvites
InternalClient.prototype.getInvites = function getInvites(channel) { InternalClient.prototype.getInvites = function getInvites(channel) {
var _this30 = this; var _this31 = this;
if (!(channel instanceof _StructuresChannel2["default"])) { if (!(channel instanceof _StructuresChannel2["default"])) {
var server = this.resolver.resolveServer(channel); var server = this.resolver.resolveServer(channel);
if (server) { if (server) {
return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) { return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) {
return res.map(function (data) { return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client); return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client);
}); });
}); });
} }
} }
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
return _this30.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) { return _this31.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return res.map(function (data) { return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client); return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client);
}); });
}); });
}); });
@@ -1190,7 +1201,7 @@ var InternalClient = (function () {
//def overwritePermissions //def overwritePermissions
InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) { InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) {
var _this31 = this; var _this32 = this;
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
if (!channel instanceof _StructuresServerChannel2["default"]) { if (!channel instanceof _StructuresServerChannel2["default"]) {
@@ -1203,7 +1214,7 @@ var InternalClient = (function () {
}; };
if (role instanceof String || typeof role === "string") { if (role instanceof String || typeof role === "string") {
role = _this31.resolver.resolveUser(role) || _this31.resolver.resolveRole(role); role = _this32.resolver.resolveUser(role) || _this32.resolver.resolveRole(role);
} }
if (role instanceof _StructuresUser2["default"]) { if (role instanceof _StructuresUser2["default"]) {
@@ -1236,7 +1247,7 @@ var InternalClient = (function () {
} }
} }
return _this31.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data); return _this32.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
}); });
}; };
@@ -1275,49 +1286,49 @@ var InternalClient = (function () {
//def sendTyping //def sendTyping
InternalClient.prototype.sendTyping = function sendTyping(channel) { InternalClient.prototype.sendTyping = function sendTyping(channel) {
var _this32 = this; var _this33 = this;
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
return _this32.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true); return _this33.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
}); });
}; };
//def startTyping //def startTyping
InternalClient.prototype.startTyping = function startTyping(channel) { InternalClient.prototype.startTyping = function startTyping(channel) {
var _this33 = this; var _this34 = this;
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
if (_this33.intervals.typing[channel.id]) { if (_this34.intervals.typing[channel.id]) {
// typing interval already exists, leave it alone // typing interval already exists, leave it alone
throw new Error("Already typing in that channel"); throw new Error("Already typing in that channel");
} }
_this33.intervals.typing[channel.id] = setInterval(function () { _this34.intervals.typing[channel.id] = setInterval(function () {
return _this33.sendTyping(channel)["catch"](function (error) { return _this34.sendTyping(channel)["catch"](function (error) {
return _this33.emit("error", error); return _this34.emit("error", error);
}); });
}, 4000); }, 4000);
return _this33.sendTyping(channel); return _this34.sendTyping(channel);
}); });
}; };
//def stopTyping //def stopTyping
InternalClient.prototype.stopTyping = function stopTyping(channel) { InternalClient.prototype.stopTyping = function stopTyping(channel) {
var _this34 = this; var _this35 = this;
return this.resolver.resolveChannel(channel).then(function (channel) { return this.resolver.resolveChannel(channel).then(function (channel) {
if (!_this34.intervals.typing[channel.id]) { if (!_this35.intervals.typing[channel.id]) {
// typing interval doesn"t exist // typing interval doesn"t exist
throw new Error("Not typing in that channel"); throw new Error("Not typing in that channel");
} }
clearInterval(_this34.intervals.typing[channel.id]); clearInterval(_this35.intervals.typing[channel.id]);
_this34.intervals.typing[channel.id] = false; _this35.intervals.typing[channel.id] = false;
}); });
}; };
@@ -1355,12 +1366,12 @@ var InternalClient = (function () {
//def setChannelTopic //def setChannelTopic
InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) { InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) {
var _this35 = this; var _this36 = this;
var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1]; var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) { return this.resolver.resolveChannel(chann).then(function (channel) {
return _this35.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { return _this36.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name, name: channel.name,
position: channel.position, position: channel.position,
topic: topic topic: topic
@@ -1373,12 +1384,12 @@ var InternalClient = (function () {
//def setChannelName //def setChannelName
InternalClient.prototype.setChannelName = function setChannelName(chann) { InternalClient.prototype.setChannelName = function setChannelName(chann) {
var _this36 = this; var _this37 = this;
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) { return this.resolver.resolveChannel(chann).then(function (channel) {
return _this36.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { return _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name, name: name,
position: channel.position, position: channel.position,
topic: channel.topic topic: channel.topic
@@ -1391,13 +1402,13 @@ var InternalClient = (function () {
//def setChannelNameAndTopic //def setChannelNameAndTopic
InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) { InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) {
var _this37 = this; var _this38 = this;
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
var topic = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2]; var topic = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];
return this.resolver.resolveChannel(chann).then(function (channel) { return this.resolver.resolveChannel(chann).then(function (channel) {
return _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { return _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name, name: name,
position: channel.position, position: channel.position,
topic: topic topic: topic
@@ -1411,12 +1422,12 @@ var InternalClient = (function () {
//def setTopic //def setTopic
InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) { InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) {
var _this38 = this; var _this39 = this;
var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) { return this.resolver.resolveChannel(chann).then(function (channel) {
return _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { return _this39.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name, name: channel.name,
position: position, position: position,
topic: channel.topic topic: channel.topic
@@ -1478,7 +1489,7 @@ var InternalClient = (function () {
}; };
InternalClient.prototype.createWS = function createWS(url) { InternalClient.prototype.createWS = function createWS(url) {
var _this39 = this; var _this40 = this;
var self = this; var self = this;
var client = self.client; var client = self.client;
@@ -1986,7 +1997,7 @@ var InternalClient = (function () {
data.id = data.id || user.id; data.id = data.id || user.id;
data.avatar = data.avatar || user.avatar; data.avatar = data.avatar || user.avatar;
data.discriminator = data.discriminator || user.discriminator; data.discriminator = data.discriminator || user.discriminator;
_this39.email = data.email || _this39.email; _this40.email = data.email || _this40.email;
var presenceUser = new _StructuresUser2["default"](data, client); var presenceUser = new _StructuresUser2["default"](data, client);

View File

@@ -143,7 +143,9 @@ export default class InternalClient {
this.cleanIntervals(); this.cleanIntervals();
this.leaveVoiceChannel(); this.voiceConnections.forEach(vc => {
this.leaveVoiceChannel(vc);
});
if (this.client.options.revive && !forced) { if (this.client.options.revive && !forced) {
this.setup(); this.setup();
@@ -208,7 +210,7 @@ export default class InternalClient {
// preserve old functionality for non-bots // preserve old functionality for non-bots
if (this.voiceConnections[0]) { if (this.voiceConnections[0]) {
this.voiceConnections[0].destroy(); this.voiceConnections[0].destroy();
this.voiceConnections = []; this.voiceConnections.remove(this.voiceConnections[0])
} }
return Promise.resolve(); return Promise.resolve();
} }
@@ -258,43 +260,48 @@ export default class InternalClient {
}); });
} }
var joinVoice = new Promise((resolve, reject) => { var joinVoice = () => {
var session, token, server = channel.server, endpoint; return new Promise((resolve, reject) => {
var session, token, server = channel.server, endpoint;
var check = m => { var check = m => {
var data = JSON.parse(m); var data = JSON.parse(m);
if (data.d.guild_id !== server.id) return // ensure it is the right server if (data.d.guild_id !== server.id) return // ensure it is the right server
if (data.t === "VOICE_STATE_UPDATE") { if (data.t === "VOICE_STATE_UPDATE") {
session = data.d.session_id; session = data.d.session_id;
} else if (data.t === "VOICE_SERVER_UPDATE") { } else if (data.t === "VOICE_SERVER_UPDATE") {
token = data.d.token; token = data.d.token;
endpoint = data.d.endpoint; endpoint = data.d.endpoint;
var chan = new VoiceConnection( var chan = new VoiceConnection(
channel, this.client, session, token, server, endpoint channel, this.client, session, token, server, endpoint
); );
this.voiceConnections.add(chan); this.voiceConnections.add(chan);
chan.on("ready", () => resolve(chan)); chan.on("ready", () => resolve(chan));
chan.on("error", reject); chan.on("error", reject);
this.client.emit("debug", "removed temporary voice websocket listeners"); this.client.emit("debug", "removed temporary voice websocket listeners");
this.websocket.removeListener("message", check); this.websocket.removeListener("message", check);
} }
}; };
this.websocket.on("message", check); this.websocket.on("message", check);
joinSendWS(); joinSendWS();
}); });
}
if (!this.user.bot && this.voiceConnections.length > 0) // nonbot, one voiceconn only, just like last time just disconnect
return this.leaveVoiceChannel(this.voiceConnections[0]).then(joinVoice);
var existingServerConn = this.voiceConnections.get("server", channel.server); // same server connection var existingServerConn = this.voiceConnections.get("server", channel.server); // same server connection
if (existingServerConn) if (existingServerConn) {
joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler
return Promise.resolve(existingServerConn);
}
return joinVoice; if (!this.user.bot && this.voiceConnections.length > 0) { // nonbot, one voiceconn only, just like last time just disconnect
return this.leaveVoiceChannel().then(joinVoice);
}
return joinVoice();
}); });
} }