serenity/builder/
create_scheduled_event.rs

1#[cfg(feature = "http")]
2use super::Builder;
3use super::CreateAttachment;
4#[cfg(feature = "http")]
5use crate::http::CacheHttp;
6#[cfg(feature = "http")]
7use crate::internal::prelude::*;
8use crate::model::prelude::*;
9
10/// [Discord docs](https://discord.com/developers/docs/resources/guild-scheduled-event#create-guild-scheduled-event)
11#[derive(Clone, Debug, Serialize)]
12#[must_use]
13pub struct CreateScheduledEvent<'a> {
14    #[serde(skip_serializing_if = "Option::is_none")]
15    channel_id: Option<ChannelId>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    entity_metadata: Option<ScheduledEventMetadata>,
18    name: String,
19    privacy_level: ScheduledEventPrivacyLevel,
20    scheduled_start_time: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    scheduled_end_time: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    description: Option<String>,
25    entity_type: ScheduledEventType,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    image: Option<String>,
28
29    #[serde(skip)]
30    audit_log_reason: Option<&'a str>,
31}
32
33impl<'a> CreateScheduledEvent<'a> {
34    /// Creates a builder with the provided kind, name, and start time, leaving all other fields
35    /// empty.
36    pub fn new(
37        kind: ScheduledEventType,
38        name: impl Into<String>,
39        scheduled_start_time: impl Into<Timestamp>,
40    ) -> Self {
41        Self {
42            name: name.into(),
43            entity_type: kind,
44            scheduled_start_time: scheduled_start_time.into().to_string(),
45
46            image: None,
47            channel_id: None,
48            description: None,
49            entity_metadata: None,
50            scheduled_end_time: None,
51
52            // Set the privacy level to `GUILD_ONLY`. As this is the only possible value of this
53            // field, it's onlyu used at event creation, and we don't even parse it into the
54            // `ScheduledEvent` struct.
55            privacy_level: ScheduledEventPrivacyLevel::GuildOnly,
56
57            audit_log_reason: None,
58        }
59    }
60
61    /// Sets the channel id of the scheduled event. Required if [`Self::kind`] is
62    /// [`ScheduledEventType::StageInstance`] or [`ScheduledEventType::Voice`].
63    pub fn channel_id<C: Into<ChannelId>>(mut self, channel_id: C) -> Self {
64        self.channel_id = Some(channel_id.into());
65        self
66    }
67
68    /// Sets the name of the scheduled event, replacing the current value as set in [`Self::new`].
69    pub fn name(mut self, name: impl Into<String>) -> Self {
70        self.name = name.into();
71        self
72    }
73
74    /// Sets the description of the scheduled event.
75    pub fn description(mut self, description: impl Into<String>) -> Self {
76        self.description = Some(description.into());
77        self
78    }
79
80    /// Sets the start time of the scheduled event, replacing the current value as set in
81    /// [`Self::new`].
82    pub fn start_time(mut self, timestamp: impl Into<Timestamp>) -> Self {
83        self.scheduled_start_time = timestamp.into().to_string();
84        self
85    }
86
87    /// Sets the end time of the scheduled event. Required if [`Self::kind`] is
88    /// [`ScheduledEventType::External`].
89    pub fn end_time(mut self, timestamp: impl Into<Timestamp>) -> Self {
90        self.scheduled_end_time = Some(timestamp.into().to_string());
91        self
92    }
93
94    /// Sets the entity type of the scheduled event, replacing the current value as set in
95    /// [`Self::new`].
96    pub fn kind(mut self, kind: ScheduledEventType) -> Self {
97        self.entity_type = kind;
98        self
99    }
100
101    /// Sets the location of the scheduled event. Required to be set and non-empty if
102    /// [`Self::kind`] is [`ScheduledEventType::External`].
103    ///
104    /// [`External`]: ScheduledEventType::External
105    pub fn location(mut self, location: impl Into<String>) -> Self {
106        self.entity_metadata = Some(ScheduledEventMetadata {
107            location: Some(location.into()),
108        });
109        self
110    }
111
112    /// Sets the cover image for the scheduled event.
113    pub fn image(mut self, image: &CreateAttachment) -> Self {
114        self.image = Some(image.to_base64());
115        self
116    }
117
118    /// Sets the request's audit log reason.
119    pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
120        self.audit_log_reason = Some(reason);
121        self
122    }
123}
124
125#[cfg(feature = "http")]
126#[async_trait::async_trait]
127impl Builder for CreateScheduledEvent<'_> {
128    type Context<'ctx> = GuildId;
129    type Built = ScheduledEvent;
130
131    /// Creates a new scheduled event in the guild with the data set, if any.
132    ///
133    /// **Note**: Requires the [Create Events] permission.
134    ///
135    /// # Errors
136    ///
137    /// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
138    /// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
139    ///
140    /// [Create Events]: Permissions::CREATE_EVENTS
141    async fn execute(
142        self,
143        cache_http: impl CacheHttp,
144        ctx: Self::Context<'_>,
145    ) -> Result<Self::Built> {
146        #[cfg(feature = "cache")]
147        crate::utils::user_has_guild_perms(&cache_http, ctx, Permissions::CREATE_EVENTS)?;
148
149        cache_http.http().create_scheduled_event(ctx, &self, self.audit_log_reason).await
150    }
151}