titanium_model/
invite.rs

1//! Invite types for Discord guild invites.
2//!
3//! Invites allow users to join guilds or group DMs.
4
5use crate::Snowflake;
6use crate::TitanString;
7use serde::{Deserialize, Serialize};
8
9/// Event data for INVITE_CREATE.
10#[derive(Debug, Clone, Deserialize, Serialize)]
11pub struct InviteCreateEvent<'a> {
12    /// The channel the invite is for.
13    pub channel_id: Snowflake,
14
15    /// The unique invite code.
16    #[serde(default)]
17    pub code: TitanString<'a>,
18
19    /// The time at which the invite was created (ISO8601 timestamp).
20    #[serde(default)]
21    pub created_at: TitanString<'a>,
22
23    /// The guild of the invite.
24    #[serde(default)]
25    pub guild_id: Option<Snowflake>,
26
27    /// The user that created the invite.
28    #[serde(default)]
29    pub inviter: Option<super::User<'a>>,
30
31    /// How long the invite is valid for (in seconds).
32    pub max_age: u32,
33
34    /// The maximum number of times the invite can be used.
35    pub max_uses: u32,
36
37    /// The target type for this voice channel invite.
38    #[serde(default)]
39    pub target_type: Option<u8>,
40
41    /// The user whose stream to display for voice channel stream invites.
42    #[serde(default)]
43    pub target_user: Option<super::User<'a>>,
44
45    /// The embedded application for voice channel invites.
46    #[serde(default)]
47    pub target_application: Option<super::Application>,
48
49    /// Whether or not the invite is temporary.
50    pub temporary: bool,
51
52    /// How many times the invite has been used.
53    pub uses: u32,
54}
55
56/// Event data for INVITE_DELETE.
57#[derive(Debug, Clone, Deserialize, Serialize)]
58pub struct InviteDeleteEvent<'a> {
59    /// The channel of the invite.
60    pub channel_id: Snowflake,
61
62    /// The guild of the invite.
63    #[serde(default)]
64    pub guild_id: Option<Snowflake>,
65
66    /// The unique invite code.
67    #[serde(default)]
68    pub code: TitanString<'a>,
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_invite_create_event() {
77        let json = r#"{
78            "channel_id": "123",
79            "code": "abcdef",
80            "created_at": "2021-01-01T00:00:00.000Z",
81            "max_age": 86400,
82            "max_uses": 0,
83            "temporary": false,
84            "uses": 0
85        }"#;
86
87        let event: InviteCreateEvent = crate::json::from_str(json).unwrap();
88        assert_eq!(event.code, "abcdef");
89        assert_eq!(event.max_age, 86400);
90    }
91}