Skip to main content

djs_rest/endpoints/
emoji.rs

1//! [Emoji endpoints](https://docs.discord.com/developers/resources/emoji): a guild's custom
2//! emojis and the application's own emojis.
3//!
4//! Discord limits the guild emoji routes per guild, separately from the usual buckets, so the
5//! rate-limit headers may be inaccurate and an unexpected `429` is retried as usual.
6
7use djs_model::{
8    emoji::Emoji,
9    id::{ApplicationId, EmojiId, GuildId},
10    rest::{CreateApplicationEmoji, CreateGuildEmoji, ModifyApplicationEmoji, ModifyGuildEmoji},
11};
12
13use super::Items;
14use crate::client::boxed;
15use crate::{Error, Request, Rest, Route};
16
17/// Guild emojis.
18impl Rest {
19    /// [List Guild Emojis](https://docs.discord.com/developers/resources/emoji#list-guild-emojis):
20    /// returns every custom emoji of a guild. [`Emoji::user`] is included with the
21    /// `CREATE_GUILD_EXPRESSIONS` or `MANAGE_GUILD_EXPRESSIONS` permission.
22    ///
23    /// discord.js counterpart: `GuildEmojiManager#fetch()`.
24    ///
25    /// # Errors
26    ///
27    /// See [`Rest::request`].
28    pub fn list_guild_emojis(
29        &self,
30        guild_id: GuildId,
31    ) -> impl Future<Output = Result<Vec<Emoji>, Error>> + Send + '_ {
32        boxed(async move {
33            self.call(Request::new(Route::ListGuildEmojis { guild_id }))
34                .await
35        })
36    }
37
38    /// [Get Guild Emoji](https://docs.discord.com/developers/resources/emoji#get-guild-emoji):
39    /// returns one custom emoji of a guild.
40    ///
41    /// discord.js counterpart: `GuildEmojiManager#fetch(id)`.
42    ///
43    /// # Errors
44    ///
45    /// See [`Rest::request`]; `10014` Unknown Emoji.
46    pub fn get_guild_emoji(
47        &self,
48        guild_id: GuildId,
49        emoji_id: EmojiId,
50    ) -> impl Future<Output = Result<Emoji, Error>> + Send + '_ {
51        boxed(async move {
52            self.call(Request::new(Route::GetGuildEmoji { guild_id, emoji_id }))
53                .await
54        })
55    }
56
57    /// [Create Guild Emoji](https://docs.discord.com/developers/resources/emoji#create-guild-emoji):
58    /// uploads a custom emoji. Needs `CREATE_GUILD_EXPRESSIONS`.
59    ///
60    /// `reason` is shown in the audit log (D-086).
61    ///
62    /// discord.js counterpart: `GuildEmojiManager#create`.
63    ///
64    /// # Examples
65    ///
66    /// ```no_run
67    /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId) -> Result<(), Box<dyn std::error::Error>> {
68    /// use djs_model::{rest::CreateGuildEmoji, util::ImageData};
69    ///
70    /// let image = ImageData::new(std::fs::read("ferris.png")?)?;
71    /// let emoji = rest
72    ///     .create_guild_emoji(guild_id, &CreateGuildEmoji::new("ferris", image), Some("New mascot"))
73    ///     .await?;
74    /// println!("Use it as <:{}:{}>", emoji.name, emoji.id);
75    /// # Ok(())
76    /// # }
77    /// ```
78    ///
79    /// # Errors
80    ///
81    /// See [`Rest::request`]; `30008` or `30018` when the guild has no free (animated) emoji
82    /// slot; a `400` without a JSON error code when the image is larger than 256 KiB.
83    pub fn create_guild_emoji<'a>(
84        &'a self,
85        guild_id: GuildId,
86        body: &'a CreateGuildEmoji,
87        reason: Option<&'a str>,
88    ) -> impl Future<Output = Result<Emoji, Error>> + Send + 'a {
89        boxed(async move {
90            self.call(
91                Request::new(Route::CreateGuildEmoji { guild_id })
92                    .json(body)?
93                    .reason(reason),
94            )
95            .await
96        })
97    }
98
99    /// [Modify Guild Emoji](https://docs.discord.com/developers/resources/emoji#modify-guild-emoji):
100    /// renames an emoji or changes which roles may use it. Needs `MANAGE_GUILD_EXPRESSIONS`,
101    /// or `CREATE_GUILD_EXPRESSIONS` for emojis the bot created.
102    ///
103    /// `reason` is shown in the audit log (D-086).
104    ///
105    /// discord.js counterpart: `GuildEmoji#edit`, `GuildEmojiManager#edit`.
106    ///
107    /// # Errors
108    ///
109    /// See [`Rest::request`]; `10014` Unknown Emoji.
110    pub fn modify_guild_emoji<'a>(
111        &'a self,
112        guild_id: GuildId,
113        emoji_id: EmojiId,
114        body: &'a ModifyGuildEmoji,
115        reason: Option<&'a str>,
116    ) -> impl Future<Output = Result<Emoji, Error>> + Send + 'a {
117        boxed(async move {
118            self.call(
119                Request::new(Route::ModifyGuildEmoji { guild_id, emoji_id })
120                    .json(body)?
121                    .reason(reason),
122            )
123            .await
124        })
125    }
126
127    /// [Delete Guild Emoji](https://docs.discord.com/developers/resources/emoji#delete-guild-emoji):
128    /// deletes a custom emoji. Needs `MANAGE_GUILD_EXPRESSIONS`, or `CREATE_GUILD_EXPRESSIONS`
129    /// for emojis the bot created.
130    ///
131    /// `reason` is shown in the audit log (D-086).
132    ///
133    /// discord.js counterpart: `GuildEmoji#delete`, `GuildEmojiManager#delete`.
134    ///
135    /// # Errors
136    ///
137    /// See [`Rest::request`]; `10014` Unknown Emoji.
138    pub fn delete_guild_emoji<'a>(
139        &'a self,
140        guild_id: GuildId,
141        emoji_id: EmojiId,
142        reason: Option<&'a str>,
143    ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
144        boxed(async move {
145            self.call(Request::new(Route::DeleteGuildEmoji { guild_id, emoji_id }).reason(reason))
146                .await
147        })
148    }
149}
150
151/// Application emojis: emojis owned by the bot's application, usable by the bot everywhere.
152impl Rest {
153    /// [List Application Emojis](https://docs.discord.com/developers/resources/emoji#list-application-emojis):
154    /// returns every emoji of the application, with [`Emoji::user`] (the team member who
155    /// uploaded it, or the bot). Discord wraps the list in an object; it is unwrapped.
156    ///
157    /// discord.js counterpart: `ApplicationEmojiManager#fetch()`.
158    ///
159    /// # Errors
160    ///
161    /// See [`Rest::request`].
162    pub fn list_application_emojis(
163        &self,
164        application_id: ApplicationId,
165    ) -> impl Future<Output = Result<Vec<Emoji>, Error>> + Send + '_ {
166        boxed(async move {
167            let list: Items<Emoji> = self
168                .request(Request::new(Route::ListApplicationEmojis {
169                    application_id,
170                }))
171                .await?;
172            Ok(list.items)
173        })
174    }
175
176    /// [Get Application Emoji](https://docs.discord.com/developers/resources/emoji#get-application-emoji):
177    /// returns one emoji of the application, with [`Emoji::user`].
178    ///
179    /// discord.js counterpart: `ApplicationEmojiManager#fetch(id)`.
180    ///
181    /// # Errors
182    ///
183    /// See [`Rest::request`]; `10014` Unknown Emoji.
184    pub fn get_application_emoji(
185        &self,
186        application_id: ApplicationId,
187        emoji_id: EmojiId,
188    ) -> impl Future<Output = Result<Emoji, Error>> + Send + '_ {
189        boxed(async move {
190            self.call(Request::new(Route::GetApplicationEmoji {
191                application_id,
192                emoji_id,
193            }))
194            .await
195        })
196    }
197
198    /// [Create Application Emoji](https://docs.discord.com/developers/resources/emoji#create-application-emoji):
199    /// uploads an emoji the bot can use in every guild and DM, without needing the
200    /// `USE_EXTERNAL_EMOJIS` permission there.
201    ///
202    /// discord.js counterpart: `ApplicationEmojiManager#create`.
203    ///
204    /// # Examples
205    ///
206    /// ```no_run
207    /// # async fn example(rest: djs_rest::Rest, application_id: djs_model::id::ApplicationId) -> Result<(), Box<dyn std::error::Error>> {
208    /// use djs_model::{rest::CreateApplicationEmoji, util::ImageData};
209    ///
210    /// let image = ImageData::new(std::fs::read("loading.gif")?)?;
211    /// let body = CreateApplicationEmoji::new("loading", image);
212    /// let emoji = rest.create_application_emoji(application_id, &body).await?;
213    /// assert!(emoji.animated);
214    /// # Ok(())
215    /// # }
216    /// ```
217    ///
218    /// # Errors
219    ///
220    /// See [`Rest::request`]; `30008` when the application has no free emoji slot; a `400`
221    /// without a JSON error code when the image is larger than 256 KiB.
222    pub fn create_application_emoji<'a>(
223        &'a self,
224        application_id: ApplicationId,
225        body: &'a CreateApplicationEmoji,
226    ) -> impl Future<Output = Result<Emoji, Error>> + Send + 'a {
227        boxed(async move {
228            self.call(Request::new(Route::CreateApplicationEmoji { application_id }).json(body)?)
229                .await
230        })
231    }
232
233    /// [Modify Application Emoji](https://docs.discord.com/developers/resources/emoji#modify-application-emoji):
234    /// renames an emoji of the application.
235    ///
236    /// discord.js counterpart: `ApplicationEmoji#edit`, `ApplicationEmojiManager#edit`.
237    ///
238    /// # Errors
239    ///
240    /// See [`Rest::request`]; `10014` Unknown Emoji.
241    pub fn modify_application_emoji<'a>(
242        &'a self,
243        application_id: ApplicationId,
244        emoji_id: EmojiId,
245        body: &'a ModifyApplicationEmoji,
246    ) -> impl Future<Output = Result<Emoji, Error>> + Send + 'a {
247        boxed(async move {
248            self.call(
249                Request::new(Route::ModifyApplicationEmoji {
250                    application_id,
251                    emoji_id,
252                })
253                .json(body)?,
254            )
255            .await
256        })
257    }
258
259    /// [Delete Application Emoji](https://docs.discord.com/developers/resources/emoji#delete-application-emoji):
260    /// deletes an emoji of the application.
261    ///
262    /// discord.js counterpart: `ApplicationEmoji#delete`, `ApplicationEmojiManager#delete`.
263    ///
264    /// # Errors
265    ///
266    /// See [`Rest::request`]; `10014` Unknown Emoji.
267    pub fn delete_application_emoji(
268        &self,
269        application_id: ApplicationId,
270        emoji_id: EmojiId,
271    ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
272        boxed(async move {
273            self.call(Request::new(Route::DeleteApplicationEmoji {
274                application_id,
275                emoji_id,
276            }))
277            .await
278        })
279    }
280}