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?;
Implementations§
Source§impl ExecuteWebhook
impl ExecuteWebhook
Sourcepub fn new() -> Self
pub fn new() -> Self
Equivalent to Self::default
.
Sourcepub fn avatar_url(self, avatar_url: impl Into<String>) -> Self
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?;
Sourcepub fn content(self, content: impl Into<String>) -> Self
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);
}
Sourcepub fn in_thread(self, thread_id: impl Into<ChannelId>) -> Self
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?;
Sourcepub fn add_file(self, file: CreateAttachment) -> Self
pub fn add_file(self, file: CreateAttachment) -> Self
Appends a file to the webhook message.
Sourcepub fn add_files(
self,
files: impl IntoIterator<Item = CreateAttachment>,
) -> Self
pub fn add_files( self, files: impl IntoIterator<Item = CreateAttachment>, ) -> Self
Appends a list of files to the webhook message.
Sourcepub fn files(self, files: impl IntoIterator<Item = CreateAttachment>) -> Self
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.
Sourcepub fn allowed_mentions(self, allowed_mentions: CreateAllowedMentions) -> Self
pub fn allowed_mentions(self, allowed_mentions: CreateAllowedMentions) -> Self
Set the allowed mentions for the message.
Sourcepub fn components(self, components: Vec<CreateActionRow>) -> Self
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
).
Adds a clickable button to this message.
Convenience method that wraps Self::components
. Arranges buttons in action rows
automatically.
Adds an interactive select menu to this message.
Convenience method that wraps Self::components
.
Sourcepub fn embed(self, embed: CreateEmbed) -> Self
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.
Sourcepub fn embeds(self, embeds: Vec<CreateEmbed>) -> Self
pub fn embeds(self, embeds: Vec<CreateEmbed>) -> Self
Set multiple embeds for the message.
Sourcepub fn tts(self, tts: bool) -> Self
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);
}
Sourcepub fn username(self, username: impl Into<String>) -> Self
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);
}
Sourcepub fn flags(self, flags: MessageFlags) -> Self
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);
}
Sourcepub fn thread_name(self, thread_name: String) -> Self
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
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,
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)
type Context<'ctx> = (WebhookId, &'ctx str, bool)
type Built = Option<Message>
Source§impl Clone for ExecuteWebhook
impl Clone for ExecuteWebhook
Source§fn clone(&self) -> ExecuteWebhook
fn clone(&self) -> ExecuteWebhook
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more