#[non_exhaustive]pub struct Webhook {
pub id: WebhookId,
pub kind: WebhookType,
pub avatar: Option<String>,
pub channel_id: Option<ChannelId>,
pub guild_id: Option<GuildId>,
pub name: Option<String>,
pub token: Option<String>,
pub user: Option<User>,
}
Expand description
A representation of a webhook, which is a low-effort way to post messages to channels. They do not necessarily require a bot user or authentication to use.
Fields (Non-exhaustive)
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.id: WebhookId
The unique Id.
Can be used to calculate the creation date of the webhook.
kind: WebhookType
The type of the webhook.
avatar: Option<String>
The default avatar.
This can be modified via ExecuteWebhook::avatar_url
.
channel_id: Option<ChannelId>
The Id of the channel that owns the webhook.
guild_id: Option<GuildId>
The Id of the guild that owns the webhook.
name: Option<String>
The default name of the webhook.
This can be modified via ExecuteWebhook::username
.
token: Option<String>
The webhook’s secure token.
user: Option<User>
The user that created the webhook.
Note: This is not received when getting a webhook by its token.
Implementations
sourceimpl Webhook
impl Webhook
sourcepub async fn from_id(
http: impl AsRef<Http>,
webhook_id: impl Into<WebhookId>
) -> Result<Self>
Available on crate feature model
only.
pub async fn from_id(
http: impl AsRef<Http>,
webhook_id: impl Into<WebhookId>
) -> Result<Self>
model
only.Retrieves a webhook given its Id.
This method requires authentication, whereas Webhook::from_id_with_token
and
Webhook::from_url
do not.
Examples
Retrieve a webhook by Id:
let id = 245037420704169985;
let webhook = Webhook::from_id(&http, id).await?;
Errors
Returns an Error::Http
if the current user is not authenticated, or if the webhook does
not exist.
May also return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn from_id_with_token(
http: impl AsRef<Http>,
webhook_id: impl Into<WebhookId>,
token: &str
) -> Result<Self>
Available on crate feature model
only.
pub async fn from_id_with_token(
http: impl AsRef<Http>,
webhook_id: impl Into<WebhookId>,
token: &str
) -> Result<Self>
model
only.Retrieves a webhook given its Id and unique token.
This method does not require authentication.
Examples
Retrieve a webhook by Id and its unique token:
let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let webhook = Webhook::from_id_with_token(&http, id, token).await?;
Errors
Returns an Error::Http
if the webhook does not exist, or if the token is invalid.
May also return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn from_url(http: impl AsRef<Http>, url: &str) -> Result<Self>
Available on crate feature model
only.
pub async fn from_url(http: impl AsRef<Http>, url: &str) -> Result<Self>
model
only.Retrieves a webhook given its url.
This method does not require authentication
Examples
Retrieve a webhook by url:
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let webhook = Webhook::from_url(&http, url).await?;
Errors
Returns an Error::Http
if the url is malformed, or otherwise if the webhook does not exist, or if the token is invalid.
May also return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn delete(&self, http: impl AsRef<Http>) -> Result<()>
Available on crate feature model
only.
pub async fn delete(&self, http: impl AsRef<Http>) -> Result<()>
model
only.Deletes the webhook.
As this calls the Http::delete_webhook_with_token
function,
authentication is not required.
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the webhook does not exist,
the token is invalid, or if the webhook could not otherwise
be deleted.
sourcepub async fn edit_name(
&mut self,
http: impl AsRef<Http>,
name: &str
) -> Result<()>
Available on crate feature model
only.
pub async fn edit_name(
&mut self,
http: impl AsRef<Http>,
name: &str
) -> Result<()>
model
only.Edits the name of a webhook.
Refer to Http::edit_webhook
for restrictions on webhook names.
Does not require authentication, as this calls Http::edit_webhook_with_token
internally.
Examples
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;
webhook.edit_name(&http, "new name").await?;
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the content is malformed, or if the token is invalid.
Or may return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn edit_avatar<'a>(
&mut self,
http: impl AsRef<Http>,
avatar: impl Into<AttachmentType<'a>>
) -> Result<()>
Available on crate feature model
only.
pub async fn edit_avatar<'a>(
&mut self,
http: impl AsRef<Http>,
avatar: impl Into<AttachmentType<'a>>
) -> Result<()>
model
only.Edits a webhook’s avatar.
Refer to Http::edit_webhook
for restrictions on webhook avatars.
Does not require authentication, as it calls Http::edit_webhook_with_token
internally.
Examples
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;
webhook.edit_avatar(&http, "./webhook_img.png").await?;
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the content is malformed, or if the token is invalid.
Or may return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn delete_avatar(&mut self, http: impl AsRef<Http>) -> Result<()>
Available on crate feature model
only.
pub async fn delete_avatar(&mut self, http: impl AsRef<Http>) -> Result<()>
model
only.Deletes a webhook’s avatar, resetting it to the default logo.
Does not require authentication, as it calls Http::edit_webhook_with_token
internally.
Examples
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;
webhook.delete_avatar(&http).await?;
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the content is malformed, or if the token is invalid.
Or may return an Error::Json
if there is an error in deserialising Discord’s response.
sourcepub async fn execute<'a, F>(
&self,
http: impl AsRef<Http>,
wait: bool,
f: F
) -> Result<Option<Message>> where
for<'b> F: FnOnce(&'b mut ExecuteWebhook<'a>) -> &'b mut ExecuteWebhook<'a>,
Available on crate feature model
only.
pub async fn execute<'a, F>(
&self,
http: impl AsRef<Http>,
wait: bool,
f: F
) -> Result<Option<Message>> where
for<'b> F: FnOnce(&'b mut ExecuteWebhook<'a>) -> &'b mut ExecuteWebhook<'a>,
model
only.Executes a webhook with the fields set via the given builder.
The builder provides a method of setting only the fields you need, without needing to pass a long set of arguments.
Examples
Execute a webhook with message content of test
:
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;
webhook.execute(&http, false, |w| w.content("test")).await?;
Execute a webhook with message content of test
, overriding the
username to serenity
, and sending an embed:
use serenity::model::channel::Embed;
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let mut webhook = Webhook::from_url(&http, url).await?;
let embed = Embed::fake(|e| {
e.title("Rust's website")
.description(
"Rust is a systems programming language that runs
blazingly fast, prevents segfaults, and guarantees
thread safety.",
)
.url("https://rust-lang.org")
});
webhook
.execute(&http, false, |w| w.content("test").username("serenity").embeds(vec![embed]))
.await?;
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the content is malformed, or if the webhook’s token is invalid.
Or may return an Error::Json
if there is an error deserialising Discord’s response.
sourcepub async fn get_message(
&self,
http: impl AsRef<Http>,
message_id: MessageId
) -> Result<Message>
Available on crate feature model
only.
pub async fn get_message(
&self,
http: impl AsRef<Http>,
message_id: MessageId
) -> Result<Message>
model
only.Gets a previously sent message from the webhook.
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return Error::Http
if the webhook’s token is invalid, or
the given message Id does not belong to the current webhook.
Or may return an Error::Json
if there is an error deserialising Discord’s response.
sourcepub async fn edit_message<F>(
&self,
http: impl AsRef<Http>,
message_id: MessageId,
f: F
) -> Result<Message> where
F: FnOnce(&mut EditWebhookMessage) -> &mut EditWebhookMessage,
Available on crate feature model
only.
pub async fn edit_message<F>(
&self,
http: impl AsRef<Http>,
message_id: MessageId,
f: F
) -> Result<Message> where
F: FnOnce(&mut EditWebhookMessage) -> &mut EditWebhookMessage,
model
only.Edits a webhook message with the fields set via the given builder.
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the content is malformed, the webhook’s token is invalid, or
the given message Id does not belong to the current webhook.
Or may return an Error::Json
if there is an error deserialising Discord’s response.
sourcepub async fn delete_message(
&self,
http: impl AsRef<Http>,
message_id: MessageId
) -> Result<()>
Available on crate feature model
only.
pub async fn delete_message(
&self,
http: impl AsRef<Http>,
message_id: MessageId
) -> Result<()>
model
only.Deletes a webhook message.
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the webhook’s token is invalid or
the given message Id does not belong to the current webhook.
sourcepub async fn refresh(&mut self, http: impl AsRef<Http>) -> Result<()>
Available on crate feature model
only.
pub async fn refresh(&mut self, http: impl AsRef<Http>) -> Result<()>
model
only.Retrieves the latest information about the webhook, editing the webhook in-place.
As this calls the Http::get_webhook_with_token
function,
authentication is not required.
Errors
Returns an Error::Model
if the Self::token
is None
.
May also return an Error::Http
if the http client errors or if Discord returns an error.
Such as if the Webhook
was deleted.
Or may return an Error::Json
if there is an error deserialising Discord’s response.
sourcepub fn url(&self) -> Result<String>
Available on crate feature model
only.
pub fn url(&self) -> Result<String>
model
only.Returns the url of the webhook.
assert_eq!(hook.url(), "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV")
Errors
Returns an Error::Model
if the Self::token
is None
.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Webhook
impl<'de> Deserialize<'de> for Webhook
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
impl RefUnwindSafe for Webhook
impl Send for Webhook
impl Sync for Webhook
impl Unpin for Webhook
impl UnwindSafe for Webhook
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more