Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] New ModifyCurrentApplication features #4

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
39 changes: 34 additions & 5 deletions src/Discord.Net.Core/Entities/Applications/ApplicationFlags.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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,32 @@
using System;
using System.Linq;
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
{
/// <summary>
/// Gets the scopes to install this application.
/// </summary>
public IReadOnlyCollection<string> Scopes { get; }
public IReadOnlyCollection<string> Scopes { get; }

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

internal ApplicationInstallParams(string[] scopes, GuildPermission? permission)
public ApplicationInstallParams(string[] scopes, GuildPermission permission)
{
Preconditions.NotNull(scopes, nameof(scopes));
foreach (var scope in scopes)
{
Scopes = scopes.ToImmutableArray();
Permission = permission;
Preconditions.NotNullOrEmpty(scope, nameof(scope));
}
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,28 @@ 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; }
}
19 changes: 7 additions & 12 deletions src/Discord.Net.Rest/API/Common/InstallParams.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using System.Text.Json;
using System.Text.Json.Serialization;
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
{
[JsonPropertyName("scopes")]
public string[] Scopes { get; set; } = Array.Empty<string>();
[JsonPropertyName("permissions")]
public ulong Permission { get; set; }
}
[JsonPropertyName("scopes")]
public string[] Scopes { get; set; } = Array.Empty<string>();
[JsonPropertyName("permissions")]
public ulong Permission { get; set; }
}
25 changes: 18 additions & 7 deletions src/Discord.Net.Rest/API/Rest/ModifyCurrentApplicationBotParams.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Discord.API.Rest;

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

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

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

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

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

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

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

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

[JsonPropertyName("tags")]
public Optional<string[]> Tags { get; set; }
}
14 changes: 13 additions & 1 deletion src/Discord.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Discord.API;
using Discord.API.Rest;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,9 +29,12 @@ 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)
Preconditions.AtMost(args.Description.Value.Length, DiscordConfig.MaxApplicationDescriptionLength, nameof(args.Description), $"An application description tag mus have length less or equal to {DiscordConfig.MaxApplicationDescriptionLength}");
Expand All @@ -42,6 +46,14 @@ 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.Map(a => new InstallParams
{
Permission = (ulong)args.InstallParams.Value.Permission,
Scopes = args.InstallParams.Value.Scopes.ToArray()
})
}, 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 Down Expand Up @@ -92,8 +94,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
.Map(p => new ApplicationInstallParams(p.Scopes, (GuildPermission)p.Permission))
.GetValueOrDefault(null);

if (model.Flags.IsSpecified)
Flags = model.Flags.Value;
Expand Down