partially working autorevive

This commit is contained in:
Amish Shah
2015-12-06 19:30:32 +00:00
parent 41efef750f
commit d504406cfc
5 changed files with 81 additions and 17 deletions

View File

@@ -33,11 +33,15 @@ var Client = (function (_EventEmitter) {
client. client.
*/ */
function Client(options) { function Client() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, Client); _classCallCheck(this, Client);
_EventEmitter.call(this); _EventEmitter.call(this);
this.options = options || {}; this.options = options || {};
this.options.compress = options.compress || true;
this.options.autoRevive = options.autoRevive || false;
this.internal = new _InternalClient2["default"](this); this.internal = new _InternalClient2["default"](this);
} }
@@ -60,6 +64,18 @@ var Client = (function (_EventEmitter) {
return this.internal.logout().then(callback, errCB(callback)); return this.internal.logout().then(callback, errCB(callback));
}; };
// def destroy
Client.prototype.destroy = function destroy() {
var _this = this;
var callback = arguments.length <= 0 || arguments[0] === undefined ? function () /*err*/{} : arguments[0];
this.internal.logout().then(function () {
_this.internal.disconnected(true);
});
};
// def sendMessage // def sendMessage
Client.prototype.sendMessage = function sendMessage(where, content) { Client.prototype.sendMessage = function sendMessage(where, content) {
@@ -551,7 +567,7 @@ var Client = (function (_EventEmitter) {
Client.prototype.awaitResponse = function awaitResponse(msg) { Client.prototype.awaitResponse = function awaitResponse(msg) {
var toSend = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; var toSend = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var _this = this; var _this2 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*e, newMsg*/{} : arguments[3]; var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*e, newMsg*/{} : arguments[3];
@@ -585,7 +601,7 @@ var Client = (function (_EventEmitter) {
} }
// (msg) promise // (msg) promise
return ret.then(function () { return ret.then(function () {
return _this.internal.awaitResponse(msg); return _this2.internal.awaitResponse(msg);
}).then(function (newMsg) { }).then(function (newMsg) {
callback(null, newMsg); callback(null, newMsg);
return newMsg; return newMsg;

View File

@@ -125,6 +125,11 @@ var InternalClient = (function () {
function InternalClient(discordClient) { function InternalClient(discordClient) {
_classCallCheck(this, InternalClient); _classCallCheck(this, InternalClient);
this.setup(discordClient);
}
InternalClient.prototype.setup = function setup(discordClient) {
discordClient = discordClient || this.client;
this.client = discordClient; this.client = discordClient;
this.state = _ConnectionState2["default"].IDLE; this.state = _ConnectionState2["default"].IDLE;
this.websocket = null; this.websocket = null;
@@ -149,7 +154,7 @@ var InternalClient = (function () {
this.resolver = new _ResolverResolver2["default"](this); this.resolver = new _ResolverResolver2["default"](this);
this.readyTime = null; this.readyTime = null;
this.messageAwaits = {}; this.messageAwaits = {};
} };
InternalClient.prototype.cleanIntervals = function cleanIntervals() { InternalClient.prototype.cleanIntervals = function cleanIntervals() {
for (var _iterator = this.intervals.typing.concat(this.intervals.misc).concat(this.intervals.kai), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { for (var _iterator = this.intervals.typing.concat(this.intervals.misc).concat(this.intervals.kai), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
@@ -172,6 +177,21 @@ var InternalClient = (function () {
} }
}; };
InternalClient.prototype.disconnected = function disconnected() {
var forced = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
this.cleanIntervals();
this.leaveVoiceChannel();
if (this.client.options.autoRevive && !forced) {
this.setup();
this.login(this.email, this.password);
}
this.client.emit("disconnected");
};
//def leaveVoiceChannel //def leaveVoiceChannel
InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel() { InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel() {
@@ -1137,11 +1157,7 @@ var InternalClient = (function () {
this.websocket.onclose = function () { this.websocket.onclose = function () {
self.websocket = null; self.websocket = null;
self.state = _ConnectionState2["default"].DISCONNECTED; self.state = _ConnectionState2["default"].DISCONNECTED;
client.emit("disconnected"); self.disconnected();
self.cleanIntervals();
if (self.voiceConnection) {
self.leaveVoiceChannel();
}
}; };
this.websocket.onerror = function (e) { this.websocket.onerror = function (e) {

View File

@@ -15,7 +15,7 @@ export default class Client extends EventEmitter {
this class is an interface for the internal this class is an interface for the internal
client. client.
*/ */
constructor(options) { constructor(options = {}) {
super(); super();
this.options = options || {}; this.options = options || {};
this.options.compress = options.compress || true; this.options.compress = options.compress || true;
@@ -69,6 +69,15 @@ export default class Client extends EventEmitter {
return this.internal.logout() return this.internal.logout()
.then(callback, errCB(callback)); .then(callback, errCB(callback));
} }
// def destroy
destroy(callback = (/*err*/) => { }) {
this.internal.logout()
.then(() => {
this.internal.disconnected(true);
});
}
// def sendMessage // def sendMessage
sendMessage(where, content, options = {}, callback = (/*e, m*/) => {}) { sendMessage(where, content, options = {}, callback = (/*e, m*/) => {}) {
if (typeof options === "function") { if (typeof options === "function") {

View File

@@ -62,6 +62,11 @@ function delay(ms) {
export default class InternalClient { export default class InternalClient {
constructor(discordClient) { constructor(discordClient) {
this.setup(discordClient);
}
setup(discordClient) {
discordClient = discordClient || this.client;
this.client = discordClient; this.client = discordClient;
this.state = ConnectionState.IDLE; this.state = ConnectionState.IDLE;
this.websocket = null; this.websocket = null;
@@ -96,6 +101,20 @@ export default class InternalClient {
} }
} }
disconnected(forced = false){
this.cleanIntervals();
this.leaveVoiceChannel();
if(this.client.options.autoRevive && !forced){
this.setup();
this.login(this.email, this.password);
}
this.client.emit("disconnected");
}
get uptime() { get uptime() {
return (this.readyTime ? Date.now() - this.readyTime : null); return (this.readyTime ? Date.now() - this.readyTime : null);
} }
@@ -1028,11 +1047,7 @@ export default class InternalClient {
this.websocket.onclose = () => { this.websocket.onclose = () => {
self.websocket = null; self.websocket = null;
self.state = ConnectionState.DISCONNECTED; self.state = ConnectionState.DISCONNECTED;
client.emit("disconnected"); self.disconnected();
self.cleanIntervals();
if(self.voiceConnection){
self.leaveVoiceChannel();
}
}; };
this.websocket.onerror = e => { this.websocket.onerror = e => {

View File

@@ -2,7 +2,7 @@
/* global process */ /* global process */
var Discord = require("../"); var Discord = require("../");
var client = new Discord.Client(); var client = new Discord.Client({autoRevive : true});
var request = require("superagent"); var request = require("superagent");
client.on("ready", () => { client.on("ready", () => {
@@ -10,10 +10,14 @@ client.on("ready", () => {
setTimeout(() => { setTimeout(() => {
client.internal.websocket.close(); client.internal.websocket.close();
}, 3000); }, 10000);
}); });
client.on("autoRevive", () => {
console.log("auto revived");
});
client.on("message", msg => { client.on("message", msg => {
if(!msg.sender.equals(client.user)) if(!msg.sender.equals(client.user))
@@ -23,6 +27,10 @@ client.on("message", msg => {
msg.channel.server.channels.get("type", "voice").join(); msg.channel.server.channels.get("type", "voice").join();
} }
if (msg.content === "end") {
client.destroy();
}
if (msg.content.startsWith("$play")) { if (msg.content.startsWith("$play")) {
var url = msg.content.split(" ")[1]; var url = msg.content.split(" ")[1];