djs_model/application.rs
1//! [Applications](https://docs.discord.com/developers/resources/application): the apps (and
2//! their bot users) registered in the Developer Portal.
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 guild::PartialGuild,
9 id::{ApplicationId, ChannelId, GenericId, GuildId, SkuId, UserId},
10 macros::{int_enum, int_flags, str_enum},
11 oauth2::Scope,
12 permissions::Permissions,
13 team::Team,
14 user::User,
15 util::ImageHash,
16 webhook_event::WebhookEventType,
17};
18
19/// An [application](https://docs.discord.com/developers/resources/application#application-object).
20///
21/// discord.js counterpart: `ClientApplication` / `Application`.
22#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23#[non_exhaustive]
24pub struct Application {
25 /// The application's ID.
26 pub id: ApplicationId,
27 /// The application's name.
28 pub name: CompactString,
29 /// The [icon hash](https://docs.discord.com/developers/reference#image-formatting).
30 pub icon: Option<ImageHash>,
31 /// The application's description.
32 pub description: CompactString,
33 /// RPC origin URLs, if RPC is enabled.
34 #[serde(default, skip_serializing_if = "Vec::is_empty")]
35 pub rpc_origins: Vec<CompactString>,
36 /// Whether anyone (not only the owner) can add the bot to guilds.
37 pub bot_public: bool,
38 /// Whether the bot only joins after the full `OAuth2` code grant flow.
39 pub bot_require_code_grant: bool,
40 /// The application's bot user.
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub bot: Option<Box<User>>,
43 /// The URL of the terms of service.
44 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub terms_of_service_url: Option<CompactString>,
46 /// The URL of the privacy policy.
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub privacy_policy_url: Option<CompactString>,
49 /// The application's owner (for team-owned apps, a team pseudo-user).
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub owner: Option<Box<User>>,
52 /// The hex-encoded public key used to verify interaction requests.
53 pub verify_key: CompactString,
54 /// The team that owns the application.
55 pub team: Option<Box<Team>>,
56 /// The guild associated with the application (e.g. its support server).
57 #[serde(default, skip_serializing_if = "Option::is_none")]
58 pub guild_id: Option<GuildId>,
59 /// The associated guild.
60 #[serde(default, skip_serializing_if = "Option::is_none")]
61 pub guild: Option<Box<PartialGuild>>,
62 /// For games sold on Discord, the "Game SKU".
63 #[serde(default, skip_serializing_if = "Option::is_none")]
64 pub primary_sku_id: Option<SkuId>,
65 /// For games sold on Discord, the URL slug of the store page.
66 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub slug: Option<CompactString>,
68 /// The default rich presence invite [cover image hash](https://docs.discord.com/developers/reference#image-formatting).
69 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub cover_image: Option<ImageHash>,
71 /// The application's public flags (legacy integer field).
72 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub flags: Option<ApplicationFlags>,
74 /// The application's public flags, sent as a decimal string.
75 #[serde(
76 default,
77 skip_serializing_if = "Option::is_none",
78 with = "crate::serde_util::option_flags_string"
79 )]
80 pub flags_new: Option<ApplicationFlags>,
81 /// The approximate number of guilds the app is in.
82 #[serde(default, skip_serializing_if = "Option::is_none")]
83 pub approximate_guild_count: Option<u32>,
84 /// The approximate number of users who installed the app.
85 #[serde(default, skip_serializing_if = "Option::is_none")]
86 pub approximate_user_install_count: Option<u32>,
87 /// The approximate number of users with an `OAuth2` authorization for the app.
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub approximate_user_authorization_count: Option<u32>,
90 /// The `OAuth2` redirect URIs.
91 #[serde(default, skip_serializing_if = "Vec::is_empty")]
92 pub redirect_uris: Vec<CompactString>,
93 /// The [interactions endpoint URL](https://docs.discord.com/developers/interactions/receiving-and-responding#receiving-an-interaction).
94 #[serde(default, skip_serializing_if = "Option::is_none")]
95 pub interactions_endpoint_url: Option<CompactString>,
96 /// The role connection (linked roles) verification URL.
97 #[serde(default, skip_serializing_if = "Option::is_none")]
98 pub role_connections_verification_url: Option<CompactString>,
99 /// The URL that receives [webhook events](https://docs.discord.com/developers/events/webhook-events).
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub event_webhooks_url: Option<CompactString>,
102 /// Whether webhook events are enabled.
103 #[serde(default, skip_serializing_if = "Option::is_none")]
104 pub event_webhooks_status: Option<EventWebhookStatus>,
105 /// The webhook events the app subscribes to.
106 #[serde(default, skip_serializing_if = "Vec::is_empty")]
107 pub event_webhooks_types: Vec<WebhookEventType>,
108 /// Up to 5 tags describing the app.
109 #[serde(default, skip_serializing_if = "Vec::is_empty")]
110 pub tags: Vec<CompactString>,
111 /// The settings of the app's default in-app authorization link.
112 #[serde(default, skip_serializing_if = "Option::is_none")]
113 pub install_params: Option<InstallParams>,
114 /// The default scopes and permissions per installation context.
115 #[serde(default, skip_serializing_if = "Option::is_none")]
116 pub integration_types_config: Option<IntegrationTypesConfig>,
117 /// The custom authorization URL, if set.
118 #[serde(default, skip_serializing_if = "Option::is_none")]
119 pub custom_install_url: Option<CompactString>,
120}
121
122/// A partial [`Application`], as attached to invites (`target_application`) (D-051).
123///
124/// discord.js counterpart: a partial `APIApplication` (discord-api-types), as `IntegrationApplication`.
125#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
126#[non_exhaustive]
127pub struct PartialApplication {
128 /// The application's ID.
129 pub id: ApplicationId,
130 /// The application's name.
131 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub name: Option<CompactString>,
133 /// The [icon hash](https://docs.discord.com/developers/reference#image-formatting).
134 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub icon: Option<ImageHash>,
136 /// The application's description.
137 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub description: Option<CompactString>,
139 /// The [cover image hash](https://docs.discord.com/developers/reference#image-formatting).
140 #[serde(default, skip_serializing_if = "Option::is_none")]
141 pub cover_image: Option<ImageHash>,
142 /// The application's public flags.
143 #[serde(default, skip_serializing_if = "Option::is_none")]
144 pub flags: Option<ApplicationFlags>,
145}
146
147int_enum! {
148 /// [Where an app can be installed](https://docs.discord.com/developers/resources/application#application-object-application-integration-types).
149 ///
150 /// discord.js counterpart: `ApplicationIntegrationType`.
151 #[doc(alias = "ApplicationIntegrationType")]
152 pub enum IntegrationType: u8 {
153 /// In guilds (servers).
154 GuildInstall = 0,
155 /// For users (the app follows the user everywhere).
156 UserInstall = 1,
157 }
158}
159
160/// The default install settings per installation context; the keys of
161/// [`integration_types_config`](Application::integration_types_config).
162///
163/// discord.js counterpart: `APIApplicationIntegrationTypesConfigMap` (discord-api-types).
164#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
165#[non_exhaustive]
166pub struct IntegrationTypesConfig {
167 /// For guild installs (key `"0"`).
168 #[serde(rename = "0", default, skip_serializing_if = "Option::is_none")]
169 pub guild_install: Option<IntegrationTypeConfig>,
170 /// For user installs (key `"1"`).
171 #[serde(rename = "1", default, skip_serializing_if = "Option::is_none")]
172 pub user_install: Option<IntegrationTypeConfig>,
173}
174
175/// The [install settings of one installation context](https://docs.discord.com/developers/resources/application#application-object-application-integration-type-configuration-object).
176///
177/// discord.js counterpart: `APIApplicationIntegrationTypeConfiguration` (discord-api-types).
178#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
179#[non_exhaustive]
180pub struct IntegrationTypeConfig {
181 /// The scopes and permissions of the context's default in-app authorization link.
182 #[serde(default, skip_serializing_if = "Option::is_none")]
183 pub oauth2_install_params: Option<InstallParams>,
184}
185
186/// The [settings of an in-app authorization link](https://docs.discord.com/developers/resources/application#install-params-object).
187///
188/// discord.js counterpart: `ClientApplicationInstallParams`.
189#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
190#[non_exhaustive]
191pub struct InstallParams {
192 /// The scopes to authorize.
193 pub scopes: Vec<Scope>,
194 /// The permissions requested for the bot role.
195 pub permissions: Permissions,
196}
197
198impl InstallParams {
199 /// An authorization link for `scopes`, requesting `permissions` for the bot role (only
200 /// used with the `bot` scope).
201 ///
202 /// discord.js counterpart: the `scopes` and `permissions` of `InviteGenerationOptions`.
203 #[must_use]
204 pub fn new(scopes: impl IntoIterator<Item = Scope>, permissions: Permissions) -> Self {
205 Self {
206 scopes: scopes.into_iter().collect(),
207 permissions,
208 }
209 }
210}
211
212int_enum! {
213 /// [Whether webhook events are enabled](https://docs.discord.com/developers/resources/application#application-object-application-event-webhook-status).
214 ///
215 /// discord.js counterpart: `ApplicationWebhookEventStatus`.
216 pub enum EventWebhookStatus: u8 {
217 /// Disabled by the developer (the default).
218 Disabled = 1,
219 /// Enabled by the developer.
220 Enabled = 2,
221 /// Disabled by Discord, usually because of inactivity.
222 DisabledByDiscord = 3,
223 }
224}
225
226/// A running [Activity instance](https://docs.discord.com/developers/resources/application#get-application-activity-instance-activity-instance-object).
227///
228/// discord.js counterpart: `ActivityInstance`.
229#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
230#[non_exhaustive]
231pub struct ActivityInstance {
232 /// The application.
233 pub application_id: ApplicationId,
234 /// The instance's ID.
235 pub instance_id: CompactString,
236 /// The unique ID of the launch.
237 pub launch_id: GenericId,
238 /// Where the instance runs.
239 pub location: ActivityLocation,
240 /// The users connected to the instance.
241 pub users: Vec<UserId>,
242}
243
244/// [Where an Activity instance runs](https://docs.discord.com/developers/resources/application#get-application-activity-instance-activity-location-object).
245///
246/// discord.js counterpart: `ActivityLocation`.
247#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
248#[non_exhaustive]
249pub struct ActivityLocation {
250 /// The location's ID.
251 pub id: CompactString,
252 /// Whether the location is a guild channel or a private channel.
253 pub kind: ActivityLocationKind,
254 /// The channel.
255 pub channel_id: ChannelId,
256 /// The guild, for guild channels.
257 #[serde(default, skip_serializing_if = "Option::is_none")]
258 pub guild_id: Option<GuildId>,
259}
260
261str_enum! {
262 /// The [kind of an Activity location](https://docs.discord.com/developers/resources/application#get-application-activity-instance-activity-location-kind-enum).
263 ///
264 /// discord.js counterpart: `ActivityLocationKind` (discord-api-types).
265 pub enum ActivityLocationKind {
266 /// A guild channel.
267 GuildChannel = "gc",
268 /// A private channel (DM or group DM).
269 PrivateChannel = "pc",
270 }
271}
272
273int_flags! {
274 /// [Flags of an application](https://docs.discord.com/developers/resources/application#application-object-application-flags).
275 ///
276 /// The `GATEWAY_*` flags tell whether the app may use the privileged
277 /// [intents](crate::gateway::Intents): the plain flag is granted after approval (bots in 100
278 /// or more servers), the `*_LIMITED` flag when it is enabled in the Developer Portal for a
279 /// bot in fewer than 100 servers.
280 ///
281 /// discord.js counterpart: `ApplicationFlags` / `ApplicationFlagsBitField`.
282 ///
283 /// # Examples
284 ///
285 /// ```
286 /// use djs_model::application::ApplicationFlags;
287 ///
288 /// let flags: ApplicationFlags = serde_json::from_str("532480").unwrap();
289 /// assert!(flags.contains(ApplicationFlags::GATEWAY_MESSAGE_CONTENT_LIMITED));
290 /// ```
291 pub struct ApplicationFlags: u64 {
292 /// The app's embedded activity has been released.
293 ///
294 /// **Undocumented:** known from discord-api-types, not in the official docs; may
295 /// change without notice.
296 const EMBEDDED_RELEASED = 1 << 1;
297 /// The app can manage emojis.
298 ///
299 /// **Undocumented:** known from discord-api-types, not in the official docs; may
300 /// change without notice.
301 const MANAGED_EMOJI = 1 << 2;
302 /// The app's embedded activity supports in-app purchases.
303 ///
304 /// **Undocumented:** known from discord-api-types, not in the official docs; may
305 /// change without notice.
306 const EMBEDDED_IAP = 1 << 3;
307 /// The app can create group DMs.
308 ///
309 /// **Undocumented:** known from discord-api-types, not in the official docs; may
310 /// change without notice.
311 const GROUP_DM_CREATE = 1 << 4;
312 /// The app uses the [Auto Moderation API](https://docs.discord.com/developers/resources/auto-moderation)
313 /// (shown as a badge on its profile).
314 const APPLICATION_AUTO_MODERATION_RULE_CREATE_BADGE = 1 << 6;
315 /// The app has connected to RPC.
316 ///
317 /// **Undocumented:** known from discord-api-types, not in the official docs; may
318 /// change without notice.
319 const RPC_HAS_CONNECTED = 1 << 11;
320 /// Approved for the privileged presence intent (bots in 100 or more servers).
321 const GATEWAY_PRESENCE = 1 << 12;
322 /// Presence intent enabled in the Developer Portal (bots in fewer than 100 servers).
323 const GATEWAY_PRESENCE_LIMITED = 1 << 13;
324 /// Approved for the privileged guild members intent (bots in 100 or more servers).
325 const GATEWAY_GUILD_MEMBERS = 1 << 14;
326 /// Guild members intent enabled in the Developer Portal (bots in fewer than 100
327 /// servers).
328 const GATEWAY_GUILD_MEMBERS_LIMITED = 1 << 15;
329 /// Unusual growth of the app prevents its verification.
330 const VERIFICATION_PENDING_GUILD_LIMIT = 1 << 16;
331 /// The app is embedded in the Discord client (not publicly available).
332 const EMBEDDED = 1 << 17;
333 /// Approved for the privileged message content intent (bots in 100 or more servers).
334 const GATEWAY_MESSAGE_CONTENT = 1 << 18;
335 /// Message content intent enabled in the Developer Portal (bots in fewer than 100
336 /// servers).
337 const GATEWAY_MESSAGE_CONTENT_LIMITED = 1 << 19;
338 /// The embedded app is made by Discord.
339 ///
340 /// **Undocumented:** known from discord-api-types, not in the official docs; may
341 /// change without notice.
342 const EMBEDDED_FIRST_PARTY = 1 << 20;
343 /// The app has registered global application commands (shown as a badge on its
344 /// profile).
345 const APPLICATION_COMMAND_BADGE = 1 << 23;
346 }
347}