Skip to main content

twilight_http/request/guild/
update_guild_widget_settings.rs

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