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

@@ -49,7 +49,7 @@ class Client extends EventEmitter{
}) })
} }
// 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) => {
@@ -71,6 +71,7 @@ class Client extends EventEmitter{
}); });
} }
// def sendTTSMessage
sendTTSMessage(where, content, callback = function (e, m) { }) { sendTTSMessage(where, content, callback = function (e, m) { }) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -85,7 +86,7 @@ class Client extends EventEmitter{
}); });
} }
// 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) => {
@@ -115,6 +116,7 @@ class Client extends EventEmitter{
}); });
} }
// def replyTTS
replyTTS(where, content, callback = function () { }) { replyTTS(where, content, callback = function () { }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.reply(where, content, { tts: true }) self.reply(where, content, { tts: true })
@@ -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

@@ -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));