Skip to main content

filthy_rich/types/
data.rs

1//! Return types for various functions and closures for filthy-rich. Most of these do not need to be
2//! imported separately as these are already supplied to you, and are clonable and can be debugged.
3//!
4use serde::Deserialize;
5use serde_json::Value;
6
7use crate::{ds, types::payloads::ActivityPayload};
8
9/// Data received in response from the server after sending a SET_ACTIVITY command.
10///
11/// Note that this struct doesn't fully cover the schema of the actual response since most of the fields
12/// that are found are the same as the actual activity that is sent.
13///
14/// More importantly, open-source implementations of RPC (e.g. arRPC) have different response styles so
15/// the actual output of this struct may vary depending on what client you are using.
16#[derive(Debug, Clone, Deserialize)]
17pub struct ActivityResponseData {
18    application_id: Option<String>,
19    platform: Option<String>,
20    metadata: Option<Value>,
21    /// The activity payload which came in response to the send.
22    #[serde(flatten)]
23    pub activity: ActivityPayload,
24}
25
26impl ActivityResponseData {
27    ds!(application_id, "The ID of the application");
28    ds!(platform, "The platform of the host.");
29
30    #[must_use]
31    pub fn metadata(&self) -> Option<&Value> {
32        self.metadata.as_ref()
33    }
34}
35
36/// Data received from a READY event.
37#[derive(Debug, Clone, Deserialize)]
38pub struct ReadyData {
39    v: u8,
40    /// The server configuration data for the RPC.
41    pub config: ServerConfigurationData,
42    /// The user to whom you are connected.
43    pub user: DiscordUser,
44}
45
46impl ReadyData {
47    /// The version of the RPC that is being used.
48    #[must_use]
49    pub fn version(&self) -> u8 {
50        self.v
51    }
52}
53
54/// Server configuration data received from the RPC server.
55#[derive(Debug, Clone, Deserialize)]
56pub struct ServerConfigurationData {
57    cdn_host: String,
58    api_endpoint: String,
59    environment: String,
60}
61
62impl ServerConfigurationData {
63    /// The CDN for the RPC server.
64    #[must_use]
65    pub fn cdn_host(&self) -> &str {
66        &self.cdn_host
67    }
68
69    /// The API endpoint for the RPC server.
70    #[must_use]
71    pub fn api_endpoint(&self) -> &str {
72        &self.api_endpoint
73    }
74
75    /// The environment for the RPC server.
76    #[must_use]
77    pub fn environment(&self) -> &str {
78        &self.environment
79    }
80}
81
82/// Represents the Discord user that the RPC connection is present with.
83///
84/// NOTE: Only the fields which may need a documentation have been given one.
85#[derive(Debug, Clone, Deserialize)]
86pub struct DiscordUser {
87    id: String,
88    username: String,
89    discriminator: String,
90    global_name: Option<String>,
91    avatar: Option<String>,
92    bot: Option<bool>,
93    system: Option<bool>,
94    mfa_enabled: Option<bool>,
95    banner: Option<String>,
96    accent_color: Option<isize>,
97    locale: Option<String>,
98    verified: Option<bool>,
99    email: Option<String>,
100    flags: Option<isize>,
101    premium_type: Option<isize>,
102    public_flags: Option<isize>,
103    /// Data for the user's avatar decoration.
104    pub avatar_decoration_data: Option<AvatarDecorationData>,
105    /// Data for the user's collectibles.
106    pub collectibles: Option<Collectibles>,
107    /// The user's primary guild.
108    pub primary_guild: Option<PrimaryGuild>,
109}
110
111impl DiscordUser {
112    /// The user's ID.
113    #[must_use]
114    pub fn id(&self) -> &str {
115        &self.id
116    }
117
118    /// The user's username, not unique across the platform.
119    #[must_use]
120    pub fn username(&self) -> &str {
121        &self.username
122    }
123
124    /// The user's Discord-tag.
125    #[must_use]
126    pub fn discriminator(&self) -> &str {
127        &self.discriminator
128    }
129
130    ds!(global_name, "The user's display name, if set.");
131    ds!(avatar, "The user's avatar hash.");
132
133    /// Whether the user belongs to an OAuth2 application.
134    #[must_use]
135    pub fn bot(&self) -> Option<bool> {
136        self.bot
137    }
138
139    /// Whether the user is an Official Discord System user (part of the urgent message system).
140    #[must_use]
141    pub fn system(&self) -> Option<bool> {
142        self.system
143    }
144
145    /// Whether the user has two factor enabled on their account.
146    #[must_use]
147    pub fn mfa_enabled(&self) -> Option<bool> {
148        self.mfa_enabled
149    }
150
151    ds!(banner, "The user's banner hash.");
152    ds!(locale, "The user's chosen language option.");
153    ds!(email, "The user's email.");
154
155    /// Whether the email on this account has been verified.
156    #[must_use]
157    pub fn verified(&self) -> Option<bool> {
158        self.verified
159    }
160
161    /// The user's banner color encoded as an integer representation of hexadecimal color code.
162    #[must_use]
163    pub fn accent_color(&self) -> Option<isize> {
164        self.accent_color
165    }
166
167    /// The flags on the user's account.
168    #[must_use]
169    pub fn flags(&self) -> Option<isize> {
170        self.flags
171    }
172
173    /// The type of Nitro subscription on the user's account.
174    #[must_use]
175    pub fn premium_type(&self) -> Option<isize> {
176        self.premium_type
177    }
178
179    /// The public flags on the user's account.
180    #[must_use]
181    pub fn public_flags(&self) -> Option<isize> {
182        self.public_flags
183    }
184}
185
186/// The data for the user’s avatar decoration.
187#[derive(Debug, Clone, Deserialize)]
188pub struct AvatarDecorationData {
189    asset: String,
190    sku_id: String,
191}
192
193impl AvatarDecorationData {
194    /// The avatar decoration hash. See: <https://docs.discord.com/developers/reference#image-formatting>
195    #[must_use]
196    pub fn asset(&self) -> &str {
197        &self.asset
198    }
199    /// ID of the avatar decoration's SKU.
200    #[must_use]
201    pub fn sku_id(&self) -> &str {
202        &self.sku_id
203    }
204}
205
206/// The collectibles the user has, excluding Avatar Decorations and Profile Effects.
207#[derive(Debug, Clone, Deserialize)]
208pub struct Collectibles {
209    /// The nameplate the user has.
210    pub nameplate: Option<NameplateData>,
211}
212
213#[derive(Debug, Clone, Deserialize)]
214pub struct NameplateData {
215    sku_id: String,
216    asset: String,
217    label: String,
218    palette: String,
219}
220
221impl NameplateData {
222    /// ID of the nameplate SKU.
223    #[must_use]
224    pub fn sku_id(&self) -> &str {
225        &self.sku_id
226    }
227    /// Path to the nameplate asset. See: <https://docs.discord.com/developers/reference#image-formatting>
228    #[must_use]
229    pub fn asset(&self) -> &str {
230        &self.asset
231    }
232    /// The label of this nameplate. Currently unused.
233    #[must_use]
234    pub fn label(&self) -> &str {
235        &self.label
236    }
237    /// Background color of the nameplate, one of:
238    /// `crimson`, `berry`, `sky`, `teal`, `forest`, `bubble_gum`, `violet`, `cobalt`, `clover`, `lemon`, `white`
239    #[must_use]
240    pub fn palette(&self) -> &str {
241        &self.palette
242    }
243}
244
245#[derive(Debug, Clone, Deserialize)]
246pub struct PrimaryGuild {
247    identity_guild_id: Option<String>,
248    identity_enabled: Option<bool>,
249    tag: Option<String>,
250    badge: Option<String>,
251}
252
253impl PrimaryGuild {
254    ds!(identity_guild_id, "The ID of the user's primary guild.");
255    ds!(
256        tag,
257        "The text of the user's server tag. Limited to 4 characters."
258    );
259    ds!(badge, "The server tag badge hash.");
260
261    /// Whether the user is displaying the primary guild’s server tag.
262    /// This can be `None` if the system clears the identity, e.g. the server no longer supports tags.
263    /// This will be `false` if the user manually removes their tag.
264    #[must_use]
265    pub fn identity_enabled(&self) -> Option<bool> {
266        self.identity_enabled
267    }
268}