Clean up nearly all promises to utilise chaining, other small fixes

This commit is contained in:
Schuyler Cebulskie
2016-10-30 16:27:28 -04:00
parent 8306d50bd8
commit 60e0d507f0
10 changed files with 482 additions and 620 deletions

View File

@@ -90,7 +90,7 @@ class ClientUser extends User {
/**
* Set the avatar of the logged in Client.
* @param {FileResolvable|Base64Resolveable} avatar The new avatar
* @param {FileResolvable|Base64Resolvable} avatar The new avatar
* @returns {Promise<ClientUser>}
* @example
* // set avatar
@@ -99,15 +99,13 @@ class ClientUser extends User {
* .catch(console.error);
*/
setAvatar(avatar) {
return new Promise(resolve => {
if (avatar.startsWith('data:')) {
resolve(this.client.rest.methods.updateCurrentUser({ avatar }));
} else {
this.client.resolver.resolveFile(avatar).then(data => {
resolve(this.client.rest.methods.updateCurrentUser({ avatar: data }));
});
}
});
if (avatar.startsWith('data:')) {
return this.client.rest.methods.updateCurrentUser({ avatar });
} else {
return this.client.resolver.resolveFile(avatar).then(data =>
this.client.rest.methods.updateCurrentUser({ avatar: data })
);
}
}
/**
@@ -172,16 +170,14 @@ class ClientUser extends User {
* @returns {Promise<Guild>} The guild that was created
*/
createGuild(name, region, icon = null) {
return new Promise(resolve => {
if (!icon) resolve(this.client.rest.methods.createGuild({ name, icon, region }));
if (icon.startsWith('data:')) {
resolve(this.client.rest.methods.createGuild({ name, icon, region }));
} else {
this.client.resolver.resolveFile(icon).then(data => {
resolve(this.client.rest.methods.createGuild({ name, icon: data, region }));
});
}
});
if (!icon) return this.client.rest.methods.createGuild({ name, icon, region });
if (icon.startsWith('data:')) {
return this.client.rest.methods.createGuild({ name, icon, region });
} else {
return this.client.resolver.resolveFile(icon).then(data =>
this.client.rest.methods.createGuild({ name, icon: data, region })
);
}
}
/**

View File

@@ -582,7 +582,7 @@ class Guild {
/**
* Creates a new custom emoji in the guild.
* @param {FileResolveable} attachment The image for the emoji.
* @param {FileResolvable} attachment The image for the emoji.
* @param {string} name The name for the emoji.
* @returns {Promise<Emoji>} The created emoji.
* @example
@@ -597,13 +597,10 @@ class Guild {
* .catch(console.error);
*/
createEmoji(attachment, name) {
return new Promise((resolve, reject) => {
this.client.resolver.resolveFile(attachment).then(file => {
let base64 = new Buffer(file, 'binary').toString('base64');
let dataURI = `data:;base64,${base64}`;
this.client.rest.methods.createEmoji(this, dataURI, name)
.then(resolve).catch(reject);
}).catch(reject);
return this.client.resolver.resolveFile(attachment).then(file => {
let base64 = new Buffer(file, 'binary').toString('base64');
let dataURI = `data:;base64,${base64}`;
return this.client.rest.methods.createEmoji(this, dataURI, name);
});
}

View File

@@ -1,54 +1,6 @@
const Collection = require('../util/Collection');
const Emoji = require('./Emoji');
/**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis
* will use this class opposed to the Emoji class when the client doesn't know enough
* information about them.
*/
class ReactionEmoji {
constructor(reaction, name, id) {
/**
* The message reaction this emoji refers to
* @type {MessageReaction}
*/
this.reaction = reaction;
/**
* The name of this reaction emoji.
* @type {string}
*/
this.name = name;
/**
* The ID of this reaction emoji.
* @type {string}
*/
this.id = id;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) {
return `${this.name}:${this.id}`;
}
return encodeURIComponent(this.name);
}
/**
* Creates the text required to form a graphical emoji on Discord.
* @example
* // send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.sendMessage(`The emoji used is ${reaction.emoji}`);
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
}
}
/**
* Represents a reaction to a message
*/
@@ -59,22 +11,26 @@ class MessageReaction {
* @type {Message}
*/
this.message = message;
/**
* Whether the client has given this reaction
* @type {boolean}
*/
this.me = me;
this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
/**
* The number of people that have given the same reaction.
* @type {number}
*/
this.count = count || 0;
/**
* The users that have given this reaction, mapped by their ID.
* @type {Collection<string, User>}
*/
this.users = new Collection();
this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
}
/**
@@ -83,9 +39,7 @@ class MessageReaction {
* @type {Emoji|ReactionEmoji}
*/
get emoji() {
if (this._emoji instanceof Emoji) {
return this._emoji;
}
if (this._emoji instanceof Emoji) return this._emoji;
// check to see if the emoji has become known to the client
if (this._emoji.id) {
const emojis = this.message.client.emojis;
@@ -106,11 +60,10 @@ class MessageReaction {
remove(user = this.message.client.user) {
const message = this.message;
user = this.message.client.resolver.resolveUserID(user);
if (!user) return Promise.reject('A UserIDResolvable is required (string, user, member, message, guild)');
if (!user) return Promise.reject('Couldn\'t resolve the user ID to remove from the reaction.');
return message.client.rest.methods.removeMessageReaction(
message.channel.id, message.id, this.emoji.identifier, user);
message.channel.id, message.id, this.emoji.identifier, user
);
}
/**
@@ -121,19 +74,66 @@ class MessageReaction {
*/
fetchUsers(limit = 100) {
const message = this.message;
return new Promise((resolve, reject) => {
message.client.rest.methods.getMessageReactionUsers(message.channel.id, message.id, this.emoji.identifier, limit)
.then(users => {
this.users = new Collection();
for (const rawUser of users) {
const user = this.message.client.dataManager.newUser(rawUser);
this.users.set(user.id, user);
}
this.count = this.users.size;
resolve(this.users);
}, reject);
return message.client.rest.methods.getMessageReactionUsers(
message.channel.id, message.id, this.emoji.identifier, limit
).then(users => {
this.users = new Collection();
for (const rawUser of users) {
const user = this.message.client.dataManager.newUser(rawUser);
this.users.set(user.id, user);
}
this.count = this.users.size;
return users;
});
}
}
/**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis
* will use this class opposed to the Emoji class when the client doesn't know enough
* information about them.
*/
class ReactionEmoji {
constructor(reaction, name, id) {
/**
* The message reaction this emoji refers to
* @type {MessageReaction}
*/
this.reaction = reaction;
/**
* The name of this reaction emoji.
* @type {string}
*/
this.name = name;
/**
* The ID of this reaction emoji.
* @type {string}
*/
this.id = id;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) return `${this.name}:${this.id}`;
return encodeURIComponent(this.name);
}
/**
* Creates the text required to form a graphical emoji on Discord.
* @example
* // send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.sendMessage(`The emoji used is ${reaction.emoji}`);
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
}
}
module.exports = MessageReaction;

View File

@@ -61,17 +61,14 @@ class TextChannel extends GuildChannel {
* .catch(console.error)
*/
createWebhook(name, avatar) {
return new Promise((resolve, reject) => {
if (avatar) {
this.client.resolver.resolveFile(avatar).then(file => {
let base64 = new Buffer(file, 'binary').toString('base64');
let dataURI = `data:;base64,${base64}`;
resolve(this.client.rest.methods.createWebhook(this, name, dataURI));
}, reject);
} else {
resolve(this.client.rest.methods.createWebhook(this, name));
}
});
if (avatar) {
return this.client.resolver.resolveFile(avatar).then(file => {
let base64 = new Buffer(file, 'binary').toString('base64');
let dataURI = `data:;base64,${base64}`;
return this.client.rest.methods.createWebhook(this, name, dataURI);
});
}
return this.client.rest.methods.createWebhook(this, name);
}
// These are here only for documentation purposes - they are implemented by TextBasedChannel

View File

@@ -143,14 +143,12 @@ class Webhook {
fileName = 'file.jpg';
}
}
return new Promise((resolve, reject) => {
this.client.resolver.resolveFile(attachment).then(file => {
resolve(this.client.rest.methods.sendWebhookMessage(this, content, options, {
file,
name: fileName,
}));
}, reject);
});
return this.client.resolver.resolveFile(attachment).then(file =>
this.client.rest.methods.sendWebhookMessage(this, content, options, {
file,
name: fileName,
})
);
}
/**
@@ -177,18 +175,15 @@ class Webhook {
* @returns {Promise<Webhook>}
*/
edit(name = this.name, avatar) {
return new Promise((resolve, reject) => {
if (avatar) {
this.client.resolver.resolveFile(avatar).then(file => {
const dataURI = this.client.resolver.resolveBase64(file);
resolve(this.client.rest.methods.editWebhook(this, name, dataURI));
}, reject);
} else {
this.client.rest.methods.editWebhook(this, name).then(data => {
this.setup(data);
resolve(this);
}, reject);
}
if (avatar) {
return this.client.resolver.resolveFile(avatar).then(file => {
const dataURI = this.client.resolver.resolveBase64(file);
return this.client.rest.methods.editWebhook(this, name, dataURI);
});
}
return this.client.rest.methods.editWebhook(this, name).then(data => {
this.setup(data);
return this;
});
}

View File

@@ -92,14 +92,12 @@ class TextBasedChannel {
fileName = 'file.jpg';
}
}
return new Promise((resolve, reject) => {
this.client.resolver.resolveFile(attachment).then(file => {
this.client.rest.methods.sendMessage(this, content, options, {
file,
name: fileName,
}).then(resolve, reject);
}, reject);
});
return this.client.resolver.resolveFile(attachment).then(file =>
this.client.rest.methods.sendMessage(this, content, options, {
file,
name: fileName,
})
);
}
/**
@@ -131,14 +129,10 @@ class TextBasedChannel {
* .catch(console.error);
*/
fetchMessage(messageID) {
return new Promise((resolve, reject) => {
this.client.rest.methods.getChannelMessage(this, messageID).then(data => {
let msg = data;
if (!(msg instanceof Message)) msg = new Message(this, data, this.client);
this._cacheMessage(msg);
resolve(msg);
}, reject);
return this.client.rest.methods.getChannelMessage(this, messageID).then(data => {
const msg = data instanceof Message ? data : new Message(this, data, this.client);
this._cacheMessage(msg);
return msg;
});
}
@@ -163,16 +157,14 @@ class TextBasedChannel {
* .catch(console.error);
*/
fetchMessages(options = {}) {
return new Promise((resolve, reject) => {
this.client.rest.methods.getChannelMessages(this, options).then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
resolve(messages);
}, reject);
return this.client.rest.methods.getChannelMessages(this, options).then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
return messages;
});
}
@@ -181,16 +173,14 @@ class TextBasedChannel {
* @returns {Promise<Collection<string, Message>>}
*/
fetchPinnedMessages() {
return new Promise((resolve, reject) => {
this.client.rest.methods.getChannelPinnedMessages(this).then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
resolve(messages);
}, reject);
return this.client.rest.methods.getChannelPinnedMessages(this).then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
return messages;
});
}
@@ -317,16 +307,12 @@ class TextBasedChannel {
* @returns {Promise<Collection<string, Message>>} Deleted messages
*/
bulkDelete(messages) {
return new Promise((resolve, reject) => {
if (!isNaN(messages)) {
this.fetchMessages({ limit: messages }).then(msgs => resolve(this.bulkDelete(msgs)));
} else if (messages instanceof Array || messages instanceof Collection) {
const messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
resolve(this.client.rest.methods.bulkDeleteMessages(this, messageIDs));
} else {
reject(new TypeError('Messages must be an Array, Collection, or number.'));
}
});
if (!isNaN(messages)) return this.fetchMessages({ limit: messages }).then(msgs => this.bulkDelete(msgs));
if (messages instanceof Array || messages instanceof Collection) {
const messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
return this.client.rest.methods.bulkDeleteMessages(this, messageIDs);
}
throw new TypeError('The messages must be an Array, Collection, or number.');
}
_cacheMessage(message) {