Skip to main content

djs_rs/manager/
application_emoji.rs

1//! [`ApplicationEmojiManager`].
2
3use std::future::Future;
4
5use djs_cache::Backend;
6use djs_collection::Collection;
7use djs_model::{
8    emoji::Emoji,
9    id::{ApplicationId, EmojiId},
10    rest::{CreateApplicationEmoji, ModifyApplicationEmoji},
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17/// The emojis of the bot's application, usable by the bot in every guild. Returned by
18/// [`Context::application_emojis`].
19///
20/// The cache does not keep application emojis, so the [`djs-model`](djs_model) [`Emoji`] is
21/// returned.
22///
23/// discord.js counterpart: `ApplicationEmojiManager` (`client.application.emojis`).
24#[derive(Debug)]
25pub struct ApplicationEmojiManager<'a, B: Backend> {
26    ctx: &'a Context<B>,
27    application_id: ApplicationId,
28}
29
30impl<B: Backend> Clone for ApplicationEmojiManager<'_, B> {
31    fn clone(&self) -> Self {
32        *self
33    }
34}
35
36impl<B: Backend> Copy for ApplicationEmojiManager<'_, B> {}
37
38impl<'a, B: Backend> ApplicationEmojiManager<'a, B> {
39    pub(crate) const fn new(ctx: &'a Context<B>, application_id: ApplicationId) -> Self {
40        Self {
41            ctx,
42            application_id,
43        }
44    }
45
46    /// An emoji ([Get Application Emoji](djs_rest::Rest::get_application_emoji)).
47    ///
48    /// discord.js counterpart: `ApplicationEmojiManager#fetch(id)`.
49    ///
50    /// # Errors
51    ///
52    /// Returns the REST error if the request failed.
53    pub fn fetch(
54        self,
55        emoji_id: EmojiId,
56    ) -> Fetch<Emoji, impl Future<Output = Result<Emoji, Error>> + Send + 'a> {
57        let Self {
58            ctx,
59            application_id,
60        } = self;
61        Fetch::new(None, async move {
62            ctx.rest()
63                .get_application_emoji(application_id, emoji_id)
64                .await
65        })
66    }
67
68    /// Every emoji of the application ([List Application
69    /// Emojis](djs_rest::Rest::list_application_emojis)).
70    ///
71    /// discord.js counterpart: `ApplicationEmojiManager#fetch()`.
72    ///
73    /// # Errors
74    ///
75    /// Returns the REST error if the request failed.
76    pub async fn list(self) -> Result<Collection<EmojiId, Emoji>, Error> {
77        let emojis = self
78            .ctx
79            .rest()
80            .list_application_emojis(self.application_id)
81            .await?;
82        Ok(emojis.into_iter().map(|emoji| (emoji.id, emoji)).collect())
83    }
84
85    /// Uploads an emoji ([Create Application Emoji](djs_rest::Rest::create_application_emoji)).
86    ///
87    /// discord.js counterpart: `ApplicationEmojiManager#create`.
88    ///
89    /// # Errors
90    ///
91    /// Returns the REST error if the request failed (at most 2000 emojis).
92    pub async fn create(self, body: &CreateApplicationEmoji) -> Result<Emoji, Error> {
93        self.ctx
94            .rest()
95            .create_application_emoji(self.application_id, body)
96            .await
97    }
98
99    /// Renames an emoji ([Modify Application Emoji](djs_rest::Rest::modify_application_emoji)).
100    ///
101    /// discord.js counterpart: `ApplicationEmojiManager#edit`.
102    ///
103    /// # Errors
104    ///
105    /// Returns the REST error if the request failed.
106    pub async fn edit(
107        self,
108        emoji_id: EmojiId,
109        body: &ModifyApplicationEmoji,
110    ) -> Result<Emoji, Error> {
111        self.ctx
112            .rest()
113            .modify_application_emoji(self.application_id, emoji_id, body)
114            .await
115    }
116
117    /// Deletes an emoji ([Delete Application Emoji](djs_rest::Rest::delete_application_emoji)).
118    ///
119    /// discord.js counterpart: `ApplicationEmojiManager#delete`.
120    ///
121    /// # Errors
122    ///
123    /// Returns the REST error if the request failed.
124    pub async fn delete(self, emoji_id: EmojiId) -> Result<(), Error> {
125        self.ctx
126            .rest()
127            .delete_application_emoji(self.application_id, emoji_id)
128            .await
129    }
130}