djs_rest/endpoints/sticker.rs
1//! [Sticker endpoints](https://docs.discord.com/developers/resources/sticker): any sticker,
2//! Discord's sticker packs, and a guild's custom stickers.
3
4use djs_model::{
5 id::{GuildId, StickerId, StickerPackId},
6 rest::{CreateGuildSticker, ModifyGuildSticker},
7 sticker::{Sticker, StickerPack},
8};
9use serde::Deserialize;
10
11use crate::client::boxed;
12use crate::{Error, Request, Rest, Route};
13
14/// The response of List Sticker Packs, unwrapped by the method (C-053).
15#[derive(Deserialize)]
16struct StickerPacks {
17 sticker_packs: Vec<StickerPack>,
18}
19
20/// Stickers and sticker packs.
21impl Rest {
22 /// [Get Sticker](https://docs.discord.com/developers/resources/sticker#get-sticker):
23 /// returns any sticker by ID — a standard sticker of a pack or a guild sticker.
24 ///
25 /// discord.js counterpart: `Client#fetchSticker`.
26 ///
27 /// # Errors
28 ///
29 /// See [`Rest::request`]; `10060` Unknown Sticker.
30 pub fn get_sticker(
31 &self,
32 sticker_id: StickerId,
33 ) -> impl Future<Output = Result<Sticker, Error>> + Send + '_ {
34 boxed(async move {
35 self.call(Request::new(Route::GetSticker { sticker_id }))
36 .await
37 })
38 }
39
40 /// [List Sticker Packs](https://docs.discord.com/developers/resources/sticker#list-sticker-packs):
41 /// returns Discord's standard sticker packs. Discord wraps the list in an object; it is
42 /// unwrapped.
43 ///
44 /// discord.js counterpart: `Client#fetchStickerPacks()`.
45 ///
46 /// # Errors
47 ///
48 /// See [`Rest::request`].
49 pub fn list_sticker_packs(
50 &self,
51 ) -> impl Future<Output = Result<Vec<StickerPack>, Error>> + Send + '_ {
52 boxed(async move {
53 let list: StickerPacks = self.call(Request::new(Route::ListStickerPacks)).await?;
54 Ok(list.sticker_packs)
55 })
56 }
57
58 /// [Get Sticker Pack](https://docs.discord.com/developers/resources/sticker#get-sticker-pack):
59 /// returns one of Discord's standard sticker packs.
60 ///
61 /// discord.js counterpart: `Client#fetchStickerPacks({ packId })`.
62 ///
63 /// # Errors
64 ///
65 /// See [`Rest::request`]; `10061` Unknown Sticker Pack.
66 pub fn get_sticker_pack(
67 &self,
68 pack_id: StickerPackId,
69 ) -> impl Future<Output = Result<StickerPack, Error>> + Send + '_ {
70 boxed(async move {
71 self.call(Request::new(Route::GetStickerPack { pack_id }))
72 .await
73 })
74 }
75}
76
77/// Guild stickers.
78impl Rest {
79 /// [List Guild Stickers](https://docs.discord.com/developers/resources/sticker#list-guild-stickers):
80 /// returns every custom sticker of a guild. [`Sticker::user`] is included with the
81 /// `CREATE_GUILD_EXPRESSIONS` or `MANAGE_GUILD_EXPRESSIONS` permission.
82 ///
83 /// discord.js counterpart: `GuildStickerManager#fetch()`.
84 ///
85 /// # Errors
86 ///
87 /// See [`Rest::request`].
88 pub fn list_guild_stickers(
89 &self,
90 guild_id: GuildId,
91 ) -> impl Future<Output = Result<Vec<Sticker>, Error>> + Send + '_ {
92 boxed(async move {
93 self.call(Request::new(Route::ListGuildStickers { guild_id }))
94 .await
95 })
96 }
97
98 /// [Get Guild Sticker](https://docs.discord.com/developers/resources/sticker#get-guild-sticker):
99 /// returns one custom sticker of a guild.
100 ///
101 /// discord.js counterpart: `GuildStickerManager#fetch(id)`.
102 ///
103 /// # Errors
104 ///
105 /// See [`Rest::request`]; `10060` Unknown Sticker.
106 pub fn get_guild_sticker(
107 &self,
108 guild_id: GuildId,
109 sticker_id: StickerId,
110 ) -> impl Future<Output = Result<Sticker, Error>> + Send + '_ {
111 boxed(async move {
112 self.call(Request::new(Route::GetGuildSticker {
113 guild_id,
114 sticker_id,
115 }))
116 .await
117 })
118 }
119
120 /// [Create Guild Sticker](https://docs.discord.com/developers/resources/sticker#create-guild-sticker):
121 /// uploads a custom sticker. Needs `CREATE_GUILD_EXPRESSIONS`; a guild has 5 sticker
122 /// slots, and more with each boost level.
123 ///
124 /// The body is sent as `multipart/form-data` fields (`name`, `description`, `tags`, `file`).
125 /// `reason` is shown in the audit log (D-086).
126 ///
127 /// discord.js counterpart: `GuildStickerManager#create`.
128 ///
129 /// # Examples
130 ///
131 /// ```no_run
132 /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId) -> Result<(), Box<dyn std::error::Error>> {
133 /// use djs_model::rest::{CreateGuildSticker, FileUpload};
134 ///
135 /// let file = FileUpload::new("wave.png", std::fs::read("wave.png")?);
136 /// let mut body = CreateGuildSticker::new("Wave", "wave", file);
137 /// body.description = "Say hello".into();
138 /// let sticker = rest.create_guild_sticker(guild_id, &body, None).await?;
139 /// println!("Created sticker {}", sticker.id);
140 /// # Ok(())
141 /// # }
142 /// ```
143 ///
144 /// # Errors
145 ///
146 /// See [`Rest::request`]; `30039` when the guild has no free sticker slot; `50045` when
147 /// the file is too large; `50081` for an invalid file.
148 pub fn create_guild_sticker<'a>(
149 &'a self,
150 guild_id: GuildId,
151 body: &'a CreateGuildSticker,
152 reason: Option<&'a str>,
153 ) -> impl Future<Output = Result<Sticker, Error>> + Send + 'a {
154 boxed(async move {
155 let fields = [
156 ("name", body.name.as_str()),
157 ("description", body.description.as_str()),
158 ("tags", body.tags.as_str()),
159 ];
160 self.call(
161 Request::new(Route::CreateGuildSticker { guild_id })
162 .form_file("file", &body.file)
163 .form_text(&fields)
164 .reason(reason),
165 )
166 .await
167 })
168 }
169
170 /// [Modify Guild Sticker](https://docs.discord.com/developers/resources/sticker#modify-guild-sticker):
171 /// changes a sticker's name, description or tags. Needs `MANAGE_GUILD_EXPRESSIONS`, or
172 /// `CREATE_GUILD_EXPRESSIONS` for stickers the bot created.
173 ///
174 /// `reason` is shown in the audit log (D-086).
175 ///
176 /// discord.js counterpart: `Sticker#edit`, `GuildStickerManager#edit`.
177 ///
178 /// # Errors
179 ///
180 /// See [`Rest::request`]; `10060` Unknown Sticker.
181 pub fn modify_guild_sticker<'a>(
182 &'a self,
183 guild_id: GuildId,
184 sticker_id: StickerId,
185 body: &'a ModifyGuildSticker,
186 reason: Option<&'a str>,
187 ) -> impl Future<Output = Result<Sticker, Error>> + Send + 'a {
188 boxed(async move {
189 self.call(
190 Request::new(Route::ModifyGuildSticker {
191 guild_id,
192 sticker_id,
193 })
194 .json(body)?
195 .reason(reason),
196 )
197 .await
198 })
199 }
200
201 /// [Delete Guild Sticker](https://docs.discord.com/developers/resources/sticker#delete-guild-sticker):
202 /// deletes a custom sticker. Needs `MANAGE_GUILD_EXPRESSIONS`, or
203 /// `CREATE_GUILD_EXPRESSIONS` for stickers the bot created.
204 ///
205 /// `reason` is shown in the audit log (D-086).
206 ///
207 /// discord.js counterpart: `Sticker#delete`, `GuildStickerManager#delete`.
208 ///
209 /// # Errors
210 ///
211 /// See [`Rest::request`]; `10060` Unknown Sticker.
212 pub fn delete_guild_sticker<'a>(
213 &'a self,
214 guild_id: GuildId,
215 sticker_id: StickerId,
216 reason: Option<&'a str>,
217 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
218 boxed(async move {
219 self.call(
220 Request::new(Route::DeleteGuildSticker {
221 guild_id,
222 sticker_id,
223 })
224 .reason(reason),
225 )
226 .await
227 })
228 }
229}