spectacles_model/guild/
mod.rs

1//! Structures related to Discord guilds.
2use chrono::{DateTime, FixedOffset};
3use serde_repr::{Deserialize_repr, Serialize_repr};
4
5use crate::{
6    channel::Channel,
7    message::Emoji,
8    presence::Presence,
9    User,
10    voice::VoiceState,
11};
12use crate::snowflake::Snowflake;
13
14pub use self::{
15    audit_log::*,
16    member::*,
17    role::*
18};
19
20mod role;
21mod audit_log;
22mod member;
23
24/// A Discord Guild, commonly referred to as a "server".
25#[derive(Deserialize, Debug, Serialize, Clone)]
26pub struct Guild {
27    /// The snowflake ID of this guild.
28    pub id: Snowflake,
29    /// The name of the guild.
30    pub name: String,
31    /// The guild's icon hash. Will be a None value if one is not set.
32    pub icon: Option<String>,
33    /// The guild's splash hash. Will be a None value if it does not exist.
34    pub splash: Option<String>,
35    /// Whether or not the user is an owner of the guild.
36    pub owner: Option<bool>,
37    /// The ID of the guild owner.
38    pub owner_id: String,
39    /// The permissions that the user has in this guild.
40    #[serde(default)]
41    pub permissions: i32,
42    /// The region in which this guild is located.
43    pub region: String,
44    /// The AFK channel ID for this guild.
45    pub afk_channel_id: Option<String>,
46    /// The AFK channel timeout for this guild.
47    pub afk_timeout: Option<i32>,
48    /// Whether or not the guild can be embedded in a widget.
49    #[serde(default)]
50    pub embed_enabled: bool,
51    /// The channel ID that an embed widget will be generated for.
52    #[serde(default)]
53    pub embed_channel_id: String,
54    /// The amount of members that are currently in this guild.
55    #[serde(default)]
56    pub member_count: i32,
57    /// A list of features that this guild currently has.
58    pub features: Vec<String>,
59    /// A collection of roles that belong to this guild.
60    pub roles: Vec<Role>,
61    /// A collection of emotes that belong to this guild.
62    pub emojis: Vec<Emoji>,
63    /// The explicit content filter level for this guild.
64    pub explicit_content_filter: ExplicitContentFilter,
65    /// The ID of the application which created the guild, if applicable.
66    pub application_id: Option<String>,
67    /// The verification level, which determines which users can chat in a guild.
68    pub verification_level: VerificationLevel,
69    /// The MFA authentication level for this guild.
70    pub mfa_level: MfaLevel,
71    /// The ID of the channel in which system messages are sent to.
72    pub system_channel_id: Option<String>,
73    /// The time that this guild was joined.
74    #[serde(default)]
75    pub joined_at: String,
76    /// Whether this guild is considered a large guild by Discord.
77    #[serde(default)]
78    pub large: bool,
79    /// Whether or not this guild is available.
80    #[serde(default)]
81    pub unavailable: bool,
82    /// Whether or not the server widget is enabled.
83    #[serde(default)]
84    pub widget_enabled: bool,
85    /// The ID of the guild's widget channel, if one exists.
86    #[serde(default)]
87    pub widget_channel_id: String,
88    /// The default message notification setting for this guild.
89    pub default_message_notifications: DefaultMessageNotifications,
90    /// A collection of guild voice states.
91    pub voice_states: Vec<VoiceState>,
92    /// A collection of channels in this guild.
93    #[serde(default)]
94    pub channels: Vec<Channel>,
95    /// A collection of members in this guild.
96    #[serde(default)]
97    pub members: Vec<GuildMember>,
98    /// A collection of presences in this guild.
99    #[serde(default)]
100    pub presences: Option<Vec<Presence>>
101}
102
103/// A Partial guild object, usually an offline guild.
104#[derive(Serialize, Deserialize, Debug, Clone)]
105pub struct UnavailableGuild {
106    /// The guild ID of the guild.
107    pub id: Snowflake,
108    /// Whether or not the guild is available, usually set to true.
109    pub unavailable: bool
110}
111
112/// The embed object of a guild.
113#[derive(Serialize, Deserialize, Debug, Clone)]
114pub struct GuildEmbed {
115    /// Whether the embed is enabled.
116    pub enabled: bool,
117    /// The channel ID of the embed.
118    pub channel_id: Snowflake,
119}
120
121/// Options for modifying a guild embed.
122#[derive(Serialize, Deserialize, Debug, Clone)]
123pub struct ModifyGuildEmbedOptions {
124    #[serde(skip_serializing_if = "Option::is_none")]
125    enabled: Option<bool>,
126    #[serde(skip_serializing_if = "Option::is_none")]
127    channel_id: Option<Snowflake>,
128}
129
130impl ModifyGuildEmbedOptions {
131    /// Sets the enabled status of this embed.
132    pub fn enabled(mut self, opt: bool) -> Self {
133        self.enabled = Some(opt);
134        self
135    }
136
137    /// Sets the channel ID of the guild embed.
138    pub fn channel_id(mut self, id: Snowflake) -> Self {
139        self.channel_id = Some(id);
140        self
141    }
142}
143
144/// A body that can be sent to the Modify Guild endpoint.
145#[derive(Serialize, Deserialize, Debug, Clone)]
146pub struct ModifyGuildOptions {
147    #[serde(skip_serializing_if = "Option::is_none")]
148    name: Option<String>,
149    #[serde(skip_serializing_if = "Option::is_none")]
150    region: Option<String>,
151    #[serde(skip_serializing_if = "Option::is_none")]
152    verification_level: Option<VerificationLevel>,
153    #[serde(skip_serializing_if = "Option::is_none")]
154    default_message_notifications: Option<DefaultMessageNotifications>,
155    #[serde(skip_serializing_if = "Option::is_none")]
156    explicit_content_filter: Option<ExplicitContentFilter>,
157    #[serde(skip_serializing_if = "Option::is_none")]
158    afk_channel_id: Option<Snowflake>,
159    #[serde(skip_serializing_if = "Option::is_none")]
160    afk_timeout: Option<i32>,
161    #[serde(skip_serializing_if = "Option::is_none")]
162    icon: Option<String>,
163    #[serde(skip_serializing_if = "Option::is_none")]
164    owner: Option<Snowflake>,
165    #[serde(skip_serializing_if = "Option::is_none")]
166    splash: Option<String>,
167    #[serde(skip_serializing_if = "Option::is_none")]
168    system_channel_id: Option<Snowflake>,
169}
170
171impl ModifyGuildOptions {
172    /// Sets a new name for this guild.
173    pub fn name(mut self, name: &str) -> Self {
174        self.name = Some(name.to_string());
175        self
176    }
177
178    /// Sets a new region for this guild.
179    pub fn region(mut self, reg: &str) -> Self {
180        self.region = Some(reg.to_string());
181        self
182    }
183
184    /// Sets a new verification level for this guild.
185    pub fn verification_level(mut self, level: VerificationLevel) -> Self {
186        self.verification_level = Some(level);
187        self
188    }
189
190    /// Sets the default message notifications level for this guild.
191    pub fn default_message_notifications(mut self, level: DefaultMessageNotifications) -> Self {
192        self.default_message_notifications = Some(level);
193        self
194    }
195
196    /// Sets the explicit content filter for this guild.
197    pub fn explicit_content_filter(mut self, filter: ExplicitContentFilter) -> Self {
198        self.explicit_content_filter = Some(filter);
199        self
200    }
201
202    /// Sets the AFK channel for this guild.
203    pub fn afk_channel(mut self, id: Snowflake) -> Self {
204        self.afk_channel_id = Some(id);
205        self
206    }
207
208    /// Sets the AFK timeout for this guild.
209    pub fn afk_timeout(mut self, timeout: i32) -> Self {
210        self.afk_timeout = Some(timeout);
211        self
212    }
213
214    /// Sets the icon of this guild.
215    pub fn icon(mut self, url: &str) -> Self {
216        self.icon = Some(url.to_string());
217        self
218    }
219
220    /// Sets a new owner for this guild.
221    pub fn owner(mut self, id: Snowflake) -> Self {
222        self.owner = Some(id);
223        self
224    }
225
226    /// Sets the splash icon for this guild.
227    pub fn splash(mut self, url: &str) -> Self {
228        self.splash = Some(url.to_string());
229        self
230    }
231
232    /// Sets the system channel of this guild.
233    pub fn system_channel(mut self, chan: Snowflake) -> Self {
234        self.system_channel_id = Some(chan);
235        self
236    }
237}
238
239/// Information about a guild's prune status.
240#[derive(Deserialize, Debug, Clone)]
241pub struct GuildPrune {
242    /// The number of members that have been pruned.
243    pub pruned: i32
244}
245
246#[derive(Deserialize, Debug, Clone)]
247pub struct GuildIntegration {
248    /// The snowflake ID of this integration.
249    pub id: Snowflake,
250    /// The name of this integration.
251    pub name: String,
252    #[serde(rename = "type")]
253    pub kind: String,
254    /// Whether or not this integration is enabled.
255    pub enabled: bool,
256    /// Whether or not this integration is syncing.
257    pub syncing: bool,
258    /// The "subscribers" role for this integration.
259    pub role_id: Snowflake,
260    /// The behavior of expiring subscribers.
261    pub expire_behavior: i32,
262    /// The grace period before expiring subscribers.
263    pub expire_grace_period: i32,
264    /// The user of this integration.
265    pub user: User,
266    /// The integration account information.
267    pub account: IntegrationAccount,
268    /// When this integration was last synced.
269    pub synced_at: DateTime<FixedOffset>,
270}
271
272#[derive(Deserialize, Debug, Clone)]
273pub struct IntegrationAccount {
274    /// The ID of the account.
275    pub id: String,
276    /// The name of the account.
277    pub name: String,
278}
279
280/// Options for modifying a guild integration.
281#[derive(Clone, Debug, Deserialize, Serialize, Default)]
282pub struct ModifyGuildIntegrationOptions {
283    #[serde(skip_serializing_if = "Option::is_none")]
284    expire_behavior: Option<i32>,
285    #[serde(skip_serializing_if = "Option::is_none")]
286    expire_grace_period: Option<i32>,
287    #[serde(skip_serializing_if = "Option::is_none")]
288    enable_emoticons: Option<bool>,
289}
290
291impl ModifyGuildIntegrationOptions {
292    /// Sets the new expire behavior for this integration.
293    pub fn expire_behavior(mut self, beh: i32) -> Self {
294        self.expire_behavior = Some(beh);
295        self
296    }
297
298    /// Sets a new expire grace period for this integration.
299    pub fn expire_grace_period(mut self, per: i32) -> Self {
300        self.expire_grace_period = Some(per);
301        self
302    }
303
304    /// Sets whether emoticons should be synced for this integration.
305    pub fn enable_emoticons(mut self, opt: bool) -> Self {
306        self.enable_emoticons = Some(opt);
307        self
308    }
309}
310
311/// Options for creating a Discord guild.
312#[derive(Clone, Debug, Serialize, Default)]
313pub struct CreateGuildOptions {
314    #[serde(default)]
315    name: String,
316    #[serde(skip_serializing_if = "Option::is_none")]
317    region: Option<String>,
318    #[serde(skip_serializing_if = "Option::is_none")]
319    icon: Option<String>,
320    verification_level: Option<VerificationLevel>,
321    #[serde(skip_serializing_if = "Option::is_none")]
322    default_message_notifications: Option<DefaultMessageNotifications>,
323    #[serde(skip_serializing_if = "Option::is_none")]
324    explicit_content_filter: Option<ExplicitContentFilter>,
325}
326
327impl CreateGuildOptions {
328    /// Sets the name that the new guild should have. This field is required.
329    pub fn name(mut self, name: &str) -> Self {
330        self.name = name.to_string();
331        self
332    }
333
334    /// Sets the voice region name for the new guild.
335    pub fn voice_region(mut self, region: &str) -> Self {
336        self.region = Some(region.to_string());
337        self
338    }
339
340    /// Sets the icon URL for the new guild. Must be base64 encoded.
341    pub fn icon(mut self, icon: String) -> Self {
342        self.icon = Some(icon);
343        self
344    }
345
346    /// Sets the verification level for the guild.
347    pub fn verification_level(mut self, lvl: VerificationLevel) -> Self {
348        self.verification_level = Some(lvl);
349        self
350    }
351
352    /// Sets the default message notifications level for the new guild.
353    pub fn default_message_notifications(mut self, notifs: DefaultMessageNotifications) -> Self {
354        self.default_message_notifications = Some(notifs);
355        self
356    }
357
358    /// Sets the explicit content filter for the new guild.
359    pub fn explicit_content_filter(mut self, filter: ExplicitContentFilter) -> Self {
360        self.explicit_content_filter = Some(filter);
361        self
362    }
363}
364
365
366/// A guild ban object.
367#[derive(Deserialize, Debug, Clone)]
368pub struct GuildBan {
369    /// The reason for the ban, if applicable.
370    pub reason: Option<String>,
371    /// The user who was banned.
372    pub user: User,
373}
374
375/// Represents a packet received when a user is banned from a guild.
376#[derive(Deserialize, Debug, Clone)]
377pub struct GuildBanAdd {
378    /// The guild ID of the guild.
379    pub guild_id: Snowflake,
380    /// The user who was banned.
381    pub user: User
382}
383
384/// Represents a packet received when a user is unbanned from a guild.
385#[derive(Deserialize, Debug, Clone)]
386pub struct GuildBanRemove {
387    /// The guild ID of the guild.
388    pub guild_id: Snowflake,
389    /// The user who was unbanned.
390    pub user: User
391}
392
393/// Represents a packet received when a guild's emojis have been updated.
394#[derive(Deserialize, Debug, Clone)]
395pub struct GuildEmojisUpdate {
396    /// The guild ID of the guild.
397    pub guild_id: Snowflake,
398    /// An array of Emoji objects.
399    pub emojis: Vec<Emoji>,
400}
401
402/// Represents a packet sent when a guild integration is updated.
403#[derive(Deserialize, Debug, Clone)]
404pub struct GuildIntegrationsUpdate {
405    /// The guild ID of the guild.
406    pub guild_id: Snowflake
407}
408
409/// Represents a packet sent when a user is removed from a guild.
410#[derive(Deserialize, Debug, Clone)]
411pub struct GuildMemberRemove {
412    /// The guild ID of the guild.
413    pub guild_id: Snowflake,
414    /// The user who was removed.
415    pub user: User
416}
417
418/// Represents a packet sent when a guild member is updated.
419#[derive(Deserialize, Clone, Debug)]
420pub struct GuildMemberUpdate {
421    /// The ID of the guild.
422    pub guild_id: Snowflake,
423    pub roles: Vec<String>,
424    /// The user who was updated.
425    pub user: User,
426    /// The nickname of the user in the guild.
427    pub nick: String
428}
429
430/// Represents a response to a Guild Request Members packet.
431#[derive(Deserialize, Debug, Clone)]
432pub struct GuildMembersChunk {
433    /// The guild ID of the guild.
434    pub guild_id: Snowflake,
435    /// An array of guild member objects.
436    pub members: Vec<GuildMember>
437}
438
439/// Represents a packet sent when a role is created ina  guild.
440#[derive(Deserialize, Clone, Debug)]
441pub struct GuildRoleCreate {
442    /// The guild ID of the guild.
443    pub guild_id: Snowflake,
444    /// The newly created role.
445    pub role: Role
446}
447
448/// Represents a packet sent when a role is created in a guild.
449#[derive(Deserialize, Clone, Debug)]
450pub struct GuildRoleUpdate {
451    /// The guild ID of the guild.
452    pub guild_id: String,
453    /// The updated role.
454    pub role: Role
455}
456
457/// Represents a packet sent when a role is created in a guild.
458#[derive(Deserialize, Clone, Debug)]
459pub struct GuildRoleDelete {
460    /// The guild ID of the guild.
461    pub guild_id: Snowflake,
462    /// The ID of the deleted role.
463    pub role: Snowflake
464}
465
466/// A guild's explicit content filter levels.
467#[derive(Deserialize_repr, Debug, Serialize_repr, Clone)]
468#[repr(u8)]
469pub enum ExplicitContentFilter {
470    /// The filter is not active.
471    Disabled,
472    /// The filter is only active for members without roles.
473    MembersWithoutRoles,
474    /// The filter is active for all members.
475    AllMembers
476}
477
478/// A guild's MFA levels.
479#[derive(Deserialize_repr, Debug, Serialize_repr, Clone)]
480#[repr(u8)]
481pub enum MfaLevel {
482    /// The guild does not require MFA for elevated actions.
483    None,
484    /// The guild requires MFA on a user account which has elevated permissions.
485    Elevated,
486}
487
488/// A guild's default message notification setting.
489#[derive(Deserialize_repr, Debug, Serialize_repr, Clone)]
490#[repr(u8)]
491pub enum DefaultMessageNotifications {
492    /// A user will be notified whenever a new message is sent in the guild.
493    AllMessages,
494    /// A user will only be notified when they are mentioned.
495    OnlyMentions
496}
497
498/// A guild's verification levels.
499#[derive(Deserialize_repr, Debug, Clone, Serialize_repr)]
500#[repr(u8)]
501pub enum VerificationLevel {
502    /// The guild is unrestricted.
503    None,
504    /// The guild requires a verified email on the user's account.
505    Low,
506    /// The guild requires that the user be registered on Discord for longer than 5 minutes.
507    Medium,
508    /// The guild requires that the user be on the guild for longer than 10 minutes.
509    High,
510    /// The guild requires that the user have a verified phone number on their account.
511    Insane
512}