Struct ExecuteWebhook

Source
pub struct ExecuteWebhook { /* private fields */ }
Expand description

A builder to create the content for a Webhook’s execution.

Refer to Http::execute_webhook for restrictions and requirements on the execution payload.

§Examples

Creating two embeds, and then sending them as part of the payload using Webhook::execute:

use serenity::builder::{CreateEmbed, ExecuteWebhook};
use serenity::http::Http;
use serenity::model::webhook::Webhook;
use serenity::model::Colour;

let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let webhook = Webhook::from_url(&http, url).await?;

let website = CreateEmbed::new()
    .title("The Rust Language Website")
    .description("Rust is a systems programming language.")
    .colour(Colour::from_rgb(222, 165, 132));

let resources = CreateEmbed::new()
    .title("Rust Resources")
    .description("A few resources to help with learning Rust")
    .colour(0xDEA584)
    .field("The Rust Book", "A comprehensive resource for Rust.", false)
    .field("Rust by Example", "A collection of Rust examples", false);

let builder = ExecuteWebhook::new()
    .content("Here's some information on Rust:")
    .embeds(vec![website, resources]);
webhook.execute(&http, false, builder).await?;

Discord docs

Implementations§

Source§

impl ExecuteWebhook

Source

pub fn new() -> Self

Equivalent to Self::default.

Source

pub fn avatar_url(self, avatar_url: impl Into<String>) -> Self

Override the default avatar of the webhook with an image URL.

§Examples

Overriding the default avatar:

let builder = ExecuteWebhook::new()
    .avatar_url("https://i.imgur.com/KTs6whd.jpg")
    .content("Here's a webhook");
webhook.execute(&http, false, builder).await?;
Source

pub fn content(self, content: impl Into<String>) -> Self

Set the content of the message.

Note that when setting at least one embed via Self::embeds, this may be omitted.

§Examples

Sending a webhook with a content of "foo":

let builder = ExecuteWebhook::new().content("foo");
let execution = webhook.execute(&http, false, builder).await;

if let Err(why) = execution {
    println!("Err sending webhook: {:?}", why);
}
Source

pub fn in_thread(self, thread_id: impl Into<ChannelId>) -> Self

Execute within a given thread. If the provided thread Id doesn’t belong to the current webhook, the API will return an error.

Note: If the given thread is archived, it will automatically be unarchived.

§Examples

Execute a webhook with message content of test, in a thread with Id 12345678:

let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;

let builder = ExecuteWebhook::new().in_thread(12345678).content("test");
webhook.execute(&http, false, builder).await?;
Source

pub fn add_file(self, file: CreateAttachment) -> Self

Appends a file to the webhook message.

Source

pub fn add_files( self, files: impl IntoIterator<Item = CreateAttachment>, ) -> Self

Appends a list of files to the webhook message.

Source

pub fn files(self, files: impl IntoIterator<Item = CreateAttachment>) -> Self

Sets a list of files to include in the webhook message.

Calling this multiple times will overwrite the file list. To append files, call Self::add_file or Self::add_files instead.

Source

pub fn allowed_mentions(self, allowed_mentions: CreateAllowedMentions) -> Self

Set the allowed mentions for the message.

Source

pub fn components(self, components: Vec<CreateActionRow>) -> Self

Sets the components for this message. Requires an application-owned webhook, meaning either the webhook’s kind field is set to WebhookType::Application, or it was created by an application (and has kind WebhookType::Incoming).

Source

pub fn button(self, button: CreateButton) -> Self

Adds a clickable button to this message.

Convenience method that wraps Self::components. Arranges buttons in action rows automatically.

Source

pub fn select_menu(self, select_menu: CreateSelectMenu) -> Self

Adds an interactive select menu to this message.

Convenience method that wraps Self::components.

Source

pub fn embed(self, embed: CreateEmbed) -> Self

Set an embed for the message.

Refer to the struct-level documentation for an example on how to use embeds.

Source

pub fn embeds(self, embeds: Vec<CreateEmbed>) -> Self

Set multiple embeds for the message.

Source

pub fn tts(self, tts: bool) -> Self

Whether the message is a text-to-speech message.

§Examples

Sending a webhook with text-to-speech enabled:

let builder = ExecuteWebhook::new().content("hello").tts(true);
let execution = webhook.execute(&http, false, builder).await;

if let Err(why) = execution {
    println!("Err sending webhook: {:?}", why);
}
Source

pub fn username(self, username: impl Into<String>) -> Self

Override the default username of the webhook.

§Examples

Overriding the username to "hakase":

let builder = ExecuteWebhook::new().content("hello").username("hakase");
let execution = webhook.execute(&http, false, builder).await;

if let Err(why) = execution {
    println!("Err sending webhook: {:?}", why);
}
Source

pub fn flags(self, flags: MessageFlags) -> Self

Sets the flags for the message.

§Examples

Suppressing an embed on the message.

let builder = ExecuteWebhook::new()
    .content("https://docs.rs/serenity/latest/serenity/")
    .flags(MessageFlags::SUPPRESS_EMBEDS);
let execution = webhook.execute(&http, false, builder).await;

if let Err(why) = execution {
    println!("Err sending webhook: {:?}", why);
}
Source

pub fn thread_name(self, thread_name: String) -> Self

Name of thread to create (requires the webhook channel to be a forum channel)

Trait Implementations§

Source§

impl Builder for ExecuteWebhook

Source§

fn execute<'life0, 'async_trait>( self, cache_http: impl 'async_trait + CacheHttp, ctx: Self::Context<'life0>, ) -> Pin<Box<dyn Future<Output = Result<Self::Built>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Executes the webhook with the given content.

§Errors

Returns Error::Http if the content is malformed, if the token is invalid, or if execution is attempted in a thread not belonging to the webhook’s Channel.

Returns Error::Json if there is an error in deserialising Discord’s response.

Source§

type Context<'ctx> = (WebhookId, &'ctx str, bool)

Additional data that’s only required when sending a request off to the API.
Source§

type Built = Option<Message>

Source§

impl Clone for ExecuteWebhook

Source§

fn clone(&self) -> ExecuteWebhook

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExecuteWebhook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ExecuteWebhook

Source§

fn default() -> ExecuteWebhook

Returns the “default value” for a type. Read more
Source§

impl Serialize for ExecuteWebhook

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneDebuggableStorage for T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> CloneableStorage for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T