Struct serenity::utils::builder::Search
[−]
[src]
pub struct Search(pub BTreeMap<&'static str, String>);
A builder used to query a Channel
or Guild
for its Message
s,
specifying certain parameters to narrow down the returned messages.
Many methods are provided to narrow down the results, such as sort_by
-
which is used with the SortingMode
enum to sort the results - or
limit
, which can be used in conjunction with offset
to paginate
results.
Examples
Provided are multiple in-depth examples for searching through different means. Also see example 08 for a fully runnable bot.
Searching a Channel
Search for messages via Context::search_channel
with the content
"rust"
, which have no embed, no attachment, searching by relevance in
ascending order, and limiting to 5 results:
// assuming you are in a context let res = context.search_channel(message.channel_id, |s| s .content("rust") .has_embed(false) .has_attachment(false) .limit(5) .sort_by(SortingMode::Relevance) .sort_order(SortingOrder::Ascending));
Searching a Guild's Channels
Search for messages with a query provided by a user, which have an
embed, have no attachment, searching by timestamp in descending order,
limiting to 2 results, and only searching channels that have a name
prefixed with "search-"
:
use serenity::client::{Client, Context}; use serenity::model::Message; use serenity::utils::builder::{SortingMode, SortingOrder}; use std::env; let mut client = Client::login_bot(&env::var("DISCORD_BOT_TOKEN").unwrap()); client.with_framework(|f| f .configure(|c| c.prefix("~").on_mention(true)) .on("search", search)); command!(search(context, message, args) { let query = args.join(" "); if query.is_empty() { let _ = context.say("You must provide a query"); return Ok(()); } let guild = message.guild().unwrap(); let channel_ids = guild .channels .values() .filter(|c| c.name.starts_with("search-")) .map(|c| c.id) .collect(); let search = context.search_guild(guild.id, channel_ids, |s| s .content(&query) .context_size(0) .has_attachment(true) .has_embed(true) .max_id(message.id.0 - 1) .sort_by(SortingMode::Timestamp) .sort_order(SortingOrder::Descending)); let mut messages = match search { Ok(messages) => messages, Err(why) => { println!("Error performing search '{}': {:?}", query, why); let _ = context.say("Error occurred while searching"); return Ok(()); }, }; let _ = context.send_message(message.channel_id, |m| m .content(&format!("Found {} total results", messages.total)) .embed(|mut e| { for (i, messages) in messages.results.iter_mut().enumerate() { let mut message = match messages.get_mut(i) { Some(message) => message, None => break, }; message.content.truncate(1000); e = e.field(|f| f .name(&format!("Result {}", i)) .value(&message.content)); } e })); });
Methods
impl Search
[src]
fn attachment_extensions(self, attachment_extensions: &[&str]) -> Self
Sets the list of attachment extensions to search by.
When providing a vector of extensions, do not include the period (.
)
character as part of the search.
This is sent to Discord as a comma-separated value list of extension names.
fn attachment_filename(self, attachment_filename: &str) -> Self
Sets the filename of the attachments to search for.
fn content(self, content: &str) -> Self
Sets the content of the Message
to search for. This is a fuzzy
search, and can partially match the given query content.
fn context_size(self, context_size: u8) -> Self
Sets the amount of "context" Message
s to provide, at maximum. This
is the number of messages to provide around each side
(ascending+descending) of the "hit" (aka found) message.
The default value is 2
. The minimum value is 0
. The maximum value is
2
.
fn embed_providers(self, embed_providers: &[&str]) -> Self
Sets the embed providers to search by.
This is a list of the providers' names.
This is sent to Discord as a comma-separated value list of provider names.
fn embed_types(self, embed_types: &[&str]) -> Self
Sets the type of Embed
s to search by.
An example of an embed type is "rich"
.
fn has_attachment(self, has_attachment: bool) -> Self
Sets whether to search for methods that do - or do not - have an attachment.
Do not specify to search for both.
fn has_embed(self, has_embed: bool) -> Self
Sets whether to search for methods that do - or do not - have an embed.
Do not specify to search for both.
fn limit(self, limit: u8) -> Self
Sets the number of messages to retrieve at maximum. This can be used
in conjunction with offset
.
The minimum value is 1
. The maximum value is 25
.
fn max_id<M: Into<MessageId>>(self, message_id: M) -> Self
Set the maximum Message
Id to search up to. All messages with an Id
greater than the given value will be ignored.
fn min_id<M: Into<MessageId>>(self, message_id: M) -> Self
Set the minimum Message
s Id to search down to. All messages with an
Id less than the given value will be ignored.
fn offset(self, offset: u16) -> Self
Set the offset of Message
s to return. This can be used in
conjunction with limit
.
The minimum value is 0
. The maximum value is 5000
.
fn sort_by(self, sorting_mode: SortingMode) -> Self
The sorting mode to use.
Refer to SortingMode
for more information.
fn sort_order(self, sorting_order: SortingOrder) -> Self
The order to sort results by.
Refer to the documentation for SortingOrder
for more information.
Trait Implementations
impl Default for Search
[src]
fn default() -> Search
Creates a new builder for searching for [Message
]s. Refer to each
method to learn what minimum and maximum values are available for each
field, as well as restrictions and other useful information.
The library does not provide defaults differently than what Discord itself defaults to.
This list of defaults is:
context_size
: 2limit
: 25offset
: 0sort_by
:SortingMode::Timestamp