diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 6ea55b92d..b3b3ad1bf 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -362,6 +362,25 @@ class Guild { }); } + /** + * Performs a search within the entire guild. + * @param {MessageSearchOptions} [options={}] Options to pass to the search + * @returns {Promise>} + * An array containing arrays of messages. Each inner array is a search context cluster. + * The message which has triggered the result will have the `hit` property set to `true`. + * @example + * guild.search({ + * content: 'discord.js', + * before: '2016-11-17' + * }).then(res => { + * const hit = res[0].find(m => m.hit).content; + * console.log(`I found: **${hit}**`); + * }).catch(console.error); + */ + search(options) { + return this.client.rest.methods.search(this, options); + } + /** * The data for editing a guild * @typedef {Object} GuildEditData @@ -600,6 +619,45 @@ class Guild { return create.then(role => role.edit(data)); } + /** + * Set the position of a role in this guild + * @param {string|Role} role the role to edit, can be a role object or a role ID. + * @param {number} position the new position of the role + * @returns {Promise} + */ + setRolePosition(role, position) { + if (typeof role === 'string') { + role = this.roles.get(role); + if (!role) return Promise.reject(new Error('Supplied role is not a role or string.')); + } + + position = Number(position); + if (isNaN(position)) return Promise.reject(new Error('Supplied position is not a number.')); + + const lowestAffected = Math.min(role.position, position); + const highestAffected = Math.max(role.position, position); + + const rolesToUpdate = this.roles.filter(r => r.position >= lowestAffected && r.position <= highestAffected); + + // stop role positions getting stupidly inflated + if (position > role.position) { + position = rolesToUpdate.first().position; + } else { + position = rolesToUpdate.last().position; + } + + const updatedRoles = []; + + for (const uRole of rolesToUpdate.values()) { + updatedRoles.push({ + id: uRole.id, + position: uRole.id === role.id ? position : uRole.position + (position < role.position ? 1 : -1), + }); + } + + return this.client.rest.methods.setRolePositions(this.id, updatedRoles); + } + /** * Creates a new custom emoji in the guild. * @param {BufferResolvable} attachment The image for the emoji. @@ -664,64 +722,6 @@ class Guild { return this.client.rest.methods.deleteGuild(this); } - /** - * Set the position of a role in this guild - * @param {string|Role} role the role to edit, can be a role object or a role ID. - * @param {number} position the new position of the role - * @returns {Promise} - */ - setRolePosition(role, position) { - if (typeof role === 'string') { - role = this.roles.get(role); - if (!role) return Promise.reject(new Error('Supplied role is not a role or string.')); - } - - position = Number(position); - if (isNaN(position)) return Promise.reject(new Error('Supplied position is not a number.')); - - const lowestAffected = Math.min(role.position, position); - const highestAffected = Math.max(role.position, position); - - const rolesToUpdate = this.roles.filter(r => r.position >= lowestAffected && r.position <= highestAffected); - - // stop role positions getting stupidly inflated - if (position > role.position) { - position = rolesToUpdate.first().position; - } else { - position = rolesToUpdate.last().position; - } - - const updatedRoles = []; - - for (const uRole of rolesToUpdate.values()) { - updatedRoles.push({ - id: uRole.id, - position: uRole.id === role.id ? position : uRole.position + (position < role.position ? 1 : -1), - }); - } - - return this.client.rest.methods.setRolePositions(this.id, updatedRoles); - } - - /** - * Performs a search - * @param {MessageSearchOptions} [options={}] Options to pass to the search - * @returns {Promise>} - * An array containing arrays of messages. Each inner array is a search context cluster. - * The message which has triggered the result will have the `hit` property set to `true`. - * @example - * guild.search({ - * content: 'discord.js', - * before: '2016-11-17' - * }).then(res => { - * const hit = res[0].find(m => m.hit).content; - * console.log(`I found: **${hit}**`); - * }).catch(console.error); - */ - search(options) { - return this.client.rest.methods.search(this, options); - } - /** * Whether this Guild equals another Guild. It compares all properties, so for most operations * it is advisable to just compare `guild.id === guild2.id` as it is much faster and is often diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js index 47bc88409..dd763d7f0 100644 --- a/src/structures/interface/TextBasedChannel.js +++ b/src/structures/interface/TextBasedChannel.js @@ -214,7 +214,7 @@ class TextBasedChannel { } /** - * Performs a search + * Performs a search within the channel. * @param {MessageSearchOptions} [options={}] Options to pass to the search * @returns {Promise>} * An array containing arrays of messages. Each inner array is a search context cluster.