spectacles_model/
invite.rs

1//! Structures related to a Discord invite.
2use std::time::Duration;
3
4use chrono::{DateTime, FixedOffset};
5
6use crate::channel::Channel;
7use crate::guild::Guild;
8use crate::User;
9
10/// Represents a code that when used, adds a user to a guild or group DM channel.
11#[derive(Deserialize, Serialize, Clone, Debug)]
12pub struct Invite {
13    /// The ID of the invite code.
14    pub code: String,
15    /// The guild that the invite belongs to.
16    #[serde(default)]
17    pub guild: Option<Guild>,
18    /// The channel that the invite belongs to.
19    pub channel: Channel,
20    /// The approximate count of online members.
21    #[serde(default)]
22    pub approximate_presence_count: Option<i32>,
23    /// The approximate count of total members.
24    #[serde(default)]
25    pub approximate_member_count: Option<i32>
26}
27
28/// Represents a partial channel invite.
29#[derive(Deserialize, Serialize, Clone, Debug)]
30pub struct PartialInvite {
31    /// The code of the invite, if one is set.
32    pub code: String
33}
34
35
36/// Detailed information about an invite.
37#[derive(Deserialize, Serialize, Clone, Debug)]
38pub struct InviteMetadata {
39    /// The user who created the invite.
40    pub inviter: User,
41    /// The amount of times that this invite has been used.
42    pub uses: i32,
43    /// The maximum amount of uses allowed for this invite.
44    pub max_uses: i32,
45    /// The duration after which the invite expires, in seconds.
46    pub max_age: i32,
47    /// Whether or not this invite grants temporary membership.
48    pub temporary: bool,
49    /// The date that this invite was created.
50    pub created_at: DateTime<FixedOffset>,
51    /// Whether or not this invite has been revoked.
52    pub revoked: bool
53}
54
55/// Represents data that is sent to the create invite endpoint.
56#[derive(Deserialize, Serialize, Clone, Debug)]
57pub struct CreateInviteOptions {
58    max_age: u64,
59    max_uses: i32,
60    temporary: bool,
61    unique: bool,
62}
63
64impl CreateInviteOptions {
65    pub fn new() -> Self {
66        CreateInviteOptions {
67            max_age: 86400,
68            max_uses: 0,
69            temporary: false,
70            unique: false,
71        }
72    }
73
74    /// Sets the maximum age for this invite.
75    pub fn set_max_age(mut self, age: Duration) -> Self {
76        self.max_age = age.as_secs();
77        self
78    }
79
80    /// Sets the maximum # of uses for this invite.
81    pub fn set_max_uses(mut self, uses: i32) -> Self {
82        self.max_uses = uses;
83        self
84    }
85
86    /// Sets whether or not this invite should be temporary.
87    pub fn set_temporary(mut self, temp: bool) -> Self {
88        self.temporary = temp;
89        self
90    }
91
92    /// Sets whether or not this invite should be unique.
93    pub fn set_unique(mut self, unique: bool) -> Self {
94        self.unique = unique;
95        self
96    }
97}