Skip to main content

djs_model/
guild_scheduled_event.rs

1//! [Guild scheduled events](https://docs.discord.com/developers/resources/guild-scheduled-event):
2//! events planned in a stage channel, a voice channel or somewhere else.
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    guild::PartialMember,
9    id::{ChannelId, GenericId, GuildId, ScheduledEventId, UserId},
10    macros::int_enum,
11    user::User,
12    util::{ImageHash, Timestamp},
13};
14
15/// A [scheduled event](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-object)
16/// of a guild.
17///
18/// Which location fields are set depends on [`entity_type`](Self::entity_type): stage and
19/// voice events have a [`channel_id`](Self::channel_id); external events have
20/// [`entity_metadata`](Self::entity_metadata) with a location and a
21/// [`scheduled_end_time`](Self::scheduled_end_time).
22///
23/// discord.js counterpart: `GuildScheduledEvent`.
24#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25#[non_exhaustive]
26pub struct GuildScheduledEvent {
27    /// The event's ID.
28    pub id: ScheduledEventId,
29    /// The guild the event belongs to.
30    pub guild_id: GuildId,
31    /// The stage or voice channel the event takes place in; `None` for external events.
32    pub channel_id: Option<ChannelId>,
33    /// Who created the event; `None` for events created before October 25th, 2021.
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub creator_id: Option<UserId>,
36    /// The event's name (1-100 characters).
37    pub name: CompactString,
38    /// The event's description (1-1000 characters).
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub description: Option<CompactString>,
41    /// When the event starts.
42    pub scheduled_start_time: Timestamp,
43    /// When the event ends; required for external events.
44    pub scheduled_end_time: Option<Timestamp>,
45    /// Who can see the event.
46    pub privacy_level: PrivacyLevel,
47    /// Whether the event is scheduled, running, finished or canceled.
48    pub status: Status,
49    /// Where the event takes place.
50    pub entity_type: EntityType,
51    /// The ID of the entity associated with the event (e.g. the stage instance).
52    pub entity_id: Option<GenericId>,
53    /// Extra data for external events.
54    pub entity_metadata: Option<EntityMetadata>,
55    /// The user who created the event.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub creator: Option<Box<User>>,
58    /// How many users are interested in the event (with `with_user_count=true`).
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub user_count: Option<u32>,
61    /// The [cover image hash](https://docs.discord.com/developers/reference#image-formatting).
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub image: Option<ImageHash>,
64    /// How the event repeats; `None` for one-time events.
65    pub recurrence_rule: Option<Box<RecurrenceRule>>,
66}
67
68int_enum! {
69    /// [Who can see](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-privacy-level)
70    /// a scheduled event.
71    ///
72    /// discord.js counterpart: `GuildScheduledEventPrivacyLevel`.
73    #[doc(alias = "GuildScheduledEventPrivacyLevel")]
74    pub enum PrivacyLevel: u8 {
75        /// Only guild members.
76        GuildOnly = 2,
77    }
78}
79
80int_enum! {
81    /// [Where](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-entity-types)
82    /// a scheduled event takes place.
83    ///
84    /// discord.js counterpart: `GuildScheduledEventEntityType`.
85    #[doc(alias = "GuildScheduledEventEntityType")]
86    pub enum EntityType: u8 {
87        /// In a stage channel.
88        StageInstance = 1,
89        /// In a voice channel.
90        Voice = 2,
91        /// Outside Discord (or in a text channel); has a location.
92        External = 3,
93    }
94}
95
96int_enum! {
97    /// The [status](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-status)
98    /// of a scheduled event.
99    ///
100    /// A scheduled event can become active or canceled; an active event can only become
101    /// completed. Completed and canceled events cannot change any more.
102    ///
103    /// discord.js counterpart: `GuildScheduledEventStatus`.
104    #[doc(alias = "GuildScheduledEventStatus")]
105    pub enum Status: u8 {
106        /// Not started yet.
107        Scheduled = 1,
108        /// Running.
109        Active = 2,
110        /// Finished.
111        Completed = 3,
112        /// Canceled.
113        Canceled = 4,
114    }
115}
116
117/// [Extra data](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-entity-metadata)
118/// of an external scheduled event.
119///
120/// discord.js counterpart: `APIGuildScheduledEventEntityMetadata` (discord-api-types).
121#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
122#[non_exhaustive]
123pub struct EntityMetadata {
124    /// Where the event takes place (1-100 characters); required for external events.
125    #[serde(default, skip_serializing_if = "Option::is_none")]
126    pub location: Option<CompactString>,
127}
128
129impl EntityMetadata {
130    /// The metadata of an external event taking place at `location` (1-100 characters).
131    ///
132    /// discord.js counterpart: the `entityMetadata` of `GuildScheduledEventCreateOptions`.
133    #[must_use]
134    pub fn new(location: impl Into<CompactString>) -> Self {
135        Self {
136            location: Some(location.into()),
137        }
138    }
139}
140
141/// A [user interested in a scheduled event](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-user-object).
142///
143/// discord.js counterpart: `GuildScheduledEventUser`.
144#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
145#[non_exhaustive]
146pub struct GuildScheduledEventUser {
147    /// The event the user is interested in.
148    pub guild_scheduled_event_id: ScheduledEventId,
149    /// The user.
150    pub user: User,
151    /// The user's member data in the event's guild (with `with_member=true`), without the
152    /// user, which is [`user`](Self::user); combine them with [`PartialMember::into_member`].
153    #[serde(default, skip_serializing_if = "Option::is_none")]
154    pub member: Option<Box<PartialMember>>,
155}
156
157/// [How a scheduled event repeats](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-recurrence-rule-object),
158/// modelled on the iCalendar RRULE.
159///
160/// Discord only accepts a subset of combinations (for example weekly events on one weekday, or
161/// monthly events on the n-th weekday); see the documentation for the allowed shapes. `end`,
162/// `by_year_day` and `count` are set by Discord and cannot be sent.
163///
164/// discord.js counterpart: `GuildScheduledEventRecurrenceRule`.
165#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
166#[non_exhaustive]
167pub struct RecurrenceRule {
168    /// When the recurrence interval starts.
169    pub start: Timestamp,
170    /// When the recurrence interval ends.
171    pub end: Option<Timestamp>,
172    /// How often the event repeats.
173    pub frequency: RecurrenceFrequency,
174    /// The spacing between events in units of `frequency` (e.g. 2 with `Weekly` = every other
175    /// week).
176    pub interval: u16,
177    /// The weekdays to repeat on.
178    pub by_weekday: Option<Vec<RecurrenceWeekday>>,
179    /// The n-th weekdays of the month to repeat on.
180    pub by_n_weekday: Option<Vec<RecurrenceNWeekday>>,
181    /// The months to repeat in.
182    pub by_month: Option<Vec<RecurrenceMonth>>,
183    /// The days of the month to repeat on.
184    pub by_month_day: Option<Vec<u8>>,
185    /// The days of the year to repeat on (1-364).
186    pub by_year_day: Option<Vec<u16>>,
187    /// How many times the event may repeat before stopping.
188    pub count: Option<u32>,
189}
190
191int_enum! {
192    /// [How often](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-recurrence-rule-object-guild-scheduled-event-recurrence-rule-frequency)
193    /// a scheduled event repeats.
194    ///
195    /// discord.js counterpart: `GuildScheduledEventRecurrenceRuleFrequency`.
196    pub enum RecurrenceFrequency: u8 {
197        /// Every year.
198        Yearly = 0,
199        /// Every month.
200        Monthly = 1,
201        /// Every week.
202        Weekly = 2,
203        /// Every day.
204        Daily = 3,
205    }
206}
207
208int_enum! {
209    /// A [day of the week](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-recurrence-rule-object-guild-scheduled-event-recurrence-rule-weekday)
210    /// in a [`RecurrenceRule`].
211    ///
212    /// discord.js counterpart: `GuildScheduledEventRecurrenceRuleWeekday`.
213    pub enum RecurrenceWeekday: u8 {
214        /// Monday.
215        Monday = 0,
216        /// Tuesday.
217        Tuesday = 1,
218        /// Wednesday.
219        Wednesday = 2,
220        /// Thursday.
221        Thursday = 3,
222        /// Friday.
223        Friday = 4,
224        /// Saturday.
225        Saturday = 5,
226        /// Sunday.
227        Sunday = 6,
228    }
229}
230
231/// The [n-th weekday of a month](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-recurrence-rule-object-guild-scheduled-event-recurrence-rule-nweekday-structure),
232/// e.g. the second Tuesday.
233///
234/// discord.js counterpart: `APIGuildScheduledEventRecurrenceRuleNWeekday` (discord-api-types).
235#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
236#[non_exhaustive]
237pub struct RecurrenceNWeekday {
238    /// The week of the month (1-5).
239    pub n: u8,
240    /// The day of that week.
241    pub day: RecurrenceWeekday,
242}
243
244impl RecurrenceNWeekday {
245    /// The `n`-th (1-5) `day` of a month, e.g. `new(2, RecurrenceWeekday::Tuesday)` for the
246    /// second Tuesday.
247    ///
248    /// discord.js counterpart: `GuildScheduledEventRecurrenceRuleNWeekday`.
249    #[must_use]
250    pub const fn new(n: u8, day: RecurrenceWeekday) -> Self {
251        Self { n, day }
252    }
253}
254
255int_enum! {
256    /// A [month](https://docs.discord.com/developers/resources/guild-scheduled-event#guild-scheduled-event-recurrence-rule-object-guild-scheduled-event-recurrence-rule-month)
257    /// in a [`RecurrenceRule`].
258    ///
259    /// discord.js counterpart: `GuildScheduledEventRecurrenceRuleMonth`.
260    pub enum RecurrenceMonth: u8 {
261        /// January.
262        January = 1,
263        /// February.
264        February = 2,
265        /// March.
266        March = 3,
267        /// April.
268        April = 4,
269        /// May.
270        May = 5,
271        /// June.
272        June = 6,
273        /// July.
274        July = 7,
275        /// August.
276        August = 8,
277        /// September.
278        September = 9,
279        /// October.
280        October = 10,
281        /// November.
282        November = 11,
283        /// December.
284        December = 12,
285    }
286}