Added deleting messages

This commit is contained in:
hydrabolt
2015-10-31 23:12:14 +00:00
parent 28308433da
commit a333548c00
10 changed files with 195 additions and 44 deletions

View File

@@ -59,6 +59,8 @@ var Client = (function (_EventEmitter) {
}); });
}; };
// def sendMessage
Client.prototype.sendMessage = function sendMessage(where, content) { Client.prototype.sendMessage = function sendMessage(where, content) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3]; var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3];
@@ -81,6 +83,8 @@ var Client = (function (_EventEmitter) {
}); });
}; };
// def sendTTSMessage
Client.prototype.sendTTSMessage = function sendTTSMessage(where, content) { Client.prototype.sendTTSMessage = function sendTTSMessage(where, content) {
var callback = arguments.length <= 2 || arguments[2] === undefined ? function (e, m) {} : arguments[2]; var callback = arguments.length <= 2 || arguments[2] === undefined ? function (e, m) {} : arguments[2];
@@ -96,6 +100,8 @@ var Client = (function (_EventEmitter) {
}); });
}; };
// def reply
Client.prototype.reply = function reply(where, content) { Client.prototype.reply = function reply(where, content) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3]; var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, m) {} : arguments[3];
@@ -126,6 +132,8 @@ var Client = (function (_EventEmitter) {
}); });
}; };
// def replyTTS
Client.prototype.replyTTS = function replyTTS(where, content) { Client.prototype.replyTTS = function replyTTS(where, content) {
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () {} : arguments[2]; var callback = arguments.length <= 2 || arguments[2] === undefined ? function () {} : arguments[2];
@@ -140,6 +148,27 @@ var Client = (function (_EventEmitter) {
}); });
}; };
Client.prototype.deleteMessage = function deleteMessage(msg) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var callback = arguments.length <= 2 || arguments[2] === undefined ? function (e) {} : arguments[2];
var self = this;
return new Promise(function (resolve, reject) {
if (typeof options === "function") {
// options is the callback
callback = options;
}
self.internal.deleteMessage(msg).then(function () {
callback();
resolve();
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
return Client; return Client;
})(EventEmitter); })(EventEmitter);

View File

@@ -184,6 +184,37 @@ var InternalClient = (function () {
}); });
}; };
InternalClient.prototype.deleteMessage = function deleteMessage(_message) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var self = this;
return new Promise(function (resolve, reject) {
var message = self.resolver.resolveMessage(_message);
if (message) {
var deleteMsg = function deleteMsg() {
request.del(Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id)).set("authorization", self.token).end(function (err, res) {
if (err) {
reject(new Error(err.response.text));
} else {
message.channel.messages.remove(message);
resolve();
}
});
};
if (options.wait) {
setTimeout(deleteMsg, options.wait);
} else {
deleteMsg();
}
} else {
reject(new Error("Supplied message did not resolve to a message!"));
}
});
};
InternalClient.prototype.sendWS = function sendWS(object) { InternalClient.prototype.sendWS = function sendWS(object) {
this.websocket.send(JSON.stringify(object)); this.websocket.send(JSON.stringify(object));
}; };

View File

@@ -63,6 +63,9 @@ var Endpoints = {
}, },
CHANNEL_PERMISSIONS: function CHANNEL_PERMISSIONS(channelID) { CHANNEL_PERMISSIONS: function CHANNEL_PERMISSIONS(channelID) {
return Endpoints.CHANNEL(channelID) + "/permissions"; return Endpoints.CHANNEL(channelID) + "/permissions";
},
CHANNEL_MESSAGE: function CHANNEL_MESSAGE(channelID, messageID) {
return Endpoints.CHANNEL_MESSAGES(channelID) + "/" + messageID;
} }
}; };

View File

@@ -68,6 +68,19 @@ var Cache = (function (_Array) {
} }
}; };
Cache.prototype.remove = function remove(data) {
var index = this.indexOf(data);
if (~index) {
this.splice(index, 1);
} else {
var item = this.get("id", data.id);
if (item) {
this.splice(this.indexOf(item), 1);
}
}
return false;
};
return Cache; return Cache;
})(Array); })(Array);

View File

@@ -10,8 +10,8 @@ a.on("debug", function (m) {
}); });
a.on("message", function (m) { a.on("message", function (m) {
if (m.content === "$$$") a.reply(m, "hi man!")["catch"](function (e) { if (m.content === "$$$") a.reply(m, "hi man!").then(function (m) {
return console.log(e.stack); a.deleteMessage(m);
}); });
}); });

View File

@@ -3,40 +3,40 @@
var InternalClient = require("./InternalClient.js"); var InternalClient = require("./InternalClient.js");
var EventEmitter = require("events"); var EventEmitter = require("events");
class Client extends EventEmitter{ 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.internal = new InternalClient(this); this.internal = new InternalClient(this);
} }
// def login // def login
login(email, password, cb=function(err, token){}){ login(email, password, cb = function (err, token) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.internal.login(email, password) self.internal.login(email, password)
.then((token)=>{ .then((token) => {
cb(null, token); cb(null, token);
resolve(token); resolve(token);
}) })
.catch((e)=>{ .catch((e) => {
cb(e); cb(e);
reject(e); reject(e);
}); });
}); });
} }
// def logout // def logout
logout(cb=function(err){}){ logout(cb = function (err) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.internal.logout() self.internal.logout()
.then(() => { .then(() => {
cb(); cb();
@@ -46,19 +46,19 @@ class Client extends EventEmitter{
cb(e); cb(e);
reject(e); reject(e);
}) })
}) })
} }
// def sendMessage
sendMessage(where, content, options={}, callback=function(e, m){}){ sendMessage(where, content, options = {}, callback = function (e, m) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(typeof options === "function"){ if (typeof options === "function") {
// options is the callback // options is the callback
callback = options; callback = options;
} }
self.internal.sendMessage(where, content, options) self.internal.sendMessage(where, content, options)
.then(m => { .then(m => {
callback(null, m); callback(null, m);
@@ -67,14 +67,15 @@ class Client extends EventEmitter{
callback(e); callback(e);
reject(e); reject(e);
}); });
}); });
} }
sendTTSMessage(where, content, callback=function(e, m){}){ // def sendTTSMessage
sendTTSMessage(where, content, callback = function (e, m) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.sendMessage(where, content, {tts:true}) self.sendMessage(where, content, { tts: true })
.then(m => { .then(m => {
callback(null, m); callback(null, m);
resolve(m); resolve(m);
@@ -82,42 +83,43 @@ class Client extends EventEmitter{
callback(e); callback(e);
reject(e); reject(e);
}); });
}); });
} }
// def reply
reply(where, content, options={}, callback=function(e,m){}){ reply(where, content, options = {}, callback = function (e, m) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(typeof options === "function"){ if (typeof options === "function") {
// options is the callback // options is the callback
callback = options; callback = options;
} }
var msg = self.internal.resolver.resolveMessage(where); var msg = self.internal.resolver.resolveMessage(where);
if(msg){ if (msg) {
content = msg.author + ", " + content; content = msg.author + ", " + content;
self.internal.sendMessage(msg, content, options) self.internal.sendMessage(msg, content, options)
.then(m => { .then(m => {
callback(null, m); callback(null, m);
resolve(m); resolve(m);
}).catch(e => { }).catch(e => {
callback(e); callback(e);
reject(e); reject(e);
}); });
}else{ } else {
var err = new Error("Destination not resolvable to a message!"); var err = new Error("Destination not resolvable to a message!");
callback(err); callback(err);
reject(err); reject(err);
} }
}); });
} }
replyTTS(where, content, callback=function(){}){ // def replyTTS
return new Promise((resolve, reject)=>{ replyTTS(where, content, callback = function () { }) {
self.reply(where, content, {tts:true}) return new Promise((resolve, reject) => {
self.reply(where, content, { tts: true })
.then(m => { .then(m => {
callback(null, m); callback(null, m);
resolve(m); resolve(m);
@@ -127,6 +129,27 @@ class Client extends EventEmitter{
}); });
}); });
} }
deleteMessage(msg, options = {}, callback = function (e) { }) {
var self = this;
return new Promise((resolve, reject) => {
if (typeof options === "function") {
// options is the callback
callback = options;
}
self.internal.deleteMessage(msg)
.then(() => {
callback();
resolve();
})
.catch(e => {
callback(e);
reject(e);
});
});
}
} }
module.exports = Client; module.exports = Client;

View File

@@ -163,7 +163,7 @@ class InternalClient {
} }
// def sendMessage // def sendMessage
sendMessage(where, _content, options={}) { sendMessage(where, _content, options = {}) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -180,9 +180,9 @@ class InternalClient {
.post(Endpoints.CHANNEL_MESSAGES(destination.id)) .post(Endpoints.CHANNEL_MESSAGES(destination.id))
.set("authorization", self.token) .set("authorization", self.token)
.send({ .send({
content : content, content: content,
mentions : mentions, mentions: mentions,
tts : options.tts tts: options.tts
}) })
.end((err, res) => { .end((err, res) => {
if (err) { if (err) {
@@ -203,6 +203,41 @@ class InternalClient {
}); });
} }
deleteMessage(_message, options = {}) {
var self = this;
return new Promise((resolve, reject) => {
var message = self.resolver.resolveMessage(_message);
if (message) {
if(options.wait){
setTimeout(deleteMsg, options.wait);
}else{
deleteMsg();
}
function deleteMsg(){
request
.del(Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id))
.set("authorization", self.token)
.end((err, res) => {
if (err) {
reject(new Error(err.response.text));
} else {
message.channel.messages.remove(message);
resolve();
}
});
}
}else {
reject(new Error("Supplied message did not resolve to a message!"));
}
});
}
sendWS(object) { sendWS(object) {
this.websocket.send(JSON.stringify(object)); this.websocket.send(JSON.stringify(object));
} }

View File

@@ -28,6 +28,7 @@ var Endpoints = {
CHANNEL_INVITES: (channelID) => `${Endpoints.CHANNEL(channelID) }/invites`, CHANNEL_INVITES: (channelID) => `${Endpoints.CHANNEL(channelID) }/invites`,
CHANNEL_TYPING: (channelID) => `${Endpoints.CHANNEL(channelID) }/typing`, CHANNEL_TYPING: (channelID) => `${Endpoints.CHANNEL(channelID) }/typing`,
CHANNEL_PERMISSIONS: (channelID) => `${Endpoints.CHANNEL(channelID) }/permissions`, CHANNEL_PERMISSIONS: (channelID) => `${Endpoints.CHANNEL(channelID) }/permissions`,
CHANNEL_MESSAGE: (channelID, messageID) => `${Endpoints.CHANNEL_MESSAGES(channelID)}/${messageID}`
}; };
var Permissions = { var Permissions = {

View File

@@ -46,6 +46,19 @@ class Cache extends Array{
return data; return data;
} }
} }
remove(data){
var index = this.indexOf(data);
if(~index){
this.splice(index, 1);
}else{
var item = this.get("id", data.id);
if(item){
this.splice(this.indexOf(item), 1);
}
}
return false;
}
} }
module.exports = Cache; module.exports = Cache;

View File

@@ -7,7 +7,10 @@ a.on("debug", (m) => console.log("[debug]",m));
a.on("message", m => { a.on("message", m => {
if(m.content === "$$$") if(m.content === "$$$")
a.reply(m, "hi man!").catch(e => console.log(e.stack)); a.reply(m, "hi man!")
.then( m => {
a.deleteMessage(m);
});
}); });
a.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e)); a.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e));