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) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
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) {
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) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
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) {
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;
})(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) {
this.websocket.send(JSON.stringify(object));
};

View File

@@ -63,6 +63,9 @@ var Endpoints = {
},
CHANNEL_PERMISSIONS: function CHANNEL_PERMISSIONS(channelID) {
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;
})(Array);

View File

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