From 166d40f1e4211cda04bcb7a3b698f440baa0d682 Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:18:06 +0300 Subject: [PATCH] [Feature] New `ModifyCurrentApplication` features (#2730) * initial commit * apply suggestings lol * Update src/Discord.Net.Core/Entities/Applications/IApplication.cs Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> * check for null values inside the array --------- Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> --- src/Discord.Net.Core/DiscordConfig.cs | 5 +++ .../Entities/Applications/ApplicationFlags.cs | 34 ++++++++++++++++ .../Applications/ApplicationInstallParams.cs | 40 +++++++++---------- .../Entities/Applications/IApplication.cs | 2 +- .../ModifyApplicationProperties.cs | 25 ++++++++++++ .../API/Common/InstallParams.cs | 21 ++++------ .../Rest/ModifyCurrentApplicationBotParams.cs | 26 ++++++++---- src/Discord.Net.Rest/ClientHelper.cs | 21 +++++++++- .../Entities/RestApplication.cs | 8 +++- 9 files changed, 137 insertions(+), 45 deletions(-) diff --git a/src/Discord.Net.Core/DiscordConfig.cs b/src/Discord.Net.Core/DiscordConfig.cs index 46fc08ff..6957c02f 100644 --- a/src/Discord.Net.Core/DiscordConfig.cs +++ b/src/Discord.Net.Core/DiscordConfig.cs @@ -232,5 +232,10 @@ namespace Discord /// Returns the max length of an application description. /// public const int MaxApplicationDescriptionLength = 400; + + /// + /// Returns the max amount of tags applied to an application. + /// + public const int MaxApplicationTagCount = 5; } } diff --git a/src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs b/src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs index 0c6cfe7e..b11467ce 100644 --- a/src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs +++ b/src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs @@ -9,28 +9,62 @@ namespace Discord; /// /// Represents public flags for an application. /// +[Flags] public enum ApplicationFlags { + /// + /// Indicates if an app uses the Auto Moderation API. + /// UsesAutoModApi = 1 << 6, + /// + /// Indicates that the app has been verified to use GUILD_PRESENCES intent. + /// GatewayPresence = 1 << 12, + /// + /// Indicates that the app has enabled the GUILD_PRESENCES intent on a bot in less than 100 servers. + /// GatewayPresenceLimited = 1 << 13, + /// + /// Indicates that the app has been verified to use GUILD_MEMBERS intent. + /// GatewayGuildMembers = 1 << 14, + /// + /// Indicates that the app has enabled the GUILD_MEMBERS intent on a bot in less than 100 servers. + /// GatewayGuildMembersLimited = 1 << 15, + /// + /// Indicates unusual growth of an app that prevents verification. + /// VerificationPendingGuildLimit = 1 << 16, + /// + /// Indicates if an app is embedded within the Discord client. + /// Embedded = 1 << 17, + /// + /// Indicates that the app has been verified to use MESSAGE_CONTENT intent. + /// GatewayMessageContent = 1 << 18, + /// + /// Indicates that the app has enabled the MESSAGE_CONTENT intent on a bot in less than 100 servers. + /// GatewayMessageContentLimited = 1 << 19, + /// + /// Indicates if an app has registered global application commands. + /// ApplicationCommandBadge = 1 << 23, + /// + /// Indicates if an app is considered active. + /// ActiveApplication = 1 << 24 } diff --git a/src/Discord.Net.Core/Entities/Applications/ApplicationInstallParams.cs b/src/Discord.Net.Core/Entities/Applications/ApplicationInstallParams.cs index 180592f1..672cb8ec 100644 --- a/src/Discord.Net.Core/Entities/Applications/ApplicationInstallParams.cs +++ b/src/Discord.Net.Core/Entities/Applications/ApplicationInstallParams.cs @@ -1,31 +1,31 @@ -using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Discord +namespace Discord; + +/// +/// Represents install parameters for an application. +/// +public class ApplicationInstallParams { /// - /// Represents install parameters for an application. + /// Gets the scopes to install this application. /// - public class ApplicationInstallParams + public IReadOnlyCollection Scopes { get; } + + /// + /// Gets the default permissions to install this application. + /// + public GuildPermission Permission { get; } + + public ApplicationInstallParams(string[] scopes, GuildPermission permission) { - /// - /// Gets the scopes to install this application. - /// - public IReadOnlyCollection Scopes { get; } + Preconditions.NotNull(scopes, nameof(scopes)); - /// - /// Gets the default permissions to install this application - /// - public GuildPermission? Permission { get; } + foreach (var s in scopes) + Preconditions.NotNull(s, nameof(scopes)); - internal ApplicationInstallParams(string[] scopes, GuildPermission? permission) - { - Scopes = scopes.ToImmutableArray(); - Permission = permission; - } + Scopes = scopes.ToImmutableArray(); + Permission = permission; } } diff --git a/src/Discord.Net.Core/Entities/Applications/IApplication.cs b/src/Discord.Net.Core/Entities/Applications/IApplication.cs index 5e2aaf0d..c9a31f9c 100644 --- a/src/Discord.Net.Core/Entities/Applications/IApplication.cs +++ b/src/Discord.Net.Core/Entities/Applications/IApplication.cs @@ -24,7 +24,7 @@ namespace Discord /// ApplicationFlags Flags { get; } /// - /// Gets a collection of install parameters for this application. + /// Gets a collection of install parameters for this application; if disabled. /// ApplicationInstallParams InstallParams { get; } /// diff --git a/src/Discord.Net.Core/Entities/Applications/ModifyApplicationProperties.cs b/src/Discord.Net.Core/Entities/Applications/ModifyApplicationProperties.cs index be6fdef0..2306752c 100644 --- a/src/Discord.Net.Core/Entities/Applications/ModifyApplicationProperties.cs +++ b/src/Discord.Net.Core/Entities/Applications/ModifyApplicationProperties.cs @@ -29,4 +29,29 @@ public class ModifyApplicationProperties /// Gets or sets the icon of the application. /// public Optional Icon { get; set; } + + /// + /// Gets or sets the default rich presence invite cover image of the application. + /// + public Optional CoverImage { get; set; } + + /// + /// Gets or set the default custom authorization URL for the app, if enabled. + /// + public Optional CustomInstallUrl { get; set; } + + /// + /// Gets or sets settings for the app's default in-app authorization link, if enabled. + /// + public Optional InstallParams { get; set; } + + /// + /// Gets or sets app's public flags. + /// + /// + /// Only , and + /// flags can be updated. + /// + public Optional Flags { get; set; } + } diff --git a/src/Discord.Net.Rest/API/Common/InstallParams.cs b/src/Discord.Net.Rest/API/Common/InstallParams.cs index 1fb987f3..d610dd92 100644 --- a/src/Discord.Net.Rest/API/Common/InstallParams.cs +++ b/src/Discord.Net.Rest/API/Common/InstallParams.cs @@ -1,17 +1,12 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Discord.API +namespace Discord.API; + +internal class InstallParams { - internal class InstallParams - { - [JsonProperty("scopes")] - public string[] Scopes { get; set; } - [JsonProperty("permissions")] - public ulong Permission { get; set; } - } + [JsonProperty("scopes")] + public string[] Scopes { get; set; } + + [JsonProperty("permissions")] + public ulong Permission { get; set; } } diff --git a/src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs index fdd665b2..3284827f 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs +++ b/src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs @@ -4,18 +4,30 @@ namespace Discord.API.Rest; internal class ModifyCurrentApplicationBotParams { - [JsonProperty("interactions_endpoint_url")] - public Optional InteractionsEndpointUrl { get; set; } - - [JsonProperty("role_connections_verification_url")] - public Optional RoleConnectionsEndpointUrl { get; set; } + [JsonProperty("custom_install_url")] + public Optional CustomInstallUrl { get; set; } [JsonProperty("description")] public Optional Description { get; set; } - [JsonProperty("tags")] - public Optional Tags { get; set; } + [JsonProperty("role_connections_verification_url")] + public Optional RoleConnectionsEndpointUrl { get; set; } + + [JsonProperty("install_params")] + public Optional InstallParams { get; set; } + + [JsonProperty("flags")] + public Optional Flags { get; set; } [JsonProperty("icon")] public Optional Icon { get; set; } + + [JsonProperty("cover_image")] + public Optional CoverImage { get; set; } + + [JsonProperty("interactions_endpoint_url")] + public Optional InteractionsEndpointUrl { get; set; } + + [JsonProperty("tags")] + public Optional Tags { get; set; } } diff --git a/src/Discord.Net.Rest/ClientHelper.cs b/src/Discord.Net.Rest/ClientHelper.cs index 1d95c2c1..1fb7b38e 100644 --- a/src/Discord.Net.Rest/ClientHelper.cs +++ b/src/Discord.Net.Rest/ClientHelper.cs @@ -1,4 +1,6 @@ +using Discord.API; using Discord.API.Rest; + using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -28,11 +30,14 @@ namespace Discord.Rest var args = new ModifyApplicationProperties(); func(args); - if(args.Tags.IsSpecified) + if (args.Tags.IsSpecified) + { + Preconditions.AtMost(args.Tags.Value.Length, DiscordConfig.MaxApplicationTagCount, nameof(args.Tags), $"An application can have a maximum of {DiscordConfig.MaxApplicationTagCount} applied."); foreach (var tag in args.Tags.Value) Preconditions.AtMost(tag.Length, DiscordConfig.MaxApplicationTagLength, nameof(args.Tags), $"An application tag must have length less or equal to {DiscordConfig.MaxApplicationTagLength}"); + } - if(args.Description.IsSpecified) + if (args.Description.IsSpecified) Preconditions.AtMost(args.Description.Value.Length, DiscordConfig.MaxApplicationDescriptionLength, nameof(args.Description), $"An application description tag mus have length less or equal to {DiscordConfig.MaxApplicationDescriptionLength}"); return await client.ApiClient.ModifyCurrentBotApplicationAsync(new() @@ -42,6 +47,18 @@ namespace Discord.Rest Icon = args.Icon.IsSpecified ? args.Icon.Value?.ToModel() : Optional.Unspecified, InteractionsEndpointUrl = args.InteractionsEndpointUrl, RoleConnectionsEndpointUrl = args.RoleConnectionsEndpointUrl, + Flags = args.Flags, + CoverImage = args.CoverImage.IsSpecified ? args.CoverImage.Value?.ToModel() : Optional.Unspecified, + CustomInstallUrl = args.CustomInstallUrl, + InstallParams = args.InstallParams.IsSpecified + ? args.InstallParams.Value is null + ? null + : new InstallParams + { + Permission = (ulong)args.InstallParams.Value.Permission, + Scopes = args.InstallParams.Value.Scopes.ToArray() + } + : Optional.Unspecified, }, options); } diff --git a/src/Discord.Net.Rest/Entities/RestApplication.cs b/src/Discord.Net.Rest/Entities/RestApplication.cs index 4b50fafd..b35322f9 100644 --- a/src/Discord.Net.Rest/Entities/RestApplication.cs +++ b/src/Discord.Net.Rest/Entities/RestApplication.cs @@ -61,8 +61,10 @@ namespace Discord.Rest /// public string InteractionsEndpointUrl { get; private set; } + /// public ApplicationInstallParams InstallParams { get; private set; } + /// public IReadOnlyCollection Tags { get; private set; } internal RestApplication(BaseDiscordClient discord, ulong id) @@ -86,8 +88,10 @@ namespace Discord.Rest Tags = model.Tags.GetValueOrDefault(null)?.ToImmutableArray() ?? ImmutableArray.Empty; PrivacyPolicy = model.PrivacyPolicy; TermsOfService = model.TermsOfService; - var installParams = model.InstallParams.GetValueOrDefault(null); - InstallParams = new ApplicationInstallParams(installParams?.Scopes ?? Array.Empty(), (GuildPermission?)installParams?.Permission); + + InstallParams = model.InstallParams.IsSpecified + ? new ApplicationInstallParams(model.InstallParams.Value.Scopes, (GuildPermission)model.InstallParams.Value.Permission) + : null; if (model.Flags.IsSpecified) Flags = model.Flags.Value;