djs_model/sticker.rs
1//! [Stickers](https://docs.discord.com/developers/resources/sticker): standard sticker packs
2//! and stickers uploaded to guilds.
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 id::{GenericId, GuildId, SkuId, StickerId, StickerPackId},
9 macros::int_enum,
10 user::User,
11};
12
13/// A [sticker](https://docs.discord.com/developers/resources/sticker#sticker-object) that can be
14/// sent in messages.
15///
16/// discord.js counterpart: `Sticker`.
17#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
18#[non_exhaustive]
19pub struct Sticker {
20 /// The sticker's ID.
21 pub id: StickerId,
22 /// For standard stickers, the pack the sticker belongs to.
23 #[serde(default, skip_serializing_if = "Option::is_none")]
24 pub pack_id: Option<StickerPackId>,
25 /// The sticker's name.
26 pub name: CompactString,
27 /// The sticker's description.
28 pub description: Option<CompactString>,
29 /// Autocomplete/suggestion tags (up to 200 characters). Standard stickers use a
30 /// comma-separated list of keywords, but that is only a convention; for guild stickers this
31 /// is the name of a Unicode emoji.
32 pub tags: CompactString,
33 /// Whether this is a standard or a guild sticker.
34 #[serde(rename = "type")]
35 pub kind: StickerType,
36 /// The sticker's file format.
37 pub format_type: StickerFormatType,
38 /// Whether the guild sticker can be used; `false` when the guild lost the Server Boosts it
39 /// needs. `true` when Discord omits the field, as it does for standard stickers (D-055).
40 #[serde(default = "crate::serde_util::default_true")]
41 pub available: bool,
42 /// The guild that owns the sticker.
43 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub guild_id: Option<GuildId>,
45 /// Who uploaded the guild sticker (requires the `MANAGE_GUILD_EXPRESSIONS` or
46 /// `CREATE_GUILD_EXPRESSIONS` permission).
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub user: Option<Box<User>>,
49 /// The standard sticker's position within its pack.
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub sort_value: Option<u32>,
52}
53
54int_enum! {
55 /// The [type of a sticker](https://docs.discord.com/developers/resources/sticker#sticker-object-sticker-types).
56 ///
57 /// discord.js counterpart: `StickerType`.
58 pub enum StickerType: u8 {
59 /// An official sticker from a pack.
60 Standard = 1,
61 /// A sticker uploaded to a guild for its members.
62 Guild = 2,
63 }
64}
65
66int_enum! {
67 /// The [file format of a sticker](https://docs.discord.com/developers/resources/sticker#sticker-object-sticker-format-types).
68 ///
69 /// discord.js counterpart: `StickerFormatType`.
70 pub enum StickerFormatType: u8 {
71 /// PNG image.
72 Png = 1,
73 /// Animated PNG image.
74 Apng = 2,
75 /// [Lottie](https://airbnb.io/lottie/) JSON animation.
76 Lottie = 3,
77 /// GIF image.
78 Gif = 4,
79 }
80}
81
82/// The minimal [sticker item](https://docs.discord.com/developers/resources/sticker#sticker-item-object)
83/// attached to messages.
84///
85/// discord.js counterpart: the stickers of `Message#stickers`.
86#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
87#[non_exhaustive]
88pub struct StickerItem {
89 /// The sticker's ID.
90 pub id: StickerId,
91 /// The sticker's name.
92 pub name: CompactString,
93 /// The sticker's file format.
94 pub format_type: StickerFormatType,
95}
96
97/// A [pack of standard stickers](https://docs.discord.com/developers/resources/sticker#sticker-pack-object).
98///
99/// discord.js counterpart: `StickerPack`.
100#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
101#[non_exhaustive]
102pub struct StickerPack {
103 /// The pack's ID.
104 pub id: StickerPackId,
105 /// The stickers in the pack.
106 pub stickers: Vec<Sticker>,
107 /// The pack's name.
108 pub name: CompactString,
109 /// The pack's SKU.
110 pub sku_id: SkuId,
111 /// The sticker shown as the pack's icon.
112 #[serde(default, skip_serializing_if = "Option::is_none")]
113 pub cover_sticker_id: Option<StickerId>,
114 /// The pack's description.
115 pub description: CompactString,
116 /// The ID of the pack's [banner image](https://docs.discord.com/developers/reference#image-formatting).
117 #[serde(default, skip_serializing_if = "Option::is_none")]
118 pub banner_asset_id: Option<GenericId>,
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn standard_sticker_is_available() {
127 let json = r#"{"id":"749054660769218631","name":"Wave","tags":"wumpus, hello, sup, hi, oi, heyo, heya, yo, wave",
128 "type":1,"format_type":3,"description":"Wumpus waves hello","asset":"",
129 "pack_id":"847199849233514549","sort_value":12}"#;
130 let sticker: Sticker = serde_json::from_str(json).unwrap();
131 assert_eq!(sticker.kind, StickerType::Standard);
132 assert_eq!(sticker.format_type, StickerFormatType::Lottie);
133 assert!(sticker.available);
134 assert_eq!(sticker.sort_value, Some(12));
135 }
136
137 #[test]
138 fn sticker_item() {
139 let item: StickerItem =
140 serde_json::from_str(r#"{"id":"1","name":"x","format_type":4}"#).unwrap();
141 assert_eq!(item.format_type, StickerFormatType::Gif);
142 }
143}