Merge pull request #75 from SimonSchick/resolvererrors

Resolvererrors
This commit is contained in:
abalabahaha
2015-11-28 16:40:25 -08:00
2 changed files with 69 additions and 86 deletions

View File

@@ -309,10 +309,6 @@ export default class InternalClient {
sendMessage(where, _content, options = {}) { sendMessage(where, _content, options = {}) {
return this.resolver.resolveChannel(where) return this.resolver.resolveChannel(where)
.catch(error => {
error.message = "Error resolving destination - " + error.toString();
throw error;
})
.then(destination => { .then(destination => {
//var destination; //var destination;
var content = this.resolver.resolveString(_content); var content = this.resolver.resolveString(_content);
@@ -381,10 +377,6 @@ export default class InternalClient {
// def sendFile // def sendFile
sendFile(where, _file, name = "image.png") { sendFile(where, _file, name = "image.png") {
return this.resolver.resolveChannel(where) return this.resolver.resolveChannel(where)
.catch(error => {
error.message = "Couldn't resolve to channel - " + error.toString();
throw error;
})
.then(channel => .then(channel =>
request request
.post(Endpoints.CHANNEL_MESSAGES(channel.id)) .post(Endpoints.CHANNEL_MESSAGES(channel.id))
@@ -398,10 +390,6 @@ export default class InternalClient {
// def getChannelLogs // def getChannelLogs
getChannelLogs(_channel, limit = 500, options = {}) { getChannelLogs(_channel, limit = 500, options = {}) {
return this.resolver.resolveChannel(_channel) return this.resolver.resolveChannel(_channel)
.catch(error => {
error.message = "Couldn't resolve to channel - " + error.toString();
throw error;
})
.then(channel => { .then(channel => {
var qsObject = {limit}; var qsObject = {limit};
if (options.before) { if (options.before) {
@@ -774,7 +762,8 @@ export default class InternalClient {
//def startTyping //def startTyping
startTyping(channel){ startTyping(channel){
return this.resolver.resolveChannel(channel).then(channel => { return this.resolver.resolveChannel(channel)
.then(channel => {
if(this.typingIntervals[channel.id]){ if(this.typingIntervals[channel.id]){
// typing interval already exists, leave it alone // typing interval already exists, leave it alone
@@ -782,7 +771,8 @@ export default class InternalClient {
} }
this.typingIntervals[channel.id] = setInterval( this.typingIntervals[channel.id] = setInterval(
() => this.sendTyping(channel).catch(error => this.emit("error", error)), () => this.sendTyping(channel)
.catch(error => this.emit("error", error)),
4000 4000
); );

View File

@@ -22,14 +22,13 @@ export default class Resolver {
resolveGameID(resource) { resolveGameID(resource) {
if (!isNaN(resource) && parseInt(resource) % 1 === 0) { if (!isNaN(resource) && parseInt(resource) % 1 === 0) {
return resource; return resource;
} else if (typeof resource == "string" || resource instanceof String) { }
if (typeof resource === "string" || resource instanceof String) {
for (var game of Games) { var gameName = resource.toLowerCase();
if (game.name.toUpperCase() === resource.toUpperCase()) { var found = Games.find(game => game.name.toLowerCase() === gameName);
return game.id; if(found) {
} return found.id;
} }
} }
return null; return null;
@@ -46,15 +45,13 @@ export default class Resolver {
resolveInviteID(resource) { resolveInviteID(resource) {
if (resource instanceof Invite) { if (resource instanceof Invite) {
return resource.id; return resource.id;
} else if (typeof resource == "string" || resource instanceof String) { }
if (typeof resource === "string" || resource instanceof String) {
if (resource.indexOf("http") === 0) { if (resource.indexOf("http") === 0) {
var split = resource.split("/"); var split = resource.split("/");
return split.pop(); return split.pop();
} else {
return resource;
} }
return resource;
} }
return null; return null;
} }
@@ -62,11 +59,14 @@ export default class Resolver {
resolveServer(resource) { resolveServer(resource) {
if (resource instanceof Server) { if (resource instanceof Server) {
return resource; return resource;
} else if (resource instanceof ServerChannel) { }
if (resource instanceof ServerChannel) {
return resource.server; return resource.server;
} else if (resource instanceof String || typeof resource === "string") { }
if (resource instanceof String || typeof resource === "string") {
return this.internal.servers.get("id", resource); return this.internal.servers.get("id", resource);
} else if (resource instanceof Message) { }
if (resource instanceof Message) {
if (resource.channel instanceof TextChannel) { if (resource.channel instanceof TextChannel) {
return resource.server; return resource.server;
} }
@@ -77,9 +77,8 @@ export default class Resolver {
resolveFile(resource) { resolveFile(resource) {
if (typeof resource === "string" || resource instanceof String) { if (typeof resource === "string" || resource instanceof String) {
return fs.createReadStream(resource); return fs.createReadStream(resource);
} else {
return resource;
} }
return resource;
} }
resolveMentions(resource) { resolveMentions(resource) {
@@ -108,38 +107,42 @@ export default class Resolver {
/* /*
accepts a Message, Channel, Server, String ID, User, PMChannel accepts a Message, Channel, Server, String ID, User, PMChannel
*/ */
var found = null;
if (resource instanceof User) { if (resource instanceof User) {
found = resource; return resource;
} else if (resource instanceof Message) { }
found = resource.author; if (resource instanceof Message) {
} else if (resource instanceof TextChannel) { return resource.author;
}
if (resource instanceof TextChannel) {
var lmsg = resource.lastMessage; var lmsg = resource.lastMessage;
if (lmsg) { if (lmsg) {
found = lmsg.author; return lmsg.author;
} }
} else if (resource instanceof Server) { }
found = resource.owner; if (resource instanceof Server) {
} else if (resource instanceof PMChannel) { return resource.owner;
found = resource.recipient; }
} else if (resource instanceof String || typeof resource === "string") { if (resource instanceof PMChannel) {
found = this.client.internal.users.get("id", resource); return resource.recipient;
}
if (resource instanceof String || typeof resource === "string") {
return this.client.internal.users.get("id", resource);
} }
return found; return null;
} }
resolveMessage(resource) { resolveMessage(resource) {
// accepts a Message, PMChannel & TextChannel // accepts a Message, PMChannel & TextChannel
var found = null;
if (resource instanceof TextChannel || resource instanceof PMChannel) { if (resource instanceof TextChannel || resource instanceof PMChannel) {
found = resource.lastMessage; return resource.lastMessage;
} else if (resource instanceof Message) { }
found = resource; if (resource instanceof Message) {
return resource;
} }
return found; return null;
} }
resolveVoiceChannel(resource) { resolveVoiceChannel(resource) {
@@ -153,44 +156,34 @@ export default class Resolver {
resolveChannel(resource) { resolveChannel(resource) {
/* /*
accepts a Message, Channel, Server, String ID, User accepts a Message, Channel, Server, String ID, User
*/ */
var self = this;
return new Promise((resolve, reject) => { if (resource instanceof Message) {
var found = null; return Promise.resolve(resource.channel);
if (resource instanceof Message) { }
found = resource.channel; if (resource instanceof Channel) {
} else if (resource instanceof Channel) { return Promise.resolve(resource);
found = resource; }
} else if (resource instanceof Server) { if (resource instanceof Server) {
found = resource.channels.get("id", resource.id); return Promise.resolve(resource.channels.get("id", resource.id));
} else if (resource instanceof String || typeof resource === "string") { }
found = self.internal.channels.get("id", resource); if (resource instanceof String || typeof resource === "string") {
} else if (resource instanceof User) { return Promise.resolve(this.internal.channels.get("id", resource));
// see if a PM exists }
var chatFound = false; if (resource instanceof User) {
for (var pmchat of self.internal.private_channels) { // see if a PM exists
if (pmchat.recipient.equals(resource)) { var chatFound = this.internal.private_channels.find(
chatFound = pmchat; pmchat => pmchat.recipient.equals(resource)
break; );
} if (chatFound) {
} // a PM already exists!
if (chatFound) { return Promise.resolve(chatFound);
// a PM already exists!
found = chatFound;
} else {
// PM does not exist :\
self.internal.startPM(resource)
.then(pmchannel => resolve(pmchannel))
.catch(e => reject(e));
return;
}
} }
if (found) // PM does not exist :\
resolve(found); return this.internal.startPM(resource);
else }
reject(new Error("Didn't found anything")); var error = new Error("Could not resolve channel");
}); error.resource = resource;
return Promise.reject(error);
} }
} }