Skip to main content

twilight_http/request/guild/sticker/
delete_guild_sticker.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{self, AuditLogReason, Request, TryIntoRequest},
5    response::{Response, ResponseFuture, marker::EmptyBody},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::id::{
10    Id,
11    marker::{GuildMarker, StickerMarker},
12};
13use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
14
15/// Deletes a guild sticker by the ID of the guild and its ID.
16///
17/// # Examples
18///
19/// ```no_run
20/// use twilight_http::Client;
21/// use twilight_model::id::Id;
22///
23/// # #[tokio::main]
24/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
25/// let client = Client::new("my token".to_owned());
26///
27/// let guild_id = Id::new(1);
28/// let sticker_id = Id::new(2);
29///
30/// client.delete_guild_sticker(guild_id, sticker_id).await?;
31/// # Ok(()) }
32/// ```
33pub struct DeleteGuildSticker<'a> {
34    guild_id: Id<GuildMarker>,
35    http: &'a Client,
36    reason: Result<Option<&'a str>, ValidationError>,
37    sticker_id: Id<StickerMarker>,
38}
39
40impl<'a> DeleteGuildSticker<'a> {
41    pub(crate) const fn new(
42        http: &'a Client,
43        guild_id: Id<GuildMarker>,
44        sticker_id: Id<StickerMarker>,
45    ) -> Self {
46        Self {
47            guild_id,
48            http,
49            reason: Ok(None),
50            sticker_id,
51        }
52    }
53}
54
55impl<'a> AuditLogReason<'a> for DeleteGuildSticker<'a> {
56    fn reason(mut self, reason: &'a str) -> Self {
57        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
58
59        self
60    }
61}
62
63impl IntoFuture for DeleteGuildSticker<'_> {
64    type Output = Result<Response<EmptyBody>, Error>;
65
66    type IntoFuture = ResponseFuture<EmptyBody>;
67
68    fn into_future(self) -> Self::IntoFuture {
69        let http = self.http;
70
71        match self.try_into_request() {
72            Ok(request) => http.request(request),
73            Err(source) => ResponseFuture::error(source),
74        }
75    }
76}
77
78impl TryIntoRequest for DeleteGuildSticker<'_> {
79    fn try_into_request(self) -> Result<Request, Error> {
80        let mut request = Request::builder(&Route::DeleteGuildSticker {
81            guild_id: self.guild_id.get(),
82            sticker_id: self.sticker_id.get(),
83        });
84
85        if let Some(reason) = self.reason.map_err(Error::validation)? {
86            request = request.headers(request::audit_header(reason)?);
87        }
88
89        request.build()
90    }
91}