mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
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
This commit is contained in:
@@ -538,7 +538,7 @@ class Guild {
|
||||
* Performs a search within the entire guild.
|
||||
* <warn>This is only available when using a user account.</warn>
|
||||
* @param {MessageSearchOptions} [options={}] Options to pass to the search
|
||||
* @returns {Promise<Array<Message[]>>}
|
||||
* @returns {Promise<MessageSearchResult>}
|
||||
* 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 = {}) {
|
||||
|
||||
@@ -203,7 +203,7 @@ class TextBasedChannel {
|
||||
* Performs a search within the channel.
|
||||
* <warn>This is only available when using a user account.</warn>
|
||||
* @param {MessageSearchOptions} [options={}] Options to pass to the search
|
||||
* @returns {Promise<Array<Message[]>>}
|
||||
* @returns {Promise<MessageSearchResult>}
|
||||
* 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 = {}) {
|
||||
|
||||
@@ -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<Message[]>} 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,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user