Skip to main content

twilight_http/request/scheduled_event/create_guild_scheduled_event/
external.rs

1use super::{
2    super::EntityMetadataFields, CreateGuildScheduledEvent, CreateGuildScheduledEventFields,
3};
4use crate::{
5    error::Error,
6    request::{AuditLogReason, Request, TryIntoRequest},
7    response::{Response, ResponseFuture},
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    guild::scheduled_event::{EntityType, GuildScheduledEvent, recurrence_rule::RecurrenceRule},
12    util::Timestamp,
13};
14use twilight_validate::request::{
15    audit_reason as validate_audit_reason,
16    scheduled_event_description as validate_scheduled_event_description,
17};
18
19/// Create an external scheduled event in a guild.
20#[must_use = "requests must be configured and executed"]
21pub struct CreateGuildExternalScheduledEvent<'a>(CreateGuildScheduledEvent<'a>);
22
23#[allow(clippy::needless_pass_by_value)]
24impl<'a> CreateGuildExternalScheduledEvent<'a> {
25    pub(crate) fn new(
26        mut inner: CreateGuildScheduledEvent<'a>,
27        name: &'a str,
28        location: &'a str,
29        scheduled_start_time: &'a Timestamp,
30        scheduled_end_time: &'a Timestamp,
31    ) -> Self {
32        inner.fields = inner.fields.map(|fields| CreateGuildScheduledEventFields {
33            entity_type: Some(EntityType::External),
34            entity_metadata: Some(EntityMetadataFields {
35                location: Some(location),
36            }),
37            name: Some(name),
38            scheduled_end_time: Some(scheduled_end_time),
39            scheduled_start_time: Some(scheduled_start_time),
40            ..fields
41        });
42
43        Self(inner)
44    }
45
46    /// Set the description of the event.
47    ///
48    /// Must be between 1 and 1000 characters in length.
49    ///
50    /// # Errors
51    ///
52    /// Returns an error of type [`ScheduledEventDescription`] if the
53    /// description is invalid.
54    ///
55    /// [`ScheduledEventDescription`]: twilight_validate::request::ValidationErrorType::ScheduledEventDescription
56    pub fn description(mut self, description: &'a str) -> Self {
57        self.0.fields = self.0.fields.and_then(|mut fields| {
58            validate_scheduled_event_description(description)?;
59            fields.description.replace(description);
60
61            Ok(fields)
62        });
63
64        self
65    }
66
67    /// Set the cover image of the event.
68    ///
69    /// This must be a Data URI, in the form of
70    /// `data:image/{type};base64,{data}` where `{type}` is the image MIME type
71    /// and `{data}` is the base64-encoded image. See [Discord Docs/Image Data].
72    ///
73    /// [Discord Docs/Image Data]: https://discord.com/developers/docs/reference#image-data
74    pub fn image(mut self, image: &'a str) -> Self {
75        self.0.fields = self.0.fields.map(|mut fields| {
76            fields.image = Some(image);
77
78            fields
79        });
80
81        self
82    }
83
84    /// Set the definition for how often this event should recur.
85    ///
86    /// This is not a required field.
87    pub fn recurrence_rule(mut self, recurrence_rule: &'a RecurrenceRule) -> Self {
88        self.0.fields = self.0.fields.map(|mut fields| {
89            fields.recurrence_rule = Some(recurrence_rule);
90
91            fields
92        });
93
94        self
95    }
96}
97
98impl<'a> AuditLogReason<'a> for CreateGuildExternalScheduledEvent<'a> {
99    fn reason(mut self, reason: &'a str) -> Self {
100        self.0.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
101
102        self
103    }
104}
105
106impl IntoFuture for CreateGuildExternalScheduledEvent<'_> {
107    type Output = Result<Response<GuildScheduledEvent>, Error>;
108
109    type IntoFuture = ResponseFuture<GuildScheduledEvent>;
110
111    fn into_future(self) -> Self::IntoFuture {
112        self.0.exec()
113    }
114}
115
116impl TryIntoRequest for CreateGuildExternalScheduledEvent<'_> {
117    fn try_into_request(self) -> Result<Request, Error> {
118        self.0.try_into_request()
119    }
120}