From 7f8cc9c297ff2cb6954ffa5faf4e49fa42215602 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 24 Jun 2017 17:23:45 -0500 Subject: [PATCH] sanity changes to search (#1593) * Create Search.js * Create Guild.js * Create TextBasedChannel.js * Create Search.js * Create Search.js * Create Guild.js * Create TextBasedChannel.js * Create Search.js --- src/structures/Guild.js | 6 +++--- src/structures/interfaces/TextBasedChannel.js | 6 +++--- src/structures/shared/Search.js | 21 +++++++++++++------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index e3c6a18d3..3b6f0d9ef 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -538,7 +538,7 @@ class Guild { * Performs a search within the entire guild. * This is only available when using a user account. * @param {MessageSearchOptions} [options={}] Options to pass to the search - * @returns {Promise>} + * @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 @@ -546,8 +546,8 @@ class Guild { * content: 'discord.js', * before: '2016-11-17' * }).then(res => { - * const hit = res.messages[0].find(m => m.hit).content; - * console.log(`I found: **${hit}**, total results: ${res.totalResults}`); + * const hit = res.results[0].find(m => m.hit).content; + * console.log(`I found: **${hit}**, total results: ${res.total}`); * }).catch(console.error); */ search(options = {}) { diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index d0239390b..5d4957ded 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -203,7 +203,7 @@ class TextBasedChannel { * Performs a search within the channel. * This is only available when using a user account. * @param {MessageSearchOptions} [options={}] Options to pass to the search - * @returns {Promise>} + * @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 @@ -211,8 +211,8 @@ class TextBasedChannel { * content: 'discord.js', * before: '2016-11-17' * }).then(res => { - * const hit = res.messages[0].find(m => m.hit).content; - * console.log(`I found: **${hit}**, total results: ${res.totalResults}`); + * const hit = res.results[0].find(m => m.hit).content; + * console.log(`I found: **${hit}**, total results: ${res.total}`); * }).catch(console.error); */ search(options = {}) { diff --git a/src/structures/shared/Search.js b/src/structures/shared/Search.js index ce2281d7a..63606f5c9 100644 --- a/src/structures/shared/Search.js +++ b/src/structures/shared/Search.js @@ -11,7 +11,7 @@ const long = require('long'); * @property {UserResolvable} [author] Author to limit search * @property {string} [authorType] One of `user`, `bot`, `webhook`, or add `-` to negate (e.g. `-webhook`) * @property {string} [sortBy='recent'] `recent` or `relevant` - * @property {string} [sortOrder='desc'] `asc` or `desc` + * @property {string} [sortOrder='descending'] `ascending` or `descending` * @property {number} [contextSize=2] How many messages to get around the matched message (0 to 2) * @property {number} [limit=25] Maximum number of results to get (1 to 25) * @property {number} [offset=0] Offset the "pages" of results (since you can only see 25 at a time) @@ -19,7 +19,7 @@ const long = require('long'); * @property {boolean} [mentionsEveryone] If everyone is mentioned * @property {string} [linkHostname] Filter links by hostname * @property {string} [embedProvider] The name of an embed provider - * @property {string} [embedType] one of `image`, `video`, `url`, `rich` + * @property {string} [embedType] one of `image`, `video`, `url`, `rich`, or add `-` to negate (e.g. `-image`) * @property {string} [attachmentFilename] The name of an attachment * @property {string} [attachmentExtension] The extension of an attachment * @property {Date} [before] Date to find messages before @@ -28,6 +28,12 @@ const long = require('long'); * @property {boolean} [nsfw=false] Include results from NSFW channels */ +/** + * @typedef {Object} MessageSearchResult + * @type {number} total Total result count + * @type {Array} results Array of message results + */ + module.exports = function search(target, options) { if (typeof options === 'string') options = { content: options }; if (options.before) { @@ -42,11 +48,14 @@ module.exports = function search(target, options) { if (!(options.during instanceof Date)) options.during = new Date(options.during); const t = options.during.getTime() - 14200704e5; options.minID = long.fromNumber(t).shiftLeft(22).toString(); - options.maxID = long.fromNumber(t + 86400000).shiftLeft(22).toString(); + options.maxID = long.fromNumber(t + 864e5).shiftLeft(22).toString(); } if (options.channel) options.channel = target.client.resolver.resolveChannelID(options.channel); if (options.author) options.author = target.client.resolver.resolveUserID(options.author); if (options.mentions) options.mentions = target.client.resolver.resolveUserID(options.options.mentions); + if (options.sortOrder) { + options.sortOrder = { ascending: 'asc', descending: 'desc' }[options.sortOrder] || options.sortOrder; + } options = { content: options.content, max_id: options.maxID, @@ -81,12 +90,12 @@ module.exports = function search(target, options) { let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search; return endpoint.get({ query: options }).then(body => { - const messages = body.messages.map(x => + const results = body.messages.map(x => x.map(m => new Message(target.client.channels.get(m.channel_id), m, target.client)) ); return { - totalResults: body.total_results, - messages, + total: body.total_results, + results, }; }); };