Skip to main content

djs_model/
guild_template.rs

1//! [Guild templates](https://docs.discord.com/developers/resources/guild-template): codes that
2//! create a new guild from a snapshot of an existing one.
3//!
4//! The snapshot ([`SerializedSourceGuild`]) does not use real IDs: roles and channels get small
5//! **placeholder IDs** (`0` is `@everyone`, channels count up from `1`) that other parts of the
6//! snapshot refer to (`parent_id`, permission overwrites, `system_channel_id`). Discord replaces
7//! them with real snowflakes when a guild is created from the template. They are therefore plain
8//! `u64`s here, not [`Id`](crate::id::Id)s, and the snapshot types are separate from
9//! [`Role`](crate::guild::Role) and [`Channel`](crate::channel::Channel) (D-059). The same format
10//! is used by the (deprecated) "create guild" REST body.
11
12use compact_str::CompactString;
13use serde::{Deserialize, Serialize};
14
15use crate::{
16    channel::{
17        AutoArchiveDuration, ChannelFlags, ChannelType, DefaultReaction, ForumLayoutType,
18        OverwriteType, SortOrderType, VideoQualityMode,
19    },
20    guild::{
21        DefaultMessageNotificationLevel, ExplicitContentFilterLevel, RoleColors,
22        SystemChannelFlags, VerificationLevel,
23    },
24    id::{EmojiId, GuildId, UserId},
25    permissions::Permissions,
26    user::User,
27    util::{ImageHash, Locale, Timestamp},
28};
29
30/// A [guild template](https://docs.discord.com/developers/resources/guild-template#guild-template-object).
31///
32/// discord.js counterpart: `GuildTemplate`.
33#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
34#[non_exhaustive]
35#[doc(alias = "Template")]
36pub struct GuildTemplate {
37    /// The template code (its unique ID), as in `discord.new/{code}`.
38    pub code: CompactString,
39    /// The template's name.
40    pub name: CompactString,
41    /// The template's description.
42    pub description: Option<CompactString>,
43    /// How many times the template has been used.
44    pub usage_count: u32,
45    /// The user who created the template.
46    pub creator_id: UserId,
47    /// The user who created the template.
48    pub creator: User,
49    /// When the template was created.
50    pub created_at: Timestamp,
51    /// When the template was last synced with its source guild.
52    pub updated_at: Timestamp,
53    /// The guild the template was made from.
54    pub source_guild_id: GuildId,
55    /// The snapshot of the source guild.
56    pub serialized_source_guild: SerializedSourceGuild,
57    /// Whether the source guild has changes that are not synced into the template yet.
58    pub is_dirty: Option<bool>,
59}
60
61/// The guild snapshot of a [`GuildTemplate`] (a partial guild with placeholder IDs, D-059).
62///
63/// Fields that the "create guild" format treats as optional are [`Option`]s here.
64///
65/// discord.js counterpart: `APITemplateSerializedSourceGuild` (discord-api-types).
66#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
67#[non_exhaustive]
68pub struct SerializedSourceGuild {
69    /// The guild's name.
70    pub name: CompactString,
71    /// The guild's description.
72    #[serde(default)]
73    pub description: Option<CompactString>,
74    /// The voice region. **Deprecated** by Discord: regions are set per voice channel.
75    #[serde(default, skip_serializing_if = "Option::is_none")]
76    pub region: Option<CompactString>,
77    /// The verification level.
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub verification_level: Option<VerificationLevel>,
80    /// The default message notification level.
81    #[serde(default, skip_serializing_if = "Option::is_none")]
82    pub default_message_notifications: Option<DefaultMessageNotificationLevel>,
83    /// The explicit content filter level.
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub explicit_content_filter: Option<ExplicitContentFilterLevel>,
86    /// The preferred locale of a Community guild.
87    pub preferred_locale: Locale,
88    /// The AFK timeout in seconds.
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub afk_timeout: Option<u32>,
91    /// The roles; the first one (placeholder ID `0`) is `@everyone`.
92    #[serde(default, skip_serializing_if = "Vec::is_empty")]
93    pub roles: Vec<TemplateRole>,
94    /// The channels; categories come before their children.
95    #[serde(default, skip_serializing_if = "Vec::is_empty")]
96    pub channels: Vec<TemplateChannel>,
97    /// The placeholder ID of the AFK channel.
98    #[serde(
99        default,
100        skip_serializing_if = "Option::is_none",
101        with = "crate::serde_util::option_placeholder_id"
102    )]
103    pub afk_channel_id: Option<u64>,
104    /// The placeholder ID of the system channel (welcome messages, boost notices).
105    #[serde(
106        default,
107        skip_serializing_if = "Option::is_none",
108        with = "crate::serde_util::option_placeholder_id"
109    )]
110    pub system_channel_id: Option<u64>,
111    /// The system channel flags.
112    #[serde(default, skip_serializing_if = "Option::is_none")]
113    pub system_channel_flags: Option<SystemChannelFlags>,
114    /// The [icon hash](https://docs.discord.com/developers/reference#image-formatting) of the
115    /// source guild.
116    #[serde(default)]
117    pub icon_hash: Option<ImageHash>,
118    /// Whether the boost progress bar is shown.
119    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
120    pub premium_progress_bar_enabled: bool,
121}
122
123/// A role in a [`SerializedSourceGuild`] (D-059).
124///
125/// discord.js counterpart: a role of `APITemplateSerializedSourceGuild` (discord-api-types).
126#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
127#[non_exhaustive]
128pub struct TemplateRole {
129    /// The placeholder ID (`0` is `@everyone`).
130    #[serde(with = "crate::serde_util::placeholder_id")]
131    pub id: u64,
132    /// The role's name.
133    pub name: CompactString,
134    /// The role's permissions.
135    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
136    pub permissions: Permissions,
137    /// The role's primary color as an RGB integer. **Deprecated** by Discord in favor of
138    /// [`colors`](Self::colors).
139    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
140    pub color: u32,
141    /// The role's colors.
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub colors: Option<RoleColors>,
144    /// Whether the role's members are listed separately.
145    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
146    pub hoist: bool,
147    /// The [role icon hash](https://docs.discord.com/developers/reference#image-formatting).
148    #[serde(default, skip_serializing_if = "Option::is_none")]
149    pub icon: Option<ImageHash>,
150    /// The role's Unicode emoji.
151    #[serde(default, skip_serializing_if = "Option::is_none")]
152    pub unicode_emoji: Option<CompactString>,
153    /// Whether everyone can mention the role.
154    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
155    pub mentionable: bool,
156}
157
158/// A channel in a [`SerializedSourceGuild`] (D-059).
159///
160/// discord.js counterpart: a channel of `APITemplateSerializedSourceGuild` (discord-api-types).
161#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
162#[non_exhaustive]
163pub struct TemplateChannel {
164    /// The placeholder ID.
165    #[serde(with = "crate::serde_util::placeholder_id")]
166    pub id: u64,
167    /// The kind of channel.
168    #[serde(rename = "type")]
169    pub kind: ChannelType,
170    /// The channel's name.
171    pub name: CompactString,
172    /// The sorting position.
173    #[serde(default)]
174    pub position: i32,
175    /// The channel's topic.
176    #[serde(default, skip_serializing_if = "Option::is_none")]
177    pub topic: Option<CompactString>,
178    /// The bitrate of a voice channel, in bits per second.
179    #[serde(default, skip_serializing_if = "Option::is_none")]
180    pub bitrate: Option<u32>,
181    /// The user limit of a voice channel (`0` = no limit).
182    #[serde(default, skip_serializing_if = "Option::is_none")]
183    pub user_limit: Option<u32>,
184    /// Whether the channel is age-restricted.
185    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
186    pub nsfw: bool,
187    /// The slowmode in seconds.
188    #[serde(default, skip_serializing_if = "Option::is_none")]
189    pub rate_limit_per_user: Option<u16>,
190    /// The placeholder ID of the parent category.
191    #[serde(
192        default,
193        skip_serializing_if = "Option::is_none",
194        with = "crate::serde_util::option_placeholder_id"
195    )]
196    pub parent_id: Option<u64>,
197    /// The permission overwrites (referring to placeholder role IDs).
198    #[serde(default, skip_serializing_if = "Vec::is_empty")]
199    pub permission_overwrites: Vec<TemplateOverwrite>,
200    /// The voice region of a voice channel (`None` = automatic).
201    #[serde(default, skip_serializing_if = "Option::is_none")]
202    pub rtc_region: Option<CompactString>,
203    /// The camera video quality of a voice channel.
204    #[serde(default, skip_serializing_if = "Option::is_none")]
205    pub video_quality_mode: Option<VideoQualityMode>,
206    /// The channel's flags.
207    #[serde(default, skip_serializing_if = "Option::is_none")]
208    pub flags: Option<ChannelFlags>,
209    /// The tags of a forum or media channel.
210    #[serde(
211        default,
212        skip_serializing_if = "Vec::is_empty",
213        deserialize_with = "crate::serde_util::null_as_default"
214    )]
215    pub available_tags: Vec<TemplateForumTag>,
216    /// The default auto-archive duration of new threads.
217    #[serde(default, skip_serializing_if = "Option::is_none")]
218    pub default_auto_archive_duration: Option<AutoArchiveDuration>,
219    /// The default reaction button of forum posts.
220    #[serde(default, skip_serializing_if = "Option::is_none")]
221    pub default_reaction_emoji: Option<DefaultReaction>,
222    /// The default slowmode of new threads, in seconds.
223    #[serde(default, skip_serializing_if = "Option::is_none")]
224    pub default_thread_rate_limit_per_user: Option<u16>,
225    /// The default sort order of forum posts.
226    #[serde(default, skip_serializing_if = "Option::is_none")]
227    pub default_sort_order: Option<SortOrderType>,
228    /// The default layout of a forum channel.
229    #[serde(default, skip_serializing_if = "Option::is_none")]
230    pub default_forum_layout: Option<ForumLayoutType>,
231}
232
233/// A permission overwrite in a [`TemplateChannel`] (D-059).
234///
235/// discord.js counterpart: a permission overwrite of `APITemplateSerializedSourceGuild` (discord-api-types).
236#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
237#[non_exhaustive]
238pub struct TemplateOverwrite {
239    /// The placeholder ID of the role (or the ID of the member).
240    #[serde(with = "crate::serde_util::placeholder_id")]
241    pub id: u64,
242    /// Whether [`id`](Self::id) is a role or a member.
243    #[serde(rename = "type")]
244    pub kind: OverwriteType,
245    /// The allowed permissions.
246    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
247    pub allow: Permissions,
248    /// The denied permissions.
249    #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
250    pub deny: Permissions,
251}
252
253/// A forum tag in a [`TemplateChannel`]: like a [`ForumTag`](crate::channel::ForumTag), but
254/// without an ID (D-059).
255///
256/// discord.js counterpart: a forum tag of `APITemplateSerializedSourceGuild` (discord-api-types).
257#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
258#[non_exhaustive]
259pub struct TemplateForumTag {
260    /// The tag's name.
261    pub name: CompactString,
262    /// Whether only members with `MANAGE_THREADS` can add the tag.
263    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
264    pub moderated: bool,
265    /// The tag's custom emoji.
266    #[serde(default, skip_serializing_if = "Option::is_none")]
267    pub emoji_id: Option<EmojiId>,
268    /// The tag's Unicode emoji.
269    #[serde(default, skip_serializing_if = "Option::is_none")]
270    pub emoji_name: Option<CompactString>,
271}