Reorganised a bunch of methods

This commit is contained in:
Schuyler Cebulskie
2016-09-11 23:55:50 -04:00
parent ce6cb626dc
commit 95de09f389
7 changed files with 169 additions and 175 deletions

File diff suppressed because one or more lines are too long

View File

@@ -23,6 +23,10 @@ class ClientUser extends User {
this._typing = new Map();
}
edit(data) {
return this.client.rest.methods.updateCurrentUser(data);
}
/**
* Set the username of the logged in Client.
* <info>Changing usernames in Discord is heavily rate limited, with only 2 requests
@@ -96,7 +100,7 @@ class ClientUser extends User {
* .then(user => console.log('Changed status!'))
* .catch(console.log);
*/
setStatus(status, game, url = null) {
setStatus(status, game = null, url = null) {
return new Promise(resolve => {
if (status === 'online' || status === 'here' || status === 'available') {
this.idleStatus = null;
@@ -136,10 +140,6 @@ class ClientUser extends User {
resolve(this);
});
}
edit(data) {
return this.client.rest.methods.updateCurrentUser(data);
}
}
module.exports = ClientUser;

View File

@@ -45,7 +45,7 @@ class EvaluatedPermissions {
/**
* Checks whether a user has multiple permissions in a channel.
* @param {Array<PermissionResolvable>} permissions the permissions to test for
* @param {PermissionResolvable[]} permissions the permissions to test for
* @param {boolean} [explicit=false] whether to require the user to explicitly have the exact permissions
* @returns {boolean}
*/

View File

@@ -235,6 +235,18 @@ class Guild {
return this.channels.get(this.id);
}
/**
* Returns the GuildMember form of a User object, if the User is present in the guild.
* @param {UserResolvable} user The user that you want to obtain the GuildMember of
* @returns {?GuildMember}
* @example
* // get the guild member of a user
* const member = guild.member(message.author);
*/
member(user) {
return this.client.resolver.resolveGuildMember(this, user);
}
/**
* Updates the Guild with new information - e.g. a new name.
* @param {GuildEditData} data The data to update the guild with
@@ -364,18 +376,6 @@ class Guild {
return this.edit({ splash });
}
/**
* Returns the GuildMember form of a User object, if the User is present in the guild.
* @param {UserResolvable} user The user that you want to obtain the GuildMember of
* @returns {?GuildMember}
* @example
* // get the guild member of a user
* const member = guild.member(message.author);
*/
member(user) {
return this.client.resolver.resolveGuildMember(this, user);
}
/**
* Bans a user from the guild.
* @param {UserResolvable} user The user to ban

View File

@@ -191,7 +191,7 @@ class GuildMember {
/**
* Checks whether the roles of the members allow them to perform specific actions.
* @param {Array<PermissionResolvable>} permissions the permissions to test for
* @param {PermissionResolvable[]} permissions the permissions to test for
* @param {boolean} [explicit=false] whether to require the member to explicitly have the exact permissions
* @returns {boolean}
*/
@@ -199,6 +199,15 @@ class GuildMember {
return permissions.map(p => this.hasPermission(p, explicit)).every(v => v);
}
/**
* Edit a Guild Member
* @param {GuildmemberEditData} data The data to edit the member with
* @returns {Promise<GuildMember>}
*/
edit(data) {
return this.client.rest.methods.updateGuildMember(this, data);
}
/**
* Mute/unmute a user
* @param {boolean} mute Whether or not the member should be muted
@@ -299,15 +308,6 @@ class GuildMember {
return this.edit({ nick });
}
/**
* Edit a Guild Member
* @param {GuildmemberEditData} data The data to edit the member with
* @returns {Promise<GuildMember>}
*/
edit(data) {
return this.client.rest.methods.updateGuildMember(this, data);
}
/**
* Deletes any DMs with this Guild Member
* @returns {Promise<DMChannel>}

View File

@@ -138,6 +138,57 @@ class Message {
this._edits = [];
}
patch(data) { // eslint-disable-line complexity
if (data.author) {
this.author = this.client.users.get(data.author.id);
if (this.guild) this.member = this.guild.member(this.author);
}
if (data.content) this.content = data.content;
if (data.timestamp) this._timestamp = new Date(data.timestamp).getTime();
if (data.edited_timestamp) {
this._editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
}
if ('tts' in data) this.tts = data.tts;
if ('mention_everyone' in data) this.mentions.everyone = data.mention_everyone;
if (data.nonce) this.nonce = data.nonce;
if (data.embeds) this.embeds = data.embeds.map(e => new Embed(this, e));
if (data.type > -1) {
this.system = false;
if (data.type === 6) this.system = true;
}
if (data.attachments) {
this.attachments = new Collection();
for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new Attachment(this, attachment));
}
}
if (data.mentions) {
for (const mention of data.mentions) {
let user = this.client.users.get(mention.id);
if (user) {
this.mentions.users.set(user.id, user);
} else {
user = this.client.dataManager.newUser(mention);
this.mentions.users.set(user.id, user);
}
}
}
if (data.mention_roles) {
for (const mention of data.mention_roles) {
const role = this.channel.guild.roles.get(mention);
if (role) this.mentions.roles.set(role.id, role);
}
}
if (data.id) this.id = data.id;
if (this.channel.guild && data.content) {
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]);
if (chan) this.mentions.channels.set(chan.id, chan);
}
}
}
/**
* When the message was sent
* @type {Date}
@@ -195,61 +246,45 @@ class Message {
return this._edits.slice().unshift(this);
}
patch(data) { // eslint-disable-line complexity
if (data.author) {
this.author = this.client.users.get(data.author.id);
if (this.guild) this.member = this.guild.member(this.author);
}
if (data.content) this.content = data.content;
if (data.timestamp) this._timestamp = new Date(data.timestamp).getTime();
if (data.edited_timestamp) {
this._editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
}
if ('tts' in data) this.tts = data.tts;
if ('mention_everyone' in data) this.mentions.everyone = data.mention_everyone;
if (data.nonce) this.nonce = data.nonce;
if (data.embeds) this.embeds = data.embeds.map(e => new Embed(this, e));
if (data.type > -1) {
this.system = false;
if (data.type === 6) {
this.system = true;
}
}
if (data.attachments) {
this.attachments = new Collection();
for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new Attachment(this, attachment));
}
}
if (data.mentions) {
for (const mention of data.mentions) {
let user = this.client.users.get(mention.id);
if (user) {
this.mentions.users.set(user.id, user);
} else {
user = this.client.dataManager.newUser(mention);
this.mentions.users.set(user.id, user);
}
}
}
if (data.mention_roles) {
for (const mention of data.mention_roles) {
const role = this.channel.guild.roles.get(mention);
if (role) {
this.mentions.roles.set(role.id, role);
}
}
}
if (data.id) this.id = data.id;
if (this.channel.guild && data.content) {
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]);
if (chan) {
this.mentions.channels.set(chan.id, chan);
}
}
}
/**
* Whether or not a user, channel or role is mentioned in this message.
* @param {GuildChannel|User|Role|string} data either a guild channel, user or a role object, or a string representing
* the ID of any of these.
* @returns {boolean}
*/
isMentioned(data) {
data = data.id ? data.id : data;
return this.mentions.users.has(data) || this.mentions.channels.has(data) || this.mentions.roles.has(data);
}
/**
* Edit the content of a message
* @param {StringResolvable} content The new content for the message
* @returns {Promise<Message>}
* @example
* // update the content of a message
* message.edit('This is my new content!')
* .then(msg => console.log(`Updated the content of a message from ${msg.author}`))
* .catch(console.log);
*/
edit(content) {
return this.client.rest.methods.updateMessage(this, content);
}
/**
* Pins this message to the channel's pinned messages
* @returns {Promise<Message>}
*/
pin() {
return this.client.rest.methods.pinMessage(this);
}
/**
* Unpins this message from the channel's pinned messages
* @returns {Promise<Message>}
*/
unpin() {
return this.client.rest.methods.unpinMessage(this);
}
/**
@@ -273,21 +308,7 @@ class Message {
}
/**
* Edit the content of a message
* @param {StringResolvable} content The new content for the message
* @returns {Promise<Message>}
* @example
* // update the content of a message
* message.edit('This is my new content!')
* .then(msg => console.log(`Updated the content of a message from ${msg.author}`))
* .catch(console.log);
*/
edit(content) {
return this.client.rest.methods.updateMessage(this, content);
}
/**
* Reply to a message
* Reply to the message
* @param {StringResolvable} content The content for the message
* @param {MessageOptions} [options = {}] The options to provide
* @returns {Promise<Message|Message[]>}
@@ -310,33 +331,6 @@ class Message {
return this.client.rest.methods.sendMessage(this.channel, content, options);
}
/**
* Whether or not a user, channel or role is mentioned in this message.
* @param {GuildChannel|User|Role|string} data either a guild channel, user or a role object, or a string representing
* the ID of any of these.
* @returns {boolean}
*/
isMentioned(data) {
data = data.id ? data.id : data;
return this.mentions.users.has(data) || this.mentions.channels.has(data) || this.mentions.roles.has(data);
}
/**
* Pins this message to the channel's pinned messages
* @returns {Promise<Message>}
*/
pin() {
return this.client.rest.methods.pinMessage(this);
}
/**
* Unpins this message from the channel's pinned messages
* @returns {Promise<Message>}
*/
unpin() {
return this.client.rest.methods.unpinMessage(this);
}
/**
* Used mainly internally. Whether two messages are identical in properties. If you want to compare messages
* without checking all the properties, use `message.id === message2.id`, which is much more efficient. This

View File

@@ -75,16 +75,48 @@ class Role {
}
/**
* Deletes the role
* @returns {Promise<Role>}
* @example
* // delete a role
* role.delete()
* .then(r => console.log(`Deleted role ${r}`))
* .catch(console.log);
* The hexadecimal version of the role color, with a leading hashtag.
* @type {string}
* @readonly
*/
delete() {
return this.client.rest.methods.deleteGuildRole(this);
get hexColor() {
let col = this.color.toString(16);
while (col.length < 6) col = `0${col}`;
return `#${col}`;
}
/**
* Get an object mapping permission names to whether or not the role enables that permission
* @returns {Object<string, boolean>}
* @example
* // print the serialized role
* console.log(role.serialize());
*/
serialize() {
const serializedPermissions = {};
for (const permissionName in Constants.PermissionFlags) {
serializedPermissions[permissionName] = this.hasPermission(permissionName);
}
return serializedPermissions;
}
/**
* Whether or not the role includes the given permission
* @param {PermissionResolvable} permission The name of the permission to test
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
* @returns {boolean}
* @example
* // see if a role can ban a member
* if (role.hasPermission('BAN_MEMBERS')) {
* console.log('This role can ban members');
* } else {
* console.log('This role can\'t ban members');
* }
*/
hasPermission(permission, explicit = false) {
permission = this.client.resolver.resolvePermission(permission);
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (this.permissions & permission) > 0;
}
/**
@@ -172,48 +204,16 @@ class Role {
}
/**
* Get an object mapping permission names to whether or not the role enables that permission
* @returns {Object<string, boolean>}
* Deletes the role
* @returns {Promise<Role>}
* @example
* // print the serialized role
* console.log(role.serialize());
* // delete a role
* role.delete()
* .then(r => console.log(`Deleted role ${r}`))
* .catch(console.log);
*/
serialize() {
const serializedPermissions = {};
for (const permissionName in Constants.PermissionFlags) {
serializedPermissions[permissionName] = this.hasPermission(permissionName);
}
return serializedPermissions;
}
/**
* Whether or not the role includes the given permission
* @param {PermissionResolvable} permission The name of the permission to test
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
* @returns {boolean}
* @example
* // see if a role can ban a member
* if (role.hasPermission('BAN_MEMBERS')) {
* console.log('This role can ban members');
* } else {
* console.log('This role can\'t ban members');
* }
*/
hasPermission(permission, explicit = false) {
permission = this.client.resolver.resolvePermission(permission);
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (this.permissions & permission) > 0;
}
/**
* The hexadecimal version of the role color, with a leading hashtag.
* @type {string}
* @readonly
*/
get hexColor() {
let col = this.color.toString(16);
while (col.length < 6) col = `0${col}`;
return `#${col}`;
delete() {
return this.client.rest.methods.deleteGuildRole(this);
}
/**