Added message sending

This commit is contained in:
hydrabolt
2015-10-31 22:59:53 +00:00
parent cc3f9d931a
commit 28308433da
14 changed files with 585 additions and 82 deletions

View File

@@ -23,9 +23,7 @@ var Client = (function (_EventEmitter) {
this.internal = new InternalClient(this);
}
/*
def login
*/
// def login
Client.prototype.login = function login(email, password) {
var cb = arguments.length <= 2 || arguments[2] === undefined ? function (err, token) {} : arguments[2];
@@ -43,9 +41,104 @@ var Client = (function (_EventEmitter) {
});
};
/*
def logout
*/
// def logout
Client.prototype.logout = function logout() {
var cb = arguments.length <= 0 || arguments[0] === undefined ? function (err) {} : arguments[0];
var self = this;
return new Promise(function (resolve, reject) {
self.internal.logout().then(function () {
cb();
resolve();
})["catch"](function (e) {
cb(e);
reject(e);
});
});
};
Client.prototype.sendMessage = function sendMessage(where, content) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3];
var self = this;
return new Promise(function (resolve, reject) {
if (typeof options === "function") {
// options is the callback
callback = options;
}
self.internal.sendMessage(where, content, options).then(function (m) {
callback(null, m);
resolve(m);
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
Client.prototype.sendTTSMessage = function sendTTSMessage(where, content) {
var callback = arguments.length <= 2 || arguments[2] === undefined ? function (e, m) {} : arguments[2];
var self = this;
return new Promise(function (resolve, reject) {
self.sendMessage(where, content, { tts: true }).then(function (m) {
callback(null, m);
resolve(m);
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
Client.prototype.reply = function reply(where, content) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3];
var self = this;
return new Promise(function (resolve, reject) {
if (typeof options === "function") {
// options is the callback
callback = options;
}
var msg = self.internal.resolver.resolveMessage(where);
if (msg) {
content = msg.author + ", " + content;
self.internal.sendMessage(msg, content, options).then(function (m) {
callback(null, m);
resolve(m);
})["catch"](function (e) {
callback(e);
reject(e);
});
} else {
var err = new Error("Destination not resolvable to a message!");
callback(err);
reject(err);
}
});
};
Client.prototype.replyTTS = function replyTTS(where, content) {
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () {} : arguments[2];
return new Promise(function (resolve, reject) {
self.reply(where, content, { tts: true }).then(function (m) {
callback(null, m);
resolve(m);
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
return Client;
})(EventEmitter);