Merge branch 'master' into indev-prism

This commit is contained in:
Schuyler Cebulskie
2017-02-01 15:29:45 -05:00
28 changed files with 275 additions and 109 deletions

View File

@@ -101,6 +101,27 @@ class Emoji {
return encodeURIComponent(this.name);
}
/**
* Data for editing an emoji
* @typedef {Object} EmojiEditData
* @property {string} [name] The name of the emoji
* @property {Collection<string, Role>|Array<string|Role>} [roles] Roles to restrict emoji to
*/
/**
* Edits the emoji
* @param {EmojiEditData} data The new data for the emoji
* @returns {Promise<Emoji>}
* @example
* // edit a emoji
* emoji.edit({name: 'newemoji'})
* .then(e => console.log(`Edited emoji ${e}`))
* .catch(console.error);
*/
edit(data) {
return this.client.rest.methods.updateEmoji(this, data);
}
/**
* When concatenated with a string, this automatically returns the emoji mention rather than the object.
* @returns {string}

View File

@@ -337,7 +337,6 @@ class Guild {
* @returns {Promise<GuildMember>}
*/
fetchMember(user, cache = true) {
if (this._fetchWaiter) return Promise.reject(new Error('Already fetching guild members.'));
user = this.client.resolver.resolveUser(user);
if (!user) return Promise.reject(new Error('User is not cached. Use Client.fetchUser first.'));
if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id));
@@ -347,26 +346,37 @@ class Guild {
/**
* Fetches all the members in the guild, even if they are offline. If the guild has less than 250 members,
* this should not be necessary.
* @param {string} [query=''] An optional query to provide when fetching members
* @param {string} [query=''] Limit fetch to members with similar usernames
* @param {number} [limit=0] Maximum number of members to request
* @returns {Promise<Guild>}
*/
fetchMembers(query = '') {
fetchMembers(query = '', limit = 0) {
return new Promise((resolve, reject) => {
if (this._fetchWaiter) throw new Error(`Already fetching guild members in ${this.id}.`);
if (this.memberCount === this.members.size) {
// uncomment in v12
// resolve(this.members)
resolve(this);
return;
}
this._fetchWaiter = resolve;
this.client.ws.send({
op: Constants.OPCodes.REQUEST_GUILD_MEMBERS,
d: {
guild_id: this.id,
query,
limit: 0,
limit,
},
});
this._checkChunks();
const handler = (members, guild) => {
if (guild.id !== this.id) return;
if (this.memberCount === this.members.size || members.length < 1000) {
this.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
// uncomment in v12
// resolve(this.members)
resolve(this);
return;
}
};
this.client.on(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
this.client.setTimeout(() => reject(new Error('Members didn\'t arrive in time.')), 120 * 1000);
});
}
@@ -608,7 +618,7 @@ class Guild {
}
/**
* Creates a new role in the guild, and optionally updates it with the given information.
* Creates a new role in the guild with given information
* @param {RoleData} [data] The data to update the role with
* @returns {Promise<Role>}
* @example
@@ -618,14 +628,15 @@ class Guild {
* .catch(console.error);
* @example
* // create a new role with data
* guild.createRole({ name: 'Super Cool People' })
* .then(role => console.log(`Created role ${role}`))
* .catch(console.error)
* guild.createRole({
* name: 'Super Cool People',
* color: 'BLUE',
* })
* .then(role => console.log(`Created role ${role}`))
* .catch(console.error)
*/
createRole(data) {
const create = this.client.rest.methods.createGuildRole(this);
if (!data) return create;
return create.then(role => role.edit(data));
return this.client.rest.methods.createGuildRole(this, data);
}
/**
@@ -671,6 +682,7 @@ class Guild {
* Creates a new custom emoji in the guild.
* @param {BufferResolvable|Base64Resolvable} attachment The image for the emoji.
* @param {string} name The name for the emoji.
* @param {Collection<Role>|Role[]} [roles] Roles to limit the emoji to
* @returns {Promise<Emoji>} The created emoji.
* @example
* // create a new emoji from a url
@@ -683,13 +695,13 @@ class Guild {
* .then(emoji => console.log(`Created new emoji with name ${emoji.name}!`))
* .catch(console.error);
*/
createEmoji(attachment, name) {
createEmoji(attachment, name, roles) {
return new Promise(resolve => {
if (typeof attachment === 'string' && attachment.startsWith('data:')) {
resolve(this.client.rest.methods.createEmoji(this, attachment, name));
resolve(this.client.rest.methods.createEmoji(this, attachment, name, roles));
} else {
this.client.resolver.resolveBuffer(attachment).then(data =>
resolve(this.client.rest.methods.createEmoji(this, data, name))
resolve(this.client.rest.methods.createEmoji(this, data, name, roles))
);
}
});
@@ -811,7 +823,6 @@ class Guild {
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, member);
}
this._checkChunks();
return member;
}
@@ -841,7 +852,6 @@ class Guild {
_removeMember(guildMember) {
this.members.delete(guildMember.id);
this._checkChunks();
}
_memberSpeakUpdate(user, speaking) {
@@ -865,15 +875,6 @@ class Guild {
}
this.presences.set(id, new Presence(presence));
}
_checkChunks() {
if (this._fetchWaiter) {
if (this.members.size === this.memberCount) {
this._fetchWaiter(this);
this._fetchWaiter = null;
}
}
}
}
module.exports = Guild;

View File

@@ -281,6 +281,16 @@ class GuildChannel extends Channel {
return equal;
}
/**
* Whether the channel is deletable by the client user.
* @type {boolean}
* @readonly
*/
get deletable() {
return this.id !== this.guild.id &&
this.permissionsFor(this.client.user).hasPermission(Constants.PermissionFlags.MANAGE_CHANNELS);
}
/**
* When concatenated with a string, this automatically returns the channel's mention instead of the Channel object.
* @returns {string}

View File

@@ -39,6 +39,12 @@ class GuildMember {
* @type {?Snowflake}
*/
this.lastMessageID = null;
/**
* The Message object of the last message sent by the member in their guild, if one was sent.
* @type {?Message}
*/
this.lastMessage = null;
}
setup(data) {
@@ -389,12 +395,20 @@ class GuildMember {
return this.edit({ nick });
}
/**
* Creates a DM channel between the client and the member
* @returns {Promise<DMChannel>}
*/
createDM() {
return this.user.createDM();
}
/**
* Deletes any DMs with this guild member
* @returns {Promise<DMChannel>}
*/
deleteDM() {
return this.client.rest.methods.deleteChannel(this);
return this.user.deleteDM();
}
/**

View File

@@ -197,12 +197,13 @@ class Message {
if (data.type === 6) this.system = true;
}
if (data.attachments) {
this.attachments = new Collection();
this.attachments.clear();
for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new Attachment(this, attachment));
}
}
if (data.mentions) {
this.mentions.users.clear();
for (const mention of data.mentions) {
let user = this.client.users.get(mention.id);
if (user) {
@@ -214,6 +215,7 @@ class Message {
}
}
if (data.mention_roles) {
this.mentions.roles.clear();
for (const mention of data.mention_roles) {
const role = this.channel.guild.roles.get(mention);
if (role) this.mentions.roles.set(role.id, role);
@@ -221,6 +223,7 @@ class Message {
}
if (data.id) this.id = data.id;
if (this.channel.guild && data.content) {
this.mentions.channels.clear();
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
for (const raw of channMentionsRaw) {
const chan = this.channel.guild.channels.get(raw.match(/([0-9]{14,20})/g)[0]);
@@ -228,11 +231,11 @@ class Message {
}
}
if (data.reactions) {
this.reactions = new Collection();
this.reactions.clear();
if (data.reactions.length > 0) {
for (const reaction of data.reactions) {
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
this.reactions.set(id, new MessageReaction(this, data.emoji, data.count, data.me));
this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me));
}
}
}

View File

@@ -55,6 +55,12 @@ class User {
* @type {?Snowflake}
*/
this.lastMessageID = null;
/**
* The Message object of the last message sent by the user, if one was sent.
* @type {?Message}
*/
this.lastMessage = null;
}
patch(data) {
@@ -173,6 +179,14 @@ class User {
return this.client.channels.filter(c => c.type === 'dm').find(c => c.recipient.id === this.id);
}
/**
* Creates a DM channel between the client and the user
* @returns {Promise<DMChannel>}
*/
createDM() {
return this.client.rest.methods.createDM(this);
}
/**
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
* @returns {Promise<DMChannel>}

View File

@@ -42,6 +42,12 @@ class UserProfile {
*/
this.premium = data.premium;
/**
* The date since which the user has had Discord Premium
* @type {?Date}
*/
this.premiumSince = data.premium_since ? new Date(data.premium_since) : null;
for (const guild of data.mutual_guilds) {
if (this.client.guilds.has(guild.id)) {
this.mutualGuilds.set(guild.id, this.client.guilds.get(guild.id));

View File

@@ -20,6 +20,12 @@ class TextBasedChannel {
* @type {?Snowflake}
*/
this.lastMessageID = null;
/**
* The Message object of the last message in the channel, if one was sent.
* @type {?Message}
*/
this.lastMessage = null;
}
/**