Rebuild lib

This commit is contained in:
meew0
2016-01-24 14:38:27 +01:00
parent 2d68849027
commit c5174b4d93
2 changed files with 126 additions and 101 deletions

View File

@@ -63,6 +63,23 @@ var Client = (function (_EventEmitter) {
this.internal = new _InternalClient2["default"](this);
}
// def loginWithToken
Client.prototype.loginWithToken = function loginWithToken(token) {
var email = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var password = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*err, token*/{} : arguments[3];
if (typeof email === "function") {
// email is the callback
callback = email;
email = null;
password = null;
}
return this.internal.loginWithToken(token, email, password).then(dataCallback(callback), errorCallback(callback));
};
// def login
Client.prototype.login = function login(email, password) {

View File

@@ -218,7 +218,13 @@ var InternalClient = (function () {
if (this.client.options.revive && !forced) {
this.setup();
this.login(this.email, this.password);
// Check whether the email is set (if not, only a token has been used for login)
if (this.email) {
this.login(this.email, this.password);
} else {
this.loginWithToken(this.token);
}
}
this.client.emit("disconnected");
@@ -394,34 +400,41 @@ var InternalClient = (function () {
});
};
// def loginWithToken
// email and password are optional
InternalClient.prototype.loginWithToken = function loginWithToken(token, email, password) {
var _this8 = this;
this.state = _ConnectionState2["default"].LOGGED_IN;
this.token = token;
this.email = email;
this.password = password;
return this.getGateway().then(function (url) {
_this8.createWS(url);
return token;
});
};
// def login
InternalClient.prototype.login = function login(email, password) {
var _this8 = this;
var _this9 = this;
var client = this.client;
if (!this.tokenCacher.done) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
_this8.login(email, password).then(resolve)["catch"](reject);
_this9.login(email, password).then(resolve)["catch"](reject);
}, 20);
});
} else {
var tk = this.tokenCacher.getToken(email, password);
if (tk) {
this.client.emit("debug", "bypassed direct API login, used cached token");
this.state = _ConnectionState2["default"].LOGGED_IN;
this.token = tk;
this.email = email;
this.password = password;
return this.getGateway().then(function (url) {
_this8.createWS(url);
return tk;
});
return Promise.resolve(tk);
return loginWithToken(tk, email, password);
}
}
@@ -435,23 +448,15 @@ var InternalClient = (function () {
email: email,
password: password
}).then(function (res) {
_this8.client.emit("debug", "direct API login, cached token was unavailable");
_this9.client.emit("debug", "direct API login, cached token was unavailable");
var token = res.token;
_this8.tokenCacher.setToken(email, password, token);
_this8.state = _ConnectionState2["default"].LOGGED_IN;
_this8.token = token;
_this8.email = email;
_this8.password = password;
return _this8.getGateway().then(function (url) {
_this8.createWS(url);
return token;
});
_this9.tokenCacher.setToken(email, password, token);
loginWithToken(token, email, password);
}, function (error) {
_this8.websocket = null;
_this9.websocket = null;
throw error;
})["catch"](function (error) {
_this8.state = _ConnectionState2["default"].DISCONNECTED;
_this9.state = _ConnectionState2["default"].DISCONNECTED;
client.emit("disconnected");
throw error;
});
@@ -460,28 +465,28 @@ var InternalClient = (function () {
// def logout
InternalClient.prototype.logout = function logout() {
var _this9 = this;
var _this10 = this;
if (this.state === _ConnectionState2["default"].DISCONNECTED || this.state === _ConnectionState2["default"].IDLE) {
return Promise.reject(new Error("Client is not logged in!"));
}
return this.apiRequest("post", _Constants.Endpoints.LOGOUT, true).then(function () {
if (_this9.websocket) {
_this9.websocket.close();
_this9.websocket = null;
if (_this10.websocket) {
_this10.websocket.close();
_this10.websocket = null;
}
_this9.token = null;
_this9.email = null;
_this9.password = null;
_this9.state = _ConnectionState2["default"].DISCONNECTED;
_this10.token = null;
_this10.email = null;
_this10.password = null;
_this10.state = _ConnectionState2["default"].DISCONNECTED;
});
};
// def startPM
InternalClient.prototype.startPM = function startPM(resUser) {
var _this10 = this;
var _this11 = this;
var user = this.resolver.resolveUser(resUser);
if (!user) {
@@ -491,7 +496,7 @@ var InternalClient = (function () {
return this.apiRequest("post", _Constants.Endpoints.USER_CHANNELS(user.id), true, {
recipient_id: user.id
}).then(function (res) {
return _this10.private_channels.add(new _StructuresPMChannel2["default"](res, _this10.client));
return _this11.private_channels.add(new _StructuresPMChannel2["default"](res, _this11.client));
});
};
@@ -506,19 +511,19 @@ var InternalClient = (function () {
// def sendMessage
InternalClient.prototype.sendMessage = function sendMessage(where, _content) {
var _this11 = this;
var _this12 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return this.resolver.resolveChannel(where).then(function (destination) {
//var destination;
var content = _this11.resolver.resolveString(_content);
var content = _this12.resolver.resolveString(_content);
return _this11.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
return _this12.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content,
tts: options.tts
}).then(function (res) {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this11.client));
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this12.client));
});
});
};
@@ -526,7 +531,7 @@ var InternalClient = (function () {
// def deleteMessage
InternalClient.prototype.deleteMessage = function deleteMessage(_message) {
var _this12 = this;
var _this13 = this;
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
@@ -537,7 +542,7 @@ var InternalClient = (function () {
var chain = options.wait ? delay(options.wait) : Promise.resolve();
return chain.then(function () {
return _this12.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true);
return _this13.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true);
}).then(function () {
return message.channel.messages.remove(message);
});
@@ -546,7 +551,7 @@ var InternalClient = (function () {
// def updateMessage
InternalClient.prototype.updateMessage = function updateMessage(msg, _content) {
var _this13 = this;
var _this14 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
@@ -562,23 +567,23 @@ var InternalClient = (function () {
content: content,
tts: options.tts
}).then(function (res) {
return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this13.client));
return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this14.client));
});
};
// def sendFile
InternalClient.prototype.sendFile = function sendFile(where, _file) {
var _this14 = this;
var _this15 = this;
var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2];
return this.resolver.resolveChannel(where).then(function (channel) {
return _this14.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
return _this15.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
name: name,
file: _this14.resolver.resolveFile(_file)
file: _this15.resolver.resolveFile(_file)
}).then(function (res) {
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this14.client));
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this15.client));
});
});
};
@@ -586,7 +591,7 @@ var InternalClient = (function () {
// def getChannelLogs
InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) {
var _this15 = this;
var _this16 = this;
var limit = arguments.length <= 1 || arguments[1] === undefined ? 50 : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
@@ -594,21 +599,21 @@ var InternalClient = (function () {
return this.resolver.resolveChannel(_channel).then(function (channel) {
var qsObject = { limit: limit };
if (options.before) {
var res = _this15.resolver.resolveMessage(options.before);
var res = _this16.resolver.resolveMessage(options.before);
if (res) {
qsObject.before = res;
}
}
if (options.after) {
var res = _this15.resolver.resolveMessage(options.after);
var res = _this16.resolver.resolveMessage(options.after);
if (res) {
qsObject.after = res;
}
}
return _this15.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) {
return _this16.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) {
return res.map(function (msg) {
return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this15.client));
return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this16.client));
});
});
});
@@ -617,13 +622,13 @@ var InternalClient = (function () {
// def getBans
InternalClient.prototype.getBans = function getBans(server) {
var _this16 = this;
var _this17 = this;
server = this.resolver.resolveServer(server);
return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) {
return res.map(function (ban) {
return _this16.users.add(new _StructuresUser2["default"](ban.user, _this16.client));
return _this17.users.add(new _StructuresUser2["default"](ban.user, _this17.client));
});
});
};
@@ -631,7 +636,7 @@ var InternalClient = (function () {
// def createChannel
InternalClient.prototype.createChannel = function createChannel(server, name) {
var _this17 = this;
var _this18 = this;
var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2];
@@ -643,23 +648,23 @@ var InternalClient = (function () {
}).then(function (res) {
var channel;
if (res.type === "text") {
channel = new _StructuresTextChannel2["default"](res, _this17.client, server);
channel = new _StructuresTextChannel2["default"](res, _this18.client, server);
} else {
channel = new _StructuresVoiceChannel2["default"](res, _this17.client, server);
channel = new _StructuresVoiceChannel2["default"](res, _this18.client, server);
}
return server.channels.add(_this17.channels.add(channel));
return server.channels.add(_this18.channels.add(channel));
});
};
// def deleteChannel
InternalClient.prototype.deleteChannel = function deleteChannel(_channel) {
var _this18 = this;
var _this19 = this;
return this.resolver.resolveChannel(_channel).then(function (channel) {
return _this18.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
return _this19.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
channel.server.channels.remove(channel);
_this18.channels.remove(channel);
_this19.channels.remove(channel);
});
});
};
@@ -697,15 +702,15 @@ var InternalClient = (function () {
// def createRole
InternalClient.prototype.createRole = function createRole(server, data) {
var _this19 = this;
var _this20 = this;
server = this.resolver.resolveServer(server);
return this.apiRequest("post", _Constants.Endpoints.SERVER_ROLES(server.id), true).then(function (res) {
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this19.client));
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this20.client));
if (data) {
return _this19.updateRole(role, data);
return _this20.updateRole(role, data);
}
return role;
});
@@ -714,7 +719,7 @@ var InternalClient = (function () {
// def updateRole
InternalClient.prototype.updateRole = function updateRole(role, data) {
var _this20 = this;
var _this21 = this;
var server = this.resolver.resolveServer(role.server);
@@ -750,7 +755,7 @@ var InternalClient = (function () {
}
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, _this20.client));
return server.roles.update(role, new _StructuresRole2["default"](res, server, _this21.client));
});
};
@@ -895,7 +900,7 @@ var InternalClient = (function () {
// def createInvite
InternalClient.prototype.createInvite = function createInvite(chanServ, options) {
var _this21 = this;
var _this22 = this;
if (chanServ instanceof _StructuresChannel2["default"]) {
// do something
@@ -928,7 +933,7 @@ var InternalClient = (function () {
}
return this.apiRequest("post", epoint, true, options).then(function (res) {
return new _StructuresInvite2["default"](res, _this21.channels.get("id", res.channel.id), _this21.client);
return new _StructuresInvite2["default"](res, _this22.channels.get("id", res.channel.id), _this22.client);
});
};
@@ -945,7 +950,7 @@ var InternalClient = (function () {
//def getInvite
InternalClient.prototype.getInvite = function getInvite(invite) {
var _this22 = this;
var _this23 = this;
invite = this.resolver.resolveInviteID(invite);
if (!invite) {
@@ -953,11 +958,11 @@ var InternalClient = (function () {
}
return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) {
if (!_this22.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this22.client);
if (!_this23.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this23.client);
}
return _this22.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this22.channels.get("id", res.channel.id), _this22.client);
return _this23.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this23.channels.get("id", res.channel.id), _this23.client);
});
});
};
@@ -965,22 +970,22 @@ var InternalClient = (function () {
//def getInvites
InternalClient.prototype.getInvites = function getInvites(channel) {
var _this23 = this;
var _this24 = this;
if (!(channel instanceof _StructuresChannel2["default"])) {
var server = this.resolver.resolveServer(channel);
if (server) {
return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this23.channels.get("id", data.channel.id), _this23.client);
return new _StructuresInvite2["default"](data, _this24.channels.get("id", data.channel.id), _this24.client);
});
});
}
}
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this23.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return _this24.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this23.channels.get("id", data.channel.id), _this23.client);
return new _StructuresInvite2["default"](data, _this24.channels.get("id", data.channel.id), _this24.client);
});
});
});
@@ -989,7 +994,7 @@ var InternalClient = (function () {
//def overwritePermissions
InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) {
var _this24 = this;
var _this25 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
var user;
@@ -1030,7 +1035,7 @@ var InternalClient = (function () {
}
}
return _this24.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
return _this25.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
});
};
@@ -1066,55 +1071,58 @@ var InternalClient = (function () {
//def sendTyping
InternalClient.prototype.sendTyping = function sendTyping(channel) {
var _this25 = this;
var _this26 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this25.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
return _this26.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
});
};
//def startTyping
InternalClient.prototype.startTyping = function startTyping(channel) {
var _this26 = this;
var _this27 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (_this26.intervals.typing[channel.id]) {
if (_this27.intervals.typing[channel.id]) {
// typing interval already exists, leave it alone
throw new Error("Already typing in that channel");
}
_this26.intervals.typing[channel.id] = setInterval(function () {
return _this26.sendTyping(channel)["catch"](function (error) {
return _this26.emit("error", error);
_this27.intervals.typing[channel.id] = setInterval(function () {
return _this27.sendTyping(channel)["catch"](function (error) {
return _this27.emit("error", error);
});
}, 4000);
return _this26.sendTyping(channel);
return _this27.sendTyping(channel);
});
};
//def stopTyping
InternalClient.prototype.stopTyping = function stopTyping(channel) {
var _this27 = this;
var _this28 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!_this27.intervals.typing[channel.id]) {
if (!_this28.intervals.typing[channel.id]) {
// typing interval doesn"t exist
throw new Error("Not typing in that channel");
}
clearInterval(_this27.intervals.typing[channel.id]);
_this27.intervals.typing[channel.id] = false;
clearInterval(_this28.intervals.typing[channel.id]);
_this28.intervals.typing[channel.id] = false;
});
};
//def updateDetails
InternalClient.prototype.updateDetails = function updateDetails(data) {
if (!email) {
throw new Error("Can't use updateDetails because only a token has been used for login!");
}
return this.apiRequest("patch", _Constants.Endpoints.ME, true, {
avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar,
email: data.email || this.email,
@@ -1139,12 +1147,12 @@ var InternalClient = (function () {
//def setChannelTopic
InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) {
var _this28 = this;
var _this29 = this;
var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this28.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this29.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name,
position: channel.position,
topic: topic
@@ -1157,12 +1165,12 @@ var InternalClient = (function () {
//def setChannelName
InternalClient.prototype.setChannelName = function setChannelName(chann) {
var _this29 = this;
var _this30 = this;
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this29.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this30.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name,
position: channel.position,
topic: channel.topic
@@ -1175,13 +1183,13 @@ var InternalClient = (function () {
//def setChannelNameAndTopic
InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) {
var _this30 = this;
var _this31 = this;
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) {
return _this30.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this31.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name,
position: channel.position,
topic: topic
@@ -1195,12 +1203,12 @@ var InternalClient = (function () {
//def setTopic
InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) {
var _this31 = this;
var _this32 = this;
var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this31.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this32.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name,
position: position,
topic: channel.topic