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, str, 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    str!(cdn_host, "The CDN for the RPC server.");
64    str!(api_endpoint, "The API endpoint for the RPC server.");
65    str!(environment, "The environment for the RPC server.");
66}
67
68/// Represents the Discord user that the RPC connection is present with.
69///
70/// NOTE: Only the fields which may need a documentation have been given one.
71#[derive(Debug, Clone, Deserialize)]
72pub struct DiscordUser {
73    id: String,
74    username: String,
75    discriminator: String,
76    global_name: Option<String>,
77    avatar: Option<String>,
78    bot: Option<bool>,
79    system: Option<bool>,
80    mfa_enabled: Option<bool>,
81    banner: Option<String>,
82    accent_color: Option<isize>,
83    locale: Option<String>,
84    verified: Option<bool>,
85    email: Option<String>,
86    flags: Option<isize>,
87    premium_type: Option<isize>,
88    public_flags: Option<isize>,
89    /// Data for the user's avatar decoration.
90    pub avatar_decoration_data: Option<AvatarDecorationData>,
91    /// Data for the user's collectibles.
92    pub collectibles: Option<Collectibles>,
93    /// The user's primary guild.
94    pub primary_guild: Option<PrimaryGuild>,
95}
96
97impl DiscordUser {
98    str!(id, "The user's ID.");
99    str!(
100        username,
101        "The user's username, not unique across the platform."
102    );
103    str!(discriminator, "The user's Discord-tag.");
104
105    ds!(global_name, "The user's display name, if set.");
106    ds!(avatar, "The user's avatar hash.");
107
108    /// Whether the user belongs to an OAuth2 application.
109    #[must_use]
110    pub fn bot(&self) -> Option<bool> {
111        self.bot
112    }
113
114    /// Whether the user is an Official Discord System user (part of the urgent message system).
115    #[must_use]
116    pub fn system(&self) -> Option<bool> {
117        self.system
118    }
119
120    /// Whether the user has two factor enabled on their account.
121    #[must_use]
122    pub fn mfa_enabled(&self) -> Option<bool> {
123        self.mfa_enabled
124    }
125
126    ds!(banner, "The user's banner hash.");
127    ds!(locale, "The user's chosen language option.");
128    ds!(email, "The user's email.");
129
130    /// Whether the email on this account has been verified.
131    #[must_use]
132    pub fn verified(&self) -> Option<bool> {
133        self.verified
134    }
135
136    /// The user's banner color encoded as an integer representation of hexadecimal color code.
137    #[must_use]
138    pub fn accent_color(&self) -> Option<isize> {
139        self.accent_color
140    }
141
142    /// The flags on the user's account.
143    #[must_use]
144    pub fn flags(&self) -> Option<isize> {
145        self.flags
146    }
147
148    /// The type of Nitro subscription on the user's account.
149    #[must_use]
150    pub fn premium_type(&self) -> Option<isize> {
151        self.premium_type
152    }
153
154    /// The public flags on the user's account.
155    #[must_use]
156    pub fn public_flags(&self) -> Option<isize> {
157        self.public_flags
158    }
159}
160
161/// The data for the user’s avatar decoration.
162#[derive(Debug, Clone, Deserialize)]
163pub struct AvatarDecorationData {
164    asset: String,
165    sku_id: String,
166}
167
168impl AvatarDecorationData {
169    str!(
170        asset,
171        "The avatar decoration hash. See: <https://docs.discord.com/developers/reference#image-formatting>"
172    );
173    str!(sku_id, "ID of the avatar decoration's SKU.");
174}
175
176/// The collectibles the user has, excluding Avatar Decorations and Profile Effects.
177#[derive(Debug, Clone, Deserialize)]
178pub struct Collectibles {
179    /// The nameplate the user has.
180    pub nameplate: Option<NameplateData>,
181}
182
183#[derive(Debug, Clone, Deserialize)]
184pub struct NameplateData {
185    sku_id: String,
186    asset: String,
187    label: String,
188    palette: String,
189}
190
191impl NameplateData {
192    str!(sku_id, "ID of the nameplate SKU.");
193    str!(
194        asset,
195        "Path to the nameplate asset. See: <https://docs.discord.com/developers/reference#image-formatting>"
196    );
197    str!(label, "The label of this nameplate. Currently unused.");
198    str!(
199        palette,
200        "Background color of the nameplate, one of: `crimson`, `berry`, `sky`, `teal`, `forest`, `bubble_gum`, `violet`, `cobalt`, `clover`, `lemon`, `white`"
201    );
202}
203
204#[derive(Debug, Clone, Deserialize)]
205pub struct PrimaryGuild {
206    identity_guild_id: Option<String>,
207    identity_enabled: Option<bool>,
208    tag: Option<String>,
209    badge: Option<String>,
210}
211
212impl PrimaryGuild {
213    ds!(identity_guild_id, "The ID of the user's primary guild.");
214    ds!(
215        tag,
216        "The text of the user's server tag. Limited to 4 characters."
217    );
218    ds!(badge, "The server tag badge hash.");
219
220    /// Whether the user is displaying the primary guild’s server tag.
221    /// This can be `None` if the system clears the identity, e.g. the server no longer supports tags.
222    /// This will be `false` if the user manually removes their tag.
223    #[must_use]
224    pub fn identity_enabled(&self) -> Option<bool> {
225        self.identity_enabled
226    }
227}