Added get channel logs

This commit is contained in:
hydrabolt
2015-10-31 23:58:47 +00:00
parent 3c16a9f2a4
commit 53ef5df10d
6 changed files with 154 additions and 6 deletions

View File

@@ -194,6 +194,29 @@ var Client = (function (_EventEmitter) {
});
};
// def getChannelLogs
Client.prototype.getChannelLogs = function getChannelLogs(where) {
var limit = arguments.length <= 1 || arguments[1] === undefined ? 500 : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (err, logs) {} : arguments[3];
var self = this;
return new Promise(function (resolve, reject) {
if (typeof options === "function") {
// options is the callback
callback = options;
}
self.internal.getChannelLogs(where, limit, options).then(function (logs) {
callback(null, logs);
resolve(logs);
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
return Client;
})(EventEmitter);

View File

@@ -251,6 +251,44 @@ var InternalClient = (function () {
});
};
InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) {
var limit = arguments.length <= 1 || arguments[1] === undefined ? 500 : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var self = this;
return new Promise(function (resolve, reject) {
self.resolver.resolveChannel(_channel).then(next)["catch"](function (e) {
return reject(new Error("couldn't resolve to channel - " + e));
});
function next(channel) {
if (options.before) options.before = self.resolver.resolveMessage(options.before);
if (options.after) options.after = self.resolver.resolveMessage(options.after);
var params = [];
if (options.before) params.push("before=" + options.before.id);
if (options.after) params.push("after=" + options.after.id);
var joinedParams = params.join();
if (joinedParams !== "") joinedParams = "&" + params.join();
request.get(Endpoints.CHANNEL_MESSAGES(channel.id) + "?limit=" + limit + joinedParams).set("authorization", self.token).end(function (err, res) {
if (err) {
reject(new Error(err.response.text));
} else {
var logs = [];
res.body.forEach(function (msg) {
logs.push(channel.messages.add(new Message(msg, channel, self.client)));
});
resolve(logs);
}
});
}
});
};
InternalClient.prototype.sendWS = function sendWS(object) {
this.websocket.send(JSON.stringify(object));
};

View File

@@ -10,8 +10,23 @@ a.on("debug", function (m) {
});
a.on("message", function (m) {
if (m.content === "$$$") a.sendMessage(m.author, "hi!")["catch"](function (e) {
return console.log(e);
if (m.content === "$$$") a.getChannelLogs(m).then(function (logs) {
for (var _iterator = logs, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var item = _ref;
console.log(item.author.username + "> " + item.content);
}
});
});

View File

@@ -171,6 +171,29 @@ class Client extends EventEmitter {
});
}
// def getChannelLogs
getChannelLogs(where, limit=500, options={}, callback=function(err, logs){}){
var self = this;
return new Promise((resolve, reject) => {
if (typeof options === "function") {
// options is the callback
callback = options;
}
self.internal.getChannelLogs(where, limit, options)
.then( logs => {
callback(null, logs);
resolve(logs);
})
.catch( e => {
callback(e);
reject(e);
});
});
}
}
module.exports = Client;

View File

@@ -172,7 +172,7 @@ class InternalClient {
self.resolver.resolveChannel(where)
.then(next)
.catch(e => reject(new Error("Error resolving destination - "+e)));
.catch(e => reject(new Error("Error resolving destination - " + e)));
function next(destination) {
//var destination;
@@ -268,8 +268,8 @@ class InternalClient {
} else {
resolve(
message.channel.messages.update
(message, new Message(res.body, message.channel, self.client)
));
(message, new Message(res.body, message.channel, self.client)
));
}
})
@@ -281,6 +281,51 @@ class InternalClient {
}
getChannelLogs(_channel, limit = 500, options = {}) {
var self = this;
return new Promise((resolve, reject) => {
self.resolver.resolveChannel(_channel)
.then(next)
.catch(e => reject(new Error("couldn't resolve to channel - " + e)));
function next(channel) {
if (options.before)
options.before = self.resolver.resolveMessage(options.before);
if (options.after)
options.after = self.resolver.resolveMessage(options.after);
var params = [];
if (options.before)
params.push("before=" + options.before.id);
if (options.after)
params.push("after=" + options.after.id);
var joinedParams = params.join();
if (joinedParams !== "")
joinedParams = "&" + params.join();
request
.get(`${Endpoints.CHANNEL_MESSAGES(channel.id)}?limit=${limit}${joinedParams}`)
.set("authorization", self.token)
.end((err, res) => {
if(err){
reject(new Error(err.response.text));
}else{
var logs = [];
res.body.forEach((msg) => {
logs.push( channel.messages.add(new Message(msg, channel, self.client)) );
});
resolve(logs);
}
});
}
});
}
sendWS(object) {
this.websocket.send(JSON.stringify(object));
}

View File

@@ -7,7 +7,11 @@ a.on("debug", (m) => console.log("[debug]",m));
a.on("message", m => {
if(m.content === "$$$")
a.sendMessage(m.author, "hi!").catch(e => console.log(e));
a.getChannelLogs(m).then( logs => {
for(var item of logs){
console.log(item.author.username + "> " + item.content);
}
});
});
a.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e));