twilight_util::link::webhook

Function parse

Source
pub fn parse(
    url: &str,
) -> Result<(Id<WebhookMarker>, Option<&str>), WebhookParseError>
Available on crate feature link only.
Expand description

Parse the webhook ID and token from a webhook URL, if it exists in the string.

§Examples

Parse a webhook URL with a token:

use twilight_model::id::Id;
use twilight_util::link::webhook;

let url = "https://canary.discord.com/api/webhooks/794590023369752587/tjxHaPHLKp9aEdSwJuLeHhHHGEqIxt1aay4I67FOP9uzsYEWmj0eJmDn-2ZvCYLyOb_K";

let (id, token) = webhook::parse(url)?;
assert_eq!(Id::new(794590023369752587), id);
assert_eq!(
    Some("tjxHaPHLKp9aEdSwJuLeHhHHGEqIxt1aay4I67FOP9uzsYEWmj0eJmDn-2ZvCYLyOb_K"),
    token,
);

Parse a webhook URL without a token:

use twilight_model::id::Id;
use twilight_util::link::webhook;

let url = "https://canary.discord.com/api/webhooks/794590023369752587";

let (id, token) = webhook::parse(url)?;
assert_eq!(Id::new(794590023369752587), id);
assert!(token.is_none());

§Errors

Returns WebhookParseErrorType::IdInvalid error type if the ID segment of the URL is not a valid integer.

Returns WebhookParseErrorType::SegmentMissing error type if one of the required segments is missing. This can be the “api” or “webhooks” standard segment of the URL or the segment containing the webhook ID.