Skip to content

Commit

Permalink
[Feature] New ModifyCurrentApplication features (#2730)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Misha-133 and quinchs committed Aug 10, 2023
1 parent 59094d2 commit 166d40f
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 44 deletions.
5 changes: 5 additions & 0 deletions src/Discord.Net.Core/DiscordConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,10 @@ public class DiscordConfig
/// Returns the max length of an application description.
/// </summary>
public const int MaxApplicationDescriptionLength = 400;

/// <summary>
/// Returns the max amount of tags applied to an application.
/// </summary>
public const int MaxApplicationTagCount = 5;
}
}
34 changes: 34 additions & 0 deletions src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,62 @@ namespace Discord;
/// <summary>
/// Represents public flags for an application.
/// </summary>
[Flags]
public enum ApplicationFlags
{
/// <summary>
/// Indicates if an app uses the Auto Moderation API.
/// </summary>
UsesAutoModApi = 1 << 6,

/// <summary>
/// Indicates that the app has been verified to use GUILD_PRESENCES intent.
/// </summary>
GatewayPresence = 1 << 12,

/// <summary>
/// Indicates that the app has enabled the GUILD_PRESENCES intent on a bot in less than 100 servers.
/// </summary>
GatewayPresenceLimited = 1 << 13,

/// <summary>
/// Indicates that the app has been verified to use GUILD_MEMBERS intent.
/// </summary>
GatewayGuildMembers = 1 << 14,

/// <summary>
/// Indicates that the app has enabled the GUILD_MEMBERS intent on a bot in less than 100 servers.
/// </summary>
GatewayGuildMembersLimited = 1 << 15,

/// <summary>
/// Indicates unusual growth of an app that prevents verification.
/// </summary>
VerificationPendingGuildLimit = 1 << 16,

/// <summary>
/// Indicates if an app is embedded within the Discord client.
/// </summary>
Embedded = 1 << 17,

/// <summary>
/// Indicates that the app has been verified to use MESSAGE_CONTENT intent.
/// </summary>
GatewayMessageContent = 1 << 18,

/// <summary>
/// Indicates that the app has enabled the MESSAGE_CONTENT intent on a bot in less than 100 servers.
/// </summary>
GatewayMessageContentLimited = 1 << 19,

/// <summary>
/// Indicates if an app has registered global application commands.
/// </summary>
ApplicationCommandBadge = 1 << 23,

/// <summary>
/// Indicates if an app is considered active.
/// </summary>
ActiveApplication = 1 << 24
}

Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Represents install parameters for an application.
/// </summary>
public class ApplicationInstallParams
{
/// <summary>
/// Represents install parameters for an application.
/// Gets the scopes to install this application.
/// </summary>
public class ApplicationInstallParams
public IReadOnlyCollection<string> Scopes { get; }

/// <summary>
/// Gets the default permissions to install this application.
/// </summary>
public GuildPermission Permission { get; }

public ApplicationInstallParams(string[] scopes, GuildPermission permission)
{
/// <summary>
/// Gets the scopes to install this application.
/// </summary>
public IReadOnlyCollection<string> Scopes { get; }
Preconditions.NotNull(scopes, nameof(scopes));

/// <summary>
/// Gets the default permissions to install this application
/// </summary>
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;
}
}
2 changes: 1 addition & 1 deletion src/Discord.Net.Core/Entities/Applications/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IApplication : ISnowflakeEntity
/// </summary>
ApplicationFlags Flags { get; }
/// <summary>
/// Gets a collection of install parameters for this application.
/// Gets a collection of install parameters for this application; <see langword="null"/> if disabled.
/// </summary>
ApplicationInstallParams InstallParams { get; }
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,29 @@ public class ModifyApplicationProperties
/// Gets or sets the icon of the application.
/// </summary>
public Optional<Image?> Icon { get; set; }

/// <summary>
/// Gets or sets the default rich presence invite cover image of the application.
/// </summary>
public Optional<Image?> CoverImage { get; set; }

/// <summary>
/// Gets or set the default custom authorization URL for the app, if enabled.
/// </summary>
public Optional<string> CustomInstallUrl { get; set; }

/// <summary>
/// Gets or sets settings for the app's default in-app authorization link, if enabled.
/// </summary>
public Optional<ApplicationInstallParams> InstallParams { get; set; }

/// <summary>
/// Gets or sets app's public flags.
/// </summary>
/// <remarks>
/// Only <see cref="ApplicationFlags.GatewayGuildMembersLimited"/>, <see cref="ApplicationFlags.GatewayMessageContentLimited"/> and
/// <see cref="ApplicationFlags.GatewayPresenceLimited"/> flags can be updated.
/// </remarks>
public Optional<ApplicationFlags> Flags { get; set; }

}
21 changes: 8 additions & 13 deletions src/Discord.Net.Rest/API/Common/InstallParams.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
24 changes: 18 additions & 6 deletions src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ namespace Discord.API.Rest;

internal class ModifyCurrentApplicationBotParams
{
[JsonProperty("interactions_endpoint_url")]
public Optional<string> InteractionsEndpointUrl { get; set; }
[JsonProperty("custom_install_url")]
public Optional<string> CustomInstallUrl { get; set; }

[JsonProperty("description")]
public Optional<string> Description { get; set; }

[JsonProperty("role_connections_verification_url")]
public Optional<string> RoleConnectionsEndpointUrl { get; set; }

[JsonProperty("description")]
public Optional<string> Description { get; set; }
[JsonProperty("install_params")]
public Optional<InstallParams> InstallParams { get; set; }

[JsonProperty("tags")]
public Optional<string[]> Tags { get; set; }
[JsonProperty("flags")]
public Optional<ApplicationFlags> Flags { get; set; }

[JsonProperty("icon")]
public Optional<Image?> Icon { get; set; }

[JsonProperty("cover_image")]
public Optional<Image?> CoverImage { get; set; }

[JsonProperty("interactions_endpoint_url")]
public Optional<string> InteractionsEndpointUrl { get; set; }

[JsonProperty("tags")]
public Optional<string[]> Tags { get; set; }
}
21 changes: 19 additions & 2 deletions src/Discord.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Discord.API;
using Discord.API.Rest;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -28,11 +30,14 @@ public static async Task<RestApplication> GetCurrentBotApplicationAsync(BaseDisc
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()
Expand All @@ -42,6 +47,18 @@ public static async Task<RestApplication> GetCurrentBotApplicationAsync(BaseDisc
Icon = args.Icon.IsSpecified ? args.Icon.Value?.ToModel() : Optional<API.Image?>.Unspecified,
InteractionsEndpointUrl = args.InteractionsEndpointUrl,
RoleConnectionsEndpointUrl = args.RoleConnectionsEndpointUrl,
Flags = args.Flags,
CoverImage = args.CoverImage.IsSpecified ? args.CoverImage.Value?.ToModel() : Optional<API.Image?>.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<InstallParams>.Unspecified,
}, options);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Discord.Net.Rest/Entities/RestApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public class RestApplication : RestEntity<ulong>, IApplication
/// <inheritdoc />
public string InteractionsEndpointUrl { get; private set; }

/// <inheritdoc />
public ApplicationInstallParams InstallParams { get; private set; }

/// <inheritdoc />
public IReadOnlyCollection<string> Tags { get; private set; }

internal RestApplication(BaseDiscordClient discord, ulong id)
Expand All @@ -86,8 +88,10 @@ internal void Update(Model model)
Tags = model.Tags.GetValueOrDefault(null)?.ToImmutableArray() ?? ImmutableArray<string>.Empty;
PrivacyPolicy = model.PrivacyPolicy;
TermsOfService = model.TermsOfService;
var installParams = model.InstallParams.GetValueOrDefault(null);
InstallParams = new ApplicationInstallParams(installParams?.Scopes ?? Array.Empty<string>(), (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;
Expand Down

0 comments on commit 166d40f

Please sign in to comment.