Skip to main content

djs_rest/endpoints/
guild_template.rs

1//! [Guild template endpoints](https://docs.discord.com/developers/resources/guild-template):
2//! snapshots of a guild's channels, roles and settings that others can create guilds from.
3//!
4//! Every endpoint except [`get_guild_template`](Rest::get_guild_template) needs
5//! `MANAGE_GUILD`.
6
7use djs_model::{
8    guild_template::GuildTemplate,
9    id::GuildId,
10    rest::{CreateGuildTemplate, ModifyGuildTemplate},
11};
12
13use crate::client::boxed;
14use crate::{Error, Request, Rest, Route};
15
16impl Rest {
17    /// [Get Guild Template](https://docs.discord.com/developers/resources/guild-template#get-guild-template):
18    /// resolves a template code (the part after `discord.new/`).
19    ///
20    /// discord.js counterpart: `Client#fetchGuildTemplate`.
21    ///
22    /// # Errors
23    ///
24    /// See [`Rest::request`]; `10057` Unknown Guild Template.
25    pub fn get_guild_template<'a>(
26        &'a self,
27        template_code: &'a str,
28    ) -> impl Future<Output = Result<GuildTemplate, Error>> + Send + 'a {
29        boxed(async move {
30            self.call(Request::new(Route::GetGuildTemplate { template_code }))
31                .await
32        })
33    }
34
35    /// [Get Guild Templates](https://docs.discord.com/developers/resources/guild-template#get-guild-templates):
36    /// returns the templates of a guild (at most one).
37    ///
38    /// discord.js counterpart: `Guild#fetchTemplates`.
39    ///
40    /// # Errors
41    ///
42    /// See [`Rest::request`].
43    pub fn get_guild_templates(
44        &self,
45        guild_id: GuildId,
46    ) -> impl Future<Output = Result<Vec<GuildTemplate>, Error>> + Send + '_ {
47        boxed(async move {
48            self.call(Request::new(Route::GetGuildTemplates { guild_id }))
49                .await
50        })
51    }
52
53    /// [Create Guild Template](https://docs.discord.com/developers/resources/guild-template#create-guild-template):
54    /// snapshots the guild into a new template.
55    ///
56    /// discord.js counterpart: `Guild#createTemplate`.
57    ///
58    /// # Examples
59    ///
60    /// ```no_run
61    /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId) -> Result<(), djs_rest::Error> {
62    /// use djs_model::rest::CreateGuildTemplate;
63    ///
64    /// let template = rest
65    ///     .create_guild_template(guild_id, &CreateGuildTemplate::new("Study group"))
66    ///     .await?;
67    /// println!("https://discord.new/{}", template.code);
68    /// # Ok(())
69    /// # }
70    /// ```
71    ///
72    /// # Errors
73    ///
74    /// See [`Rest::request`]; `30031` when the guild already has a template.
75    pub fn create_guild_template<'a>(
76        &'a self,
77        guild_id: GuildId,
78        body: &'a CreateGuildTemplate,
79    ) -> impl Future<Output = Result<GuildTemplate, Error>> + Send + 'a {
80        boxed(async move {
81            self.call(Request::new(Route::CreateGuildTemplate { guild_id }).json(body)?)
82                .await
83        })
84    }
85
86    /// [Sync Guild Template](https://docs.discord.com/developers/resources/guild-template#sync-guild-template):
87    /// updates the template's snapshot to the guild's current state.
88    ///
89    /// discord.js counterpart: `GuildTemplate#sync`.
90    ///
91    /// # Errors
92    ///
93    /// See [`Rest::request`]; `10057` Unknown Guild Template.
94    pub fn sync_guild_template<'a>(
95        &'a self,
96        guild_id: GuildId,
97        template_code: &'a str,
98    ) -> impl Future<Output = Result<GuildTemplate, Error>> + Send + 'a {
99        boxed(async move {
100            self.call(Request::new(Route::SyncGuildTemplate {
101                guild_id,
102                template_code,
103            }))
104            .await
105        })
106    }
107
108    /// [Modify Guild Template](https://docs.discord.com/developers/resources/guild-template#modify-guild-template):
109    /// changes the template's name or description.
110    ///
111    /// discord.js counterpart: `GuildTemplate#edit`.
112    ///
113    /// # Errors
114    ///
115    /// See [`Rest::request`]; `10057` Unknown Guild Template.
116    pub fn modify_guild_template<'a>(
117        &'a self,
118        guild_id: GuildId,
119        template_code: &'a str,
120        body: &'a ModifyGuildTemplate,
121    ) -> impl Future<Output = Result<GuildTemplate, Error>> + Send + 'a {
122        boxed(async move {
123            self.call(
124                Request::new(Route::ModifyGuildTemplate {
125                    guild_id,
126                    template_code,
127                })
128                .json(body)?,
129            )
130            .await
131        })
132    }
133
134    /// [Delete Guild Template](https://docs.discord.com/developers/resources/guild-template#delete-guild-template):
135    /// deletes the template and returns it.
136    ///
137    /// discord.js counterpart: `GuildTemplate#delete`.
138    ///
139    /// # Errors
140    ///
141    /// See [`Rest::request`]; `10057` Unknown Guild Template.
142    pub fn delete_guild_template<'a>(
143        &'a self,
144        guild_id: GuildId,
145        template_code: &'a str,
146    ) -> impl Future<Output = Result<GuildTemplate, Error>> + Send + 'a {
147        boxed(async move {
148            self.call(Request::new(Route::DeleteGuildTemplate {
149                guild_id,
150                template_code,
151            }))
152            .await
153        })
154    }
155}