djs_rs/manager/emoji.rs
1//! [`EmojiManager`].
2
3use std::{future::Future, sync::Arc};
4
5use djs_cache::{Backend, GuildView};
6use djs_collection::Collection;
7use djs_model::{
8 emoji::Emoji,
9 id::{EmojiId, GuildId},
10 rest::{CreateGuildEmoji, ModifyGuildEmoji},
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17/// The custom emojis of one guild. Returned by [`Context::emojis`].
18///
19/// discord.js counterpart: `GuildEmojiManager` (`guild.emojis`).
20#[derive(Debug)]
21pub struct EmojiManager<'a, B: Backend> {
22 ctx: &'a Context<B>,
23 guild_id: GuildId,
24}
25
26impl<B: Backend> Clone for EmojiManager<'_, B> {
27 fn clone(&self) -> Self {
28 *self
29 }
30}
31
32impl<B: Backend> Copy for EmojiManager<'_, B> {}
33
34impl<'a, B: Backend> EmojiManager<'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 emojis belong to.
40 ///
41 /// discord.js counterpart: `GuildEmojiManager#guild`.
42 #[must_use]
43 pub const fn guild_id(self) -> GuildId {
44 self.guild_id
45 }
46
47 /// The cached emojis of the guild, if the client has a cache.
48 ///
49 /// discord.js counterpart: `GuildEmojiManager#cache`.
50 #[must_use]
51 pub fn cache(self) -> Option<GuildView<'a, EmojiId, Emoji, B>> {
52 self.ctx.cache().map(|cache| cache.emojis(self.guild_id))
53 }
54
55 /// An emoji: the cached one, or [Get Guild Emoji](djs_rest::Rest::get_guild_emoji),
56 /// whose result is cached.
57 ///
58 /// discord.js counterpart: `GuildEmojiManager#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 emoji_id: EmojiId,
66 ) -> Fetch<Arc<Emoji>, impl Future<Output = Result<Arc<Emoji>, Error>> + Send + 'a> {
67 let cached = self.ctx.ops().emoji(self.guild_id, emoji_id);
68 let Self { ctx, guild_id } = self;
69 Fetch::new(cached, async move {
70 let emoji = ctx.rest().get_guild_emoji(guild_id, emoji_id).await?;
71 Ok(ctx.store_emoji(guild_id, &emoji))
72 })
73 }
74
75 /// Every custom emoji of the guild ([List Guild
76 /// Emojis](djs_rest::Rest::list_guild_emojis)); the emojis are cached.
77 ///
78 /// discord.js counterpart: `GuildEmojiManager#fetch()`.
79 ///
80 /// # Errors
81 ///
82 /// Returns the REST error if the request failed.
83 pub async fn list(self) -> Result<Collection<EmojiId, Arc<Emoji>>, Error> {
84 let emojis = self.ctx.rest().list_guild_emojis(self.guild_id).await?;
85 Ok(emojis
86 .iter()
87 .map(|emoji| (emoji.id, self.ctx.store_emoji(self.guild_id, emoji)))
88 .collect())
89 }
90
91 /// Uploads an emoji ([Create Guild Emoji](djs_rest::Rest::create_guild_emoji)); it is
92 /// cached.
93 ///
94 /// discord.js counterpart: `GuildEmojiManager#create`.
95 ///
96 /// # Errors
97 ///
98 /// Returns the REST error if the request failed (it needs `CREATE_EXPRESSIONS`; the
99 /// image must be at most 256 KiB).
100 pub async fn create(
101 self,
102 body: &CreateGuildEmoji,
103 reason: Option<&str>,
104 ) -> Result<Arc<Emoji>, Error> {
105 let emoji = self
106 .ctx
107 .rest()
108 .create_guild_emoji(self.guild_id, body, reason)
109 .await?;
110 Ok(self.ctx.store_emoji(self.guild_id, &emoji))
111 }
112
113 /// Renames an emoji or changes the roles allowed to use it ([Modify Guild
114 /// Emoji](djs_rest::Rest::modify_guild_emoji)); the result is cached.
115 ///
116 /// discord.js counterpart: `GuildEmojiManager#edit` / `GuildEmoji#edit`.
117 ///
118 /// # Errors
119 ///
120 /// Returns the REST error if the request failed.
121 pub async fn edit(
122 self,
123 emoji_id: EmojiId,
124 body: &ModifyGuildEmoji,
125 reason: Option<&str>,
126 ) -> Result<Arc<Emoji>, Error> {
127 let emoji = self
128 .ctx
129 .rest()
130 .modify_guild_emoji(self.guild_id, emoji_id, body, reason)
131 .await?;
132 Ok(self.ctx.store_emoji(self.guild_id, &emoji))
133 }
134
135 /// Deletes an emoji ([Delete Guild Emoji](djs_rest::Rest::delete_guild_emoji)). The
136 /// cache is updated by the `GUILD_EMOJIS_UPDATE` event that follows (D-236).
137 ///
138 /// discord.js counterpart: `GuildEmojiManager#delete` / `GuildEmoji#delete`.
139 ///
140 /// # Errors
141 ///
142 /// Returns the REST error if the request failed.
143 pub async fn delete(self, emoji_id: EmojiId, reason: Option<&str>) -> Result<(), Error> {
144 self.ctx
145 .rest()
146 .delete_guild_emoji(self.guild_id, emoji_id, reason)
147 .await
148 }
149}