Skip to main content

twilight_http/request/guild/auto_moderation/
delete_auto_moderation_rule.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::EmptyBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{self, AuditLogReason, Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::id::{
11    Id,
12    marker::{AutoModerationRuleMarker, GuildMarker},
13};
14use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
15
16/// Delete an auto moderation rule in a guild.
17///
18/// Requires the [`MANAGE_GUILD`] permission.
19///
20/// [`MANAGE_GUILD`]: twilight_model::guild::Permissions::MANAGE_GUILD
21#[must_use = "requests must be configured and executed"]
22pub struct DeleteAutoModerationRule<'a> {
23    auto_moderation_rule_id: Id<AutoModerationRuleMarker>,
24    guild_id: Id<GuildMarker>,
25    http: &'a Client,
26    reason: Result<Option<&'a str>, ValidationError>,
27}
28
29impl<'a> DeleteAutoModerationRule<'a> {
30    pub(crate) const fn new(
31        http: &'a Client,
32        guild_id: Id<GuildMarker>,
33        auto_moderation_rule_id: Id<AutoModerationRuleMarker>,
34    ) -> Self {
35        Self {
36            auto_moderation_rule_id,
37            guild_id,
38            http,
39            reason: Ok(None),
40        }
41    }
42}
43
44impl<'a> AuditLogReason<'a> for DeleteAutoModerationRule<'a> {
45    fn reason(mut self, reason: &'a str) -> Self {
46        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
47
48        self
49    }
50}
51
52#[cfg(not(target_os = "wasi"))]
53impl IntoFuture for DeleteAutoModerationRule<'_> {
54    type Output = Result<Response<EmptyBody>, Error>;
55
56    type IntoFuture = ResponseFuture<EmptyBody>;
57
58    fn into_future(self) -> Self::IntoFuture {
59        let http = self.http;
60
61        match self.try_into_request() {
62            Ok(request) => http.request(request),
63            Err(source) => ResponseFuture::error(source),
64        }
65    }
66}
67
68impl TryIntoRequest for DeleteAutoModerationRule<'_> {
69    fn try_into_request(self) -> Result<Request, Error> {
70        let mut request = Request::builder(&Route::DeleteAutoModerationRule {
71            auto_moderation_rule_id: self.auto_moderation_rule_id.get(),
72            guild_id: self.guild_id.get(),
73        });
74
75        if let Some(reason) = self.reason.map_err(Error::validation)? {
76            request = request.headers(request::audit_header(reason)?);
77        }
78
79        request.build()
80    }
81}