titanium_model/
scheduled.rs

1//! Scheduled event types for Discord guild events.
2//!
3//! Scheduled events allow guilds to plan activities.
4
5use crate::Snowflake;
6use crate::TitanString;
7use serde::{Deserialize, Serialize};
8use serde_repr::{Deserialize_repr, Serialize_repr};
9
10/// A scheduled event in a guild.
11#[derive(Debug, Clone, Deserialize, Serialize)]
12pub struct ScheduledEvent<'a> {
13    /// The ID of the scheduled event.
14    pub id: Snowflake,
15
16    /// The guild ID which the scheduled event belongs to.
17    pub guild_id: Snowflake,
18
19    /// The channel ID in which the event will be hosted (if applicable).
20    #[serde(default)]
21    pub channel_id: Option<Snowflake>,
22
23    /// The ID of the user that created the scheduled event.
24    #[serde(default)]
25    pub creator_id: Option<Snowflake>,
26
27    /// The name of the scheduled event (1-100 characters).
28    pub name: TitanString<'a>,
29
30    /// The description of the scheduled event (1-1000 characters).
31    #[serde(default)]
32    pub description: Option<TitanString<'a>>,
33
34    /// The time the scheduled event will start (ISO8601 timestamp).
35    pub scheduled_start_time: TitanString<'a>,
36
37    /// The time the scheduled event will end (ISO8601 timestamp).
38    #[serde(default)]
39    pub scheduled_end_time: Option<TitanString<'a>>,
40
41    /// The privacy level of the scheduled event.
42    pub privacy_level: ScheduledEventPrivacyLevel,
43
44    /// The status of the scheduled event.
45    pub status: ScheduledEventStatus,
46
47    /// The type of the scheduled event.
48    pub entity_type: ScheduledEventEntityType,
49
50    /// The ID of an entity associated with a guild scheduled event.
51    #[serde(default)]
52    pub entity_id: Option<Snowflake>,
53
54    /// Additional metadata for the guild scheduled event.
55    #[serde(default)]
56    pub entity_metadata: Option<ScheduledEventEntityMetadata<'a>>,
57
58    /// The user that created the scheduled event.
59    #[serde(default)]
60    pub creator: Option<super::User<'a>>,
61
62    /// The number of users subscribed to the scheduled event.
63    #[serde(default)]
64    pub user_count: Option<u32>,
65
66    /// The cover image hash of the scheduled event.
67    #[serde(default)]
68    pub image: Option<TitanString<'a>>,
69}
70
71/// Privacy level of a scheduled event.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr, Default)]
73#[repr(u8)]
74pub enum ScheduledEventPrivacyLevel {
75    /// The scheduled event is only accessible to guild members.
76    #[default]
77    GuildOnly = 2,
78}
79
80/// Status of a scheduled event.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
82#[repr(u8)]
83pub enum ScheduledEventStatus {
84    /// The event is scheduled.
85    Scheduled = 1,
86    /// The event is active/started.
87    Active = 2,
88    /// The event has completed.
89    Completed = 3,
90    /// The event was cancelled.
91    Cancelled = 4,
92}
93
94/// Entity type of a scheduled event.
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr, Default)]
96#[repr(u8)]
97pub enum ScheduledEventEntityType {
98    /// A stage instance.
99    StageInstance = 1,
100    /// A voice channel.
101    #[default]
102    Voice = 2,
103    /// An external location.
104    External = 3,
105}
106
107/// Metadata for scheduled event entities.
108#[derive(Debug, Clone, Default, Deserialize, Serialize)]
109pub struct ScheduledEventEntityMetadata<'a> {
110    /// Location of the event (required for External events).
111    #[serde(default)]
112    pub location: Option<TitanString<'a>>,
113}
114
115/// Event data for scheduled event user add/remove.
116#[derive(Debug, Clone, Deserialize, Serialize)]
117pub struct ScheduledEventUserEvent {
118    /// The ID of the scheduled event.
119    pub guild_scheduled_event_id: Snowflake,
120
121    /// The ID of the user.
122    pub user_id: Snowflake,
123
124    /// The ID of the guild.
125    pub guild_id: Snowflake,
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn test_scheduled_event() {
134        let json = r#"{
135            "id": "123",
136            "guild_id": "456",
137            "name": "Community Event",
138            "scheduled_start_time": "2024-01-01T00:00:00.000Z",
139            "privacy_level": 2,
140            "status": 1,
141            "entity_type": 2
142        }"#;
143
144        let event: ScheduledEvent = crate::json::from_str(json).unwrap();
145        assert_eq!(event.name, TitanString::Borrowed("Community Event"));
146        assert_eq!(event.status, ScheduledEventStatus::Scheduled);
147    }
148}