twilight_http/request/guild/
update_guild_widget_settings.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{self, AuditLogReason, Nullable, Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11    guild::widget::GuildWidgetSettings,
12    id::{
13        marker::{ChannelMarker, GuildMarker},
14        Id,
15    },
16};
17use twilight_validate::request::{audit_reason as validate_audit_reason, ValidationError};
18
19#[derive(Serialize)]
20struct UpdateGuildWidgetSettingsFields {
21    #[serde(skip_serializing_if = "Option::is_none")]
22    channel_id: Option<Nullable<Id<ChannelMarker>>>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    enabled: Option<bool>,
25}
26
27/// Modify a guild's widget settings.
28///
29/// See [Discord Docs/Modify Guild Widget].
30///
31/// [Discord Docs/Modify Guild Widget]: https://discord.com/developers/docs/resources/guild#modify-guild-widget
32#[must_use = "requests must be configured and executed"]
33pub struct UpdateGuildWidgetSettings<'a> {
34    fields: UpdateGuildWidgetSettingsFields,
35    guild_id: Id<GuildMarker>,
36    http: &'a Client,
37    reason: Result<Option<&'a str>, ValidationError>,
38}
39
40impl<'a> UpdateGuildWidgetSettings<'a> {
41    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
42        Self {
43            fields: UpdateGuildWidgetSettingsFields {
44                channel_id: None,
45                enabled: None,
46            },
47            guild_id,
48            http,
49            reason: Ok(None),
50        }
51    }
52
53    /// Set which channel to display on the widget.
54    pub const fn channel_id(mut self, channel_id: Option<Id<ChannelMarker>>) -> Self {
55        self.fields.channel_id = Some(Nullable(channel_id));
56
57        self
58    }
59
60    /// Set to true to enable the guild widget.
61    pub const fn enabled(mut self, enabled: bool) -> Self {
62        self.fields.enabled = Some(enabled);
63
64        self
65    }
66}
67
68impl<'a> AuditLogReason<'a> for UpdateGuildWidgetSettings<'a> {
69    fn reason(mut self, reason: &'a str) -> Self {
70        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
71
72        self
73    }
74}
75
76impl IntoFuture for UpdateGuildWidgetSettings<'_> {
77    type Output = Result<Response<GuildWidgetSettings>, Error>;
78
79    type IntoFuture = ResponseFuture<GuildWidgetSettings>;
80
81    fn into_future(self) -> Self::IntoFuture {
82        let http = self.http;
83
84        match self.try_into_request() {
85            Ok(request) => http.request(request),
86            Err(source) => ResponseFuture::error(source),
87        }
88    }
89}
90
91impl TryIntoRequest for UpdateGuildWidgetSettings<'_> {
92    fn try_into_request(self) -> Result<Request, Error> {
93        let mut request = Request::builder(&Route::UpdateGuildWidgetSettings {
94            guild_id: self.guild_id.get(),
95        });
96
97        request = request.json(&self.fields);
98
99        if let Some(reason) = self.reason.map_err(Error::validation)? {
100            request = request.headers(request::audit_header(reason)?);
101        }
102
103        request.build()
104    }
105}