Skip to main content

Module search_guild_messages

Module search_guild_messages 

Source
Expand description

HTTP request for searching for Messages within a Guild.

This endpoint supports a suite of filters for narrowing search results. While all parameters—such as channel_id, which narrows results to only messages within a list of channels—include only results with those values, some fields support negating a given value to exclude those results. Refer to Negate for those fields. Each field’s method in the SearchGuildMessages request builder also notes whether the field can be negated.

§Indexing

If the entity you are searching has not yet been indexed the endpoint will return a 202 Accepted response and the response body variant will be SearchGuildMessagesResponseBody::Accepted; otherwise, the response body variant will be SearchGuildMessagesResponseBody::Ok with message results.

§Pagination

Due to speed optimizations, search may return slightly fewer results than the limit specified when messages have not been accessed for a long time. Applications should not rely on the length of the messages array to paginate results.

Additionally, when messages are actively being created or deleted, SearchGuildMessagesOkResponseBody::total_results may not be accurate.

§Examples

Search for messages within two channels, including only messages with an image but excluding messages authored by bot users:

use std::env;
use twilight_http::{
    Client,
    request::guild::message::search_guild_messages::{
        AuthorType, SearchFilter, SearchGuildMessages, SearchGuildMessagesResponseBody,
        SearchHasType,
    },
};
use twilight_model::id::Id;

let client = Client::new(env::var("DISCORD_TOKEN")?);
let channel_ids = find_channels();

let response = client
    .search_guild_messages(guild_id)
    .channel_id(&channel_ids)
    // Include only messages with polls
    .has(&[SearchFilter::Include(SearchHasType::Poll)])
    // Exclude messages authored by bots
    .author_type(&[SearchFilter::Exclude(AuthorType::Bot)])
    .await?;
let body = response.model().await?;

match body {
    SearchGuildMessagesResponseBody::Accepted(accepted) => {
        println!(
            "message indexing in progress; retry in {} seconds",
            accepted.retry_after
        );
    }
    SearchGuildMessagesResponseBody::Ok(results) => {
        println!("there are {} total results", results.total_results);
    }
}

Structs§

SearchGuildMessages
Search a guild’s messages.
SearchGuildMessagesAcceptedResponseBody
Body of the response when the returned status code is 202 Accepted.
SearchGuildMessagesOkResponseBody
Body of the response when the returned status code is 200 OK.

Enums§

AuthorType
Filter messages by the type of author.
SearchEmbedType
Embed types to filter results by.
SearchFilter
Mark whether a field is being marked for inclusion or exclusion.
SearchGuildMessagesResponseBody
Potential successful response bodies.
SearchHasType
Filter messages by whether they have a particular type of data.
SearchSortMode
Sorting algorithm to use.
SearchSortOrder
Sorting order to use.

Traits§

Negate
Denotes whether a field value can be negated, excluding results with the field instead of including results.