Skip to main content

djs_model/rest/
emoji.rs

1//! Bodies of the [emoji endpoints](https://docs.discord.com/developers/resources/emoji): guild
2//! emojis and application emojis.
3
4use compact_str::CompactString;
5use serde::Serialize;
6
7use crate::{
8    id::RoleId,
9    util::{ImageData, Patch},
10};
11
12/// The body of [Create Guild Emoji](https://docs.discord.com/developers/resources/emoji#create-guild-emoji).
13///
14/// The image (PNG, JPEG, GIF for animated emojis, or WebP) may be at most 256 KiB; Discord
15/// shows it at 128×128.
16///
17/// discord.js counterpart: `GuildEmojiCreateOptions`.
18///
19/// # Examples
20///
21/// ```
22/// use djs_model::{id::RoleId, rest::CreateGuildEmoji, util::ImageData};
23///
24/// let png = b"\x89PNG\r\n\x1a\n...".to_vec();
25/// let mut body = CreateGuildEmoji::new("ferris", ImageData::new(png)?);
26/// body.roles.push(RoleId::new(1_100_000_000_000_000_001));
27/// let json = serde_json::to_string(&body)?;
28/// assert!(json.starts_with(r#"{"name":"ferris","image":"data:image/png;base64,"#));
29/// assert!(json.ends_with(r#","roles":["1100000000000000001"]}"#));
30/// # Ok::<(), Box<dyn std::error::Error>>(())
31/// ```
32#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
33#[non_exhaustive]
34pub struct CreateGuildEmoji {
35    /// The name (2–32 characters: letters, digits and underscores).
36    pub name: CompactString,
37    /// The image.
38    pub image: ImageData,
39    /// The roles allowed to use the emoji; empty (the default) means everyone.
40    #[serde(skip_serializing_if = "Vec::is_empty")]
41    pub roles: Vec<RoleId>,
42}
43
44impl CreateGuildEmoji {
45    /// An emoji named `name` showing `image`, usable by everyone.
46    ///
47    /// discord.js counterpart: `GuildEmojiCreateOptions`.
48    #[must_use]
49    pub fn new(name: impl Into<CompactString>, image: ImageData) -> Self {
50        Self {
51            name: name.into(),
52            image,
53            roles: Vec::new(),
54        }
55    }
56}
57
58/// The body of [Modify Guild Emoji](https://docs.discord.com/developers/resources/emoji#modify-guild-emoji).
59///
60/// Every field is optional; the image cannot be changed.
61///
62/// discord.js counterpart: `GuildEmojiEditOptions`.
63///
64/// # Examples
65///
66/// ```
67/// use djs_model::{rest::ModifyGuildEmoji, util::Patch};
68///
69/// let mut body = ModifyGuildEmoji::default();
70/// body.name = Some("crab".into());
71/// body.roles = Patch::Clear; // usable by everyone again
72/// assert_eq!(serde_json::to_string(&body)?, r#"{"name":"crab","roles":null}"#);
73/// # Ok::<(), serde_json::Error>(())
74/// ```
75#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize)]
76#[non_exhaustive]
77pub struct ModifyGuildEmoji {
78    /// The new name.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub name: Option<CompactString>,
81    /// The roles allowed to use the emoji; [`Patch::Clear`] allows everyone.
82    #[serde(skip_serializing_if = "Patch::is_keep")]
83    pub roles: Patch<Vec<RoleId>>,
84}
85
86/// The body of [Create Application Emoji](https://docs.discord.com/developers/resources/emoji#create-application-emoji).
87///
88/// Application emojis belong to the bot itself and can be used by it in every guild and DM.
89/// The image may be at most 256 KiB.
90///
91/// discord.js counterpart: `ApplicationEmojiCreateOptions`.
92#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
93#[non_exhaustive]
94pub struct CreateApplicationEmoji {
95    /// The name (2–32 characters: letters, digits and underscores).
96    pub name: CompactString,
97    /// The image.
98    pub image: ImageData,
99}
100
101impl CreateApplicationEmoji {
102    /// An emoji named `name` showing `image`.
103    ///
104    /// discord.js counterpart: `ApplicationEmojiCreateOptions`.
105    #[must_use]
106    pub fn new(name: impl Into<CompactString>, image: ImageData) -> Self {
107        Self {
108            name: name.into(),
109            image,
110        }
111    }
112}
113
114/// The body of [Modify Application Emoji](https://docs.discord.com/developers/resources/emoji#modify-application-emoji).
115///
116/// Only the name can be changed.
117///
118/// discord.js counterpart: `ApplicationEmojiEditOptions`.
119#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize)]
120#[non_exhaustive]
121pub struct ModifyApplicationEmoji {
122    /// The new name.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub name: Option<CompactString>,
125}