djs_model/invite.rs
1//! [Invites](https://docs.discord.com/developers/resources/invite) to guilds, group DMs and
2//! voice channels.
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 application::PartialApplication,
9 channel::ChannelType,
10 guild::{AgeRestrictionLevel, Feature, RoleColors, VerificationLevel, WelcomeScreen},
11 guild_scheduled_event::GuildScheduledEvent,
12 id::{ChannelId, GuildId, RoleId},
13 macros::{int_enum, int_flags},
14 user::User,
15 util::{ImageHash, Timestamp},
16};
17
18/// An [invite](https://docs.discord.com/developers/resources/invite#invite-object) to a guild,
19/// group DM or friendship.
20///
21/// discord.js counterpart: `Invite`.
22#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23#[non_exhaustive]
24pub struct Invite {
25 /// What the invite is for.
26 #[serde(rename = "type")]
27 pub kind: InviteType,
28 /// The invite code (`discord.gg/<code>`).
29 pub code: CompactString,
30 /// The guild the invite is for.
31 #[serde(default, skip_serializing_if = "Option::is_none")]
32 pub guild: Option<Box<InviteGuild>>,
33 /// The channel the invite is for.
34 pub channel: Option<InviteChannel>,
35 /// Who created the invite.
36 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub inviter: Option<Box<User>>,
38 /// What a voice channel invite opens: a stream or an embedded application.
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub target_type: Option<InviteTargetType>,
41 /// Whose stream a stream invite shows.
42 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub target_user: Option<Box<User>>,
44 /// The embedded application an Activity invite opens.
45 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub target_application: Option<Box<PartialApplication>>,
47 /// The approximate number of online members (`with_counts=true`).
48 #[serde(default, skip_serializing_if = "Option::is_none")]
49 pub approximate_presence_count: Option<u32>,
50 /// The approximate number of members (`with_counts=true`).
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub approximate_member_count: Option<u32>,
53 /// When the invite expires; `None` for invites that never expire.
54 pub expires_at: Option<Timestamp>,
55 /// The scheduled event the invite links to.
56 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub guild_scheduled_event: Option<Box<GuildScheduledEvent>>,
58 /// The guild invite's flags.
59 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub flags: Option<InviteFlags>,
61 /// Roles given to the user who accepts the invite.
62 #[serde(default, skip_serializing_if = "Vec::is_empty")]
63 pub roles: Vec<InviteRole>,
64}
65
66/// An invite together with its [metadata](https://docs.discord.com/developers/resources/invite#invite-metadata-object),
67/// as returned by the endpoints that create or list invites of a guild or channel.
68///
69/// The metadata is optional (D-117): Get Guild Invites only includes it when the bot has
70/// `MANAGE_GUILD`. Dereferences to the [`Invite`].
71///
72/// discord.js counterpart: `Invite` (with `uses`, `maxUses`, …).
73#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
74#[non_exhaustive]
75#[doc(alias = "ExtendedInvite")]
76pub struct InviteWithMetadata {
77 /// The invite.
78 #[serde(flatten)]
79 pub invite: Invite,
80 /// The usage statistics, when Discord sends them.
81 #[serde(flatten)]
82 pub metadata: Option<InviteMetadata>,
83}
84
85/// The [metadata of an invite](https://docs.discord.com/developers/resources/invite#invite-metadata-object):
86/// how often it was used and for how long it is valid.
87///
88/// discord.js counterpart: `Invite#uses`, `#maxUses`, `#maxAge`, `#temporary`,
89/// `#createdTimestamp`.
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
91#[non_exhaustive]
92pub struct InviteMetadata {
93 /// How many times the invite was used.
94 pub uses: u32,
95 /// How many times the invite may be used; `0` means unlimited.
96 pub max_uses: u32,
97 /// How long the invite is valid, in seconds; `0` means forever.
98 pub max_age: u32,
99 /// Whether the invite only grants temporary membership.
100 pub temporary: bool,
101 /// When the invite was created.
102 pub created_at: Timestamp,
103}
104
105impl core::ops::Deref for InviteWithMetadata {
106 type Target = Invite;
107
108 fn deref(&self) -> &Invite {
109 &self.invite
110 }
111}
112
113int_enum! {
114 /// [What an invite is for](https://docs.discord.com/developers/resources/invite#invite-object-invite-types).
115 ///
116 /// discord.js counterpart: `InviteType`.
117 pub enum InviteType: u8 {
118 /// A guild.
119 Guild = 0,
120 /// A group DM.
121 GroupDm = 1,
122 /// A friend request.
123 Friend = 2,
124 }
125}
126
127int_enum! {
128 /// [What a voice channel invite opens](https://docs.discord.com/developers/resources/invite#invite-object-invite-target-types).
129 ///
130 /// discord.js counterpart: `InviteTargetType`.
131 pub enum InviteTargetType: u8 {
132 /// A user's stream.
133 Stream = 1,
134 /// An embedded application (Activity).
135 EmbeddedApplication = 2,
136 }
137}
138
139/// The partial guild of an [`Invite`] (D-051).
140///
141/// discord.js counterpart: `InviteGuild`.
142#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
143#[non_exhaustive]
144pub struct InviteGuild {
145 /// The guild's ID.
146 pub id: GuildId,
147 /// The guild's name.
148 pub name: CompactString,
149 /// The [invite splash hash](https://docs.discord.com/developers/reference#image-formatting).
150 pub splash: Option<ImageHash>,
151 /// The [banner hash](https://docs.discord.com/developers/reference#image-formatting).
152 pub banner: Option<ImageHash>,
153 /// The guild's description.
154 pub description: Option<CompactString>,
155 /// The [icon hash](https://docs.discord.com/developers/reference#image-formatting).
156 pub icon: Option<ImageHash>,
157 /// The guild's features.
158 pub features: Vec<Feature>,
159 /// The guild's verification level.
160 pub verification_level: VerificationLevel,
161 /// The vanity invite code.
162 pub vanity_url_code: Option<CompactString>,
163 /// The guild's age-restriction level.
164 #[serde(default)]
165 pub nsfw_level: AgeRestrictionLevel,
166 /// The number of boosts.
167 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub premium_subscription_count: Option<u32>,
169 /// The welcome screen of a Community guild.
170 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub welcome_screen: Option<WelcomeScreen>,
172}
173
174/// The partial channel of an [`Invite`] (D-051).
175///
176/// discord.js counterpart: `APIInviteChannel` (discord-api-types).
177#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
178#[non_exhaustive]
179pub struct InviteChannel {
180 /// The channel's ID.
181 pub id: ChannelId,
182 /// The kind of channel.
183 #[serde(rename = "type")]
184 pub kind: ChannelType,
185 /// The channel's name.
186 pub name: Option<CompactString>,
187 /// The [group DM icon hash](https://docs.discord.com/developers/reference#image-formatting).
188 #[serde(default, skip_serializing_if = "Option::is_none")]
189 pub icon: Option<ImageHash>,
190 /// The usernames of a group DM's members.
191 #[serde(default, skip_serializing_if = "Vec::is_empty")]
192 pub recipients: Vec<InviteRecipient>,
193}
194
195/// A member of the group DM an [`InviteChannel`] belongs to; only the username is known.
196///
197/// discord.js counterpart: a recipient of `APIInviteChannel` (discord-api-types).
198#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
199#[non_exhaustive]
200pub struct InviteRecipient {
201 /// The user's username.
202 pub username: CompactString,
203}
204
205/// A role given to users who accept an [`Invite`] (D-051).
206///
207/// discord.js counterpart: `InviteRole`.
208#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
209#[non_exhaustive]
210pub struct InviteRole {
211 /// The role's ID.
212 pub id: RoleId,
213 /// The role's name.
214 pub name: CompactString,
215 /// The role's position.
216 pub position: i32,
217 /// The role's primary color. **Deprecated** in favor of [`colors`](Self::colors).
218 pub color: u32,
219 /// The role's colors.
220 pub colors: RoleColors,
221 /// The [role icon hash](https://docs.discord.com/developers/reference#image-formatting).
222 pub icon: Option<ImageHash>,
223 /// The role's Unicode emoji.
224 pub unicode_emoji: Option<CompactString>,
225}
226
227int_flags! {
228 /// [Flags of a guild invite](https://docs.discord.com/developers/resources/invite#invite-object-guild-invite-flags).
229 ///
230 /// discord.js counterpart: `InviteFlags` / `InviteFlagsBitField`.
231 pub struct InviteFlags: u64 {
232 /// A guest invite: it only grants access to one voice channel.
233 const IS_GUEST_INVITE = 1 << 0;
234 }
235}
236
237/// The [state of the job](https://docs.discord.com/developers/resources/invite#get-target-users-job-status)
238/// that processes an invite's target users file, which Discord does asynchronously.
239///
240/// discord.js counterpart: `TargetUsersJobStatusForInvite`.
241#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
242#[non_exhaustive]
243pub struct InviteTargetUsersJobStatus {
244 /// Where the job is.
245 pub status: InviteTargetUsersJobStatusCode,
246 /// How many users the file lists.
247 pub total_users: u32,
248 /// How many of them were processed so far.
249 pub processed_users: u32,
250 /// When the job was created.
251 pub created_at: Timestamp,
252 /// When the job finished.
253 #[serde(default)]
254 pub completed_at: Option<Timestamp>,
255 /// Why the job failed.
256 #[serde(default, skip_serializing_if = "Option::is_none")]
257 pub error_message: Option<CompactString>,
258}
259
260int_enum! {
261 /// The [status](https://docs.discord.com/developers/resources/invite#get-target-users-job-status-status-codes)
262 /// of an [`InviteTargetUsersJobStatus`].
263 ///
264 /// discord.js counterpart: `InviteTargetUsersJobStatus`.
265 pub enum InviteTargetUsersJobStatusCode: u8 {
266 /// No status given.
267 Unspecified = 0,
268 /// The file is still being processed.
269 Processing = 1,
270 /// The file was processed.
271 Completed = 2,
272 /// Processing failed; see [`InviteTargetUsersJobStatus::error_message`].
273 Failed = 3,
274 }
275}