Skip to main content

twilight_http/request/application/interaction/
delete_followup.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::EmptyBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::id::{
11    Id,
12    marker::{ApplicationMarker, MessageMarker},
13};
14
15/// Delete the original message, by its token.
16///
17/// This endpoint is not bound to the application's global rate limit.
18///
19/// # Examples
20///
21/// ```no_run
22/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
23/// use std::env;
24/// use twilight_http::Client;
25/// use twilight_model::id::Id;
26///
27/// let client = Client::new(env::var("DISCORD_TOKEN")?);
28/// let application_id = Id::new(1);
29///
30/// client
31///     .interaction(application_id)
32///     .delete_followup("token here", Id::new(2))
33///     .await?;
34/// # Ok(()) }
35/// ```
36#[must_use = "requests must be configured and executed"]
37pub struct DeleteFollowup<'a> {
38    http: &'a Client,
39    message_id: Id<MessageMarker>,
40    token: &'a str,
41    application_id: Id<ApplicationMarker>,
42}
43
44impl<'a> DeleteFollowup<'a> {
45    pub(crate) const fn new(
46        http: &'a Client,
47        application_id: Id<ApplicationMarker>,
48        token: &'a str,
49        message_id: Id<MessageMarker>,
50    ) -> Self {
51        Self {
52            http,
53            message_id,
54            token,
55            application_id,
56        }
57    }
58}
59
60#[cfg(not(target_os = "wasi"))]
61impl IntoFuture for DeleteFollowup<'_> {
62    type Output = Result<Response<EmptyBody>, Error>;
63
64    type IntoFuture = ResponseFuture<EmptyBody>;
65
66    fn into_future(self) -> Self::IntoFuture {
67        let http = self.http;
68
69        match self.try_into_request() {
70            Ok(request) => http.request(request),
71            Err(source) => ResponseFuture::error(source),
72        }
73    }
74}
75
76impl TryIntoRequest for DeleteFollowup<'_> {
77    fn try_into_request(self) -> Result<Request, Error> {
78        Request::builder(&Route::DeleteWebhookMessage {
79            message_id: self.message_id.get(),
80            thread_id: None,
81            token: self.token,
82            webhook_id: self.application_id.get(),
83        })
84        .use_authorization_token(false)
85        .build()
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::DeleteFollowup;
92    use crate::{
93        client::Client,
94        request::{Request, TryIntoRequest},
95        routing::Route,
96    };
97    use std::error::Error;
98    use twilight_model::id::Id;
99
100    #[test]
101    fn request() -> Result<(), Box<dyn Error>> {
102        let client = Client::new("token".to_owned());
103
104        let builder = DeleteFollowup::new(&client, Id::new(1), "token", Id::new(2));
105        let actual = builder.try_into_request()?;
106
107        let expected = Request::from_route(&Route::DeleteWebhookMessage {
108            message_id: 2,
109            thread_id: None,
110            token: "token",
111            webhook_id: 1,
112        });
113
114        assert_eq!(expected.path, actual.path);
115        assert!(!actual.use_authorization_token());
116
117        Ok(())
118    }
119}