Skip to main content

djs_rs/manager/
sticker.rs

1//! [`StickerManager`].
2
3use std::{future::Future, sync::Arc};
4
5use djs_cache::{Backend, GuildView};
6use djs_collection::Collection;
7use djs_model::{
8    id::{GuildId, StickerId},
9    rest::{CreateGuildSticker, ModifyGuildSticker},
10    sticker::Sticker,
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17/// The stickers of one guild. Returned by [`Context::stickers`].
18///
19/// discord.js counterpart: `GuildStickerManager` (`guild.stickers`).
20#[derive(Debug)]
21pub struct StickerManager<'a, B: Backend> {
22    ctx: &'a Context<B>,
23    guild_id: GuildId,
24}
25
26impl<B: Backend> Clone for StickerManager<'_, B> {
27    fn clone(&self) -> Self {
28        *self
29    }
30}
31
32impl<B: Backend> Copy for StickerManager<'_, B> {}
33
34impl<'a, B: Backend> StickerManager<'a, B> {
35    pub(crate) const fn new(ctx: &'a Context<B>, guild_id: GuildId) -> Self {
36        Self { ctx, guild_id }
37    }
38
39    /// The guild these stickers belong to.
40    ///
41    /// discord.js counterpart: `GuildStickerManager#guild`.
42    #[must_use]
43    pub const fn guild_id(self) -> GuildId {
44        self.guild_id
45    }
46
47    /// The cached stickers of the guild, if the client has a cache.
48    ///
49    /// discord.js counterpart: `GuildStickerManager#cache`.
50    #[must_use]
51    pub fn cache(self) -> Option<GuildView<'a, StickerId, Sticker, B>> {
52        self.ctx.cache().map(|cache| cache.stickers(self.guild_id))
53    }
54
55    /// A sticker: the cached one, or [Get Guild Sticker](djs_rest::Rest::get_guild_sticker),
56    /// whose result is cached.
57    ///
58    /// discord.js counterpart: `GuildStickerManager#fetch(id)`.
59    ///
60    /// # Errors
61    ///
62    /// Returns the REST error if Discord was asked and the request failed.
63    pub fn fetch(
64        self,
65        sticker_id: StickerId,
66    ) -> Fetch<Arc<Sticker>, impl Future<Output = Result<Arc<Sticker>, Error>> + Send + 'a> {
67        let cached = self.ctx.ops().sticker(self.guild_id, sticker_id);
68        let Self { ctx, guild_id } = self;
69        Fetch::new(cached, async move {
70            let sticker = ctx.rest().get_guild_sticker(guild_id, sticker_id).await?;
71            Ok(ctx.store_sticker(guild_id, &sticker))
72        })
73    }
74
75    /// Every sticker of the guild ([List Guild
76    /// Stickers](djs_rest::Rest::list_guild_stickers)); the stickers are cached.
77    ///
78    /// discord.js counterpart: `GuildStickerManager#fetch()`.
79    ///
80    /// # Errors
81    ///
82    /// Returns the REST error if the request failed.
83    pub async fn list(self) -> Result<Collection<StickerId, Arc<Sticker>>, Error> {
84        let stickers = self.ctx.rest().list_guild_stickers(self.guild_id).await?;
85        Ok(stickers
86            .iter()
87            .map(|sticker| (sticker.id, self.ctx.store_sticker(self.guild_id, sticker)))
88            .collect())
89    }
90
91    /// Uploads a sticker ([Create Guild Sticker](djs_rest::Rest::create_guild_sticker)); it
92    /// is cached.
93    ///
94    /// discord.js counterpart: `GuildStickerManager#create`.
95    ///
96    /// # Errors
97    ///
98    /// Returns the REST error if the request failed (it needs `CREATE_EXPRESSIONS`; the file
99    /// must be a PNG, APNG, GIF or Lottie JSON of at most 512 KiB).
100    pub async fn create(
101        self,
102        body: &CreateGuildSticker,
103        reason: Option<&str>,
104    ) -> Result<Arc<Sticker>, Error> {
105        let sticker = self
106            .ctx
107            .rest()
108            .create_guild_sticker(self.guild_id, body, reason)
109            .await?;
110        Ok(self.ctx.store_sticker(self.guild_id, &sticker))
111    }
112
113    /// Changes a sticker's name, description or tags ([Modify Guild
114    /// Sticker](djs_rest::Rest::modify_guild_sticker)); the result is cached.
115    ///
116    /// discord.js counterpart: `GuildStickerManager#edit` / `Sticker#edit`.
117    ///
118    /// # Errors
119    ///
120    /// Returns the REST error if the request failed.
121    pub async fn edit(
122        self,
123        sticker_id: StickerId,
124        body: &ModifyGuildSticker,
125        reason: Option<&str>,
126    ) -> Result<Arc<Sticker>, Error> {
127        let sticker = self
128            .ctx
129            .rest()
130            .modify_guild_sticker(self.guild_id, sticker_id, body, reason)
131            .await?;
132        Ok(self.ctx.store_sticker(self.guild_id, &sticker))
133    }
134
135    /// Deletes a sticker ([Delete Guild Sticker](djs_rest::Rest::delete_guild_sticker)). The
136    /// cache is updated by the `GUILD_STICKERS_UPDATE` event that follows (D-236).
137    ///
138    /// discord.js counterpart: `GuildStickerManager#delete` / `Sticker#delete`.
139    ///
140    /// # Errors
141    ///
142    /// Returns the REST error if the request failed.
143    pub async fn delete(self, sticker_id: StickerId, reason: Option<&str>) -> Result<(), Error> {
144        self.ctx
145            .rest()
146            .delete_guild_sticker(self.guild_id, sticker_id, reason)
147            .await
148    }
149}