Skip to content

Commit

Permalink
Implement application editing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Oct 29, 2023
1 parent 72b44c3 commit f8c454d
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public interface IApplication : IPartialApplication
/// </summary>
new bool DoesBotRequireCodeGrant { get; }

/// <summary>
/// Gets the user object for the bot user associated with the application.
/// </summary>
new Optional<IPartialUser> Bot { get; }

/// <summary>
/// Gets the URL to the application's terms of service.
/// </summary>
Expand Down Expand Up @@ -128,6 +133,23 @@ public interface IApplication : IPartialApplication
/// </summary>
new Optional<int> ApproximateGuildCount { get; }

/// <summary>
/// Gets the redirect URIs for the application.
/// </summary>
new Optional<IReadOnlyList<Uri>> RedirectUris { get; }

/// <summary>
/// Gets the interaction endpoint URL for the application.
/// </summary>
new Optional<Uri> InteractionsEndpointUrl { get; }

/// <summary>
/// Gets the application's role connection verification entry point,
/// which when configured will render the app as a verification method
/// in the guild role verification configuration.
/// </summary>
new Optional<Uri> RoleConnectionsVerificationUrl { get; }

/// <summary>
/// Gets up to 5 tags describing the content and functionality of the application.
/// </summary>
Expand All @@ -143,13 +165,6 @@ public interface IApplication : IPartialApplication
/// </summary>
new Optional<Uri> CustomInstallUrl { get; }

/// <summary>
/// Gets the application's role connection verification entry point,
/// which when configured will render the app as a verification method
/// in the guild role verification configuration.
/// </summary>
new Optional<Uri> RoleConnectionsVerificationUrl { get; }

/// <inheritdoc/>
Optional<Snowflake> IPartialApplication.ID => this.ID;

Expand All @@ -171,6 +186,9 @@ public interface IApplication : IPartialApplication
/// <inheritdoc/>
Optional<bool> IPartialApplication.DoesBotRequireCodeGrant => this.DoesBotRequireCodeGrant;

/// <inheritdoc/>
Optional<IPartialUser> IPartialApplication.Bot => this.Bot;

/// <inheritdoc/>
Optional<string> IPartialApplication.TermsOfServiceURL => this.TermsOfServiceURL;

Expand Down Expand Up @@ -207,6 +225,15 @@ public interface IApplication : IPartialApplication
/// <inheritdoc/>
Optional<int> IPartialApplication.ApproximateGuildCount => this.ApproximateGuildCount;

/// <inheritdoc/>
Optional<IReadOnlyList<Uri>> IPartialApplication.RedirectUris => this.RedirectUris;

/// <inheritdoc/>
Optional<Uri> IPartialApplication.InteractionsEndpointUrl => this.InteractionsEndpointUrl;

/// <inheritdoc/>
Optional<Uri> IPartialApplication.RoleConnectionsVerificationUrl => this.RoleConnectionsVerificationUrl;

/// <inheritdoc/>
Optional<IReadOnlyList<string>> IPartialApplication.Tags => this.Tags;

Expand All @@ -215,7 +242,4 @@ public interface IApplication : IPartialApplication

/// <inheritdoc/>
Optional<Uri> IPartialApplication.CustomInstallUrl => this.CustomInstallUrl;

/// <inheritdoc/>
Optional<Uri> IPartialApplication.RoleConnectionsVerificationUrl => this.RoleConnectionsVerificationUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public interface IPartialApplication
/// <inheritdoc cref="IApplication.DoesBotRequireCodeGrant" />
Optional<bool> DoesBotRequireCodeGrant { get; }

/// <inheritdoc cref="IApplication.Bot"/>
Optional<IPartialUser> Bot { get; }

/// <inheritdoc cref="IApplication.TermsOfServiceURL" />
Optional<string> TermsOfServiceURL { get; }

Expand Down Expand Up @@ -90,6 +93,15 @@ public interface IPartialApplication
/// <inheritdoc cref="IApplication.ApproximateGuildCount" />
Optional<int> ApproximateGuildCount { get; }

/// <inheritdoc cref="IApplication.RedirectUris" />
Optional<IReadOnlyList<Uri>> RedirectUris { get; }

/// <inheritdoc cref="IApplication.InteractionsEndpointUrl" />
Optional<Uri> InteractionsEndpointUrl { get; }

/// <inheritdoc cref="IApplication.RoleConnectionsVerificationUrl" />
Optional<Uri> RoleConnectionsVerificationUrl { get; }

/// <inheritdoc cref="IApplication.Tags" />
Optional<IReadOnlyList<string>> Tags { get; }

Expand All @@ -98,7 +110,4 @@ public interface IPartialApplication

/// <inheritdoc cref="IApplication.CustomInstallUrl" />
Optional<Uri> CustomInstallUrl { get; }

/// <inheritdoc cref="IApplication.RoleConnectionsVerificationUrl" />
Optional<Uri> RoleConnectionsVerificationUrl { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -379,4 +381,32 @@ Task<Result<IReadOnlyList<IApplicationRoleConnectionMetadata>>> UpdateApplicatio
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>The application object.</returns>
Task<Result<IApplication>> GetCurrentApplicationAsync(CancellationToken ct = default);

/// <summary>
/// Edit properties of the application associated with the requesting bot user.
/// </summary>
/// <param name="customInstallUrl">The default custom authorization URL of the app.</param>
/// <param name="description">The description of the app.</param>
/// <param name="roleConnectionsVerificationUrl">The role connections verification URL of the app.</param>
/// <param name="installParams">The settings for the app's in-app authorization.</param>
/// <param name="flags">The new flags.</param>
/// <param name="icon">The new icon.</param>
/// <param name="coverImage">The new cover image.</param>
/// <param name="interactionsEndpointUrl">The new interactions endpoint URL.</param>
/// <param name="tags">The new tags.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>The updated application.</returns>
Task<Result<IApplication>> EditCurrentApplicationAsync
(
Optional<Uri> customInstallUrl = default,
Optional<string> description = default,
Optional<Uri> roleConnectionsVerificationUrl = default,
Optional<IApplicationInstallParameters> installParams = default,
Optional<ApplicationFlags> flags = default,
Optional<Stream> icon = default,
Optional<Stream> coverImage = default,
Optional<Uri> interactionsEndpointUrl = default,
Optional<IReadOnlyList<string>> tags = default,
CancellationToken ct = default
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface IDiscordRestMonetizationAPI
/// <param name="excludeEnded">Whether to exclude expired entitlements.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>The entitlements.</returns>
public Task<Result<IReadOnlyList<IEntitlement>>> ListEntitlementsAsync
Task<Result<IReadOnlyList<IEntitlement>>> ListEntitlementsAsync
(
Snowflake applicationID,
Optional<Snowflake> userID = default,
Expand All @@ -71,7 +71,7 @@ public Task<Result<IReadOnlyList<IEntitlement>>> ListEntitlementsAsync
/// <param name="ownerType">The type of the owner.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>The test entitlement.</returns>
public Task<Result<IPartialEntitlement>> CreateTestEntitlementAsync
Task<Result<IPartialEntitlement>> CreateTestEntitlementAsync
(
Snowflake applicationID,
Snowflake skuID,
Expand All @@ -87,7 +87,7 @@ public Task<Result<IPartialEntitlement>> CreateTestEntitlementAsync
/// <param name="entitlementID">The ID of the entitlement.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A value representing the result of the operation.</returns>
public Task<Result> DeleteTestEntitlementAsync
Task<Result> DeleteTestEntitlementAsync
(
Snowflake applicationID,
Snowflake entitlementID,
Expand All @@ -100,5 +100,5 @@ public Task<Result> DeleteTestEntitlementAsync
/// <param name="applicationID">The ID of the application.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>The SKUs.</returns>
public Task<Result<IReadOnlyList<ISKU>>> ListSKUsAsync(Snowflake applicationID, CancellationToken ct = default);
Task<Result<IReadOnlyList<ISKU>>> ListSKUsAsync(Snowflake applicationID, CancellationToken ct = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public record Application
Optional<IReadOnlyList<string>> RPCOrigins,
bool IsBotPublic,
bool DoesBotRequireCodeGrant,
Optional<IPartialUser> Bot,
Optional<string> TermsOfServiceURL,
Optional<string> PrivacyPolicyURL,
Optional<IPartialUser> Owner,
Expand All @@ -53,8 +54,10 @@ public record Application
Optional<IImageHash> CoverImage = default,
Optional<ApplicationFlags> Flags = default,
Optional<int> ApproximateGuildCount = default,
Optional<IReadOnlyList<Uri>> RedirectUris = default,
Optional<Uri> InteractionsEndpointUrl = default,
Optional<Uri> RoleConnectionsVerificationUrl = default,
Optional<IReadOnlyList<string>> Tags = default,
Optional<IApplicationInstallParameters> InstallParams = default,
Optional<Uri> CustomInstallUrl = default,
Optional<Uri> RoleConnectionsVerificationUrl = default
Optional<Uri> CustomInstallUrl = default
) : IApplication;
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public record PartialApplication
Optional<IReadOnlyList<string>> RPCOrigins = default,
Optional<bool> IsBotPublic = default,
Optional<bool> DoesBotRequireCodeGrant = default,
Optional<IPartialUser> Bot = default,
Optional<string> TermsOfServiceURL = default,
Optional<string> PrivacyPolicyURL = default,
Optional<IPartialUser> Owner = default,
Expand All @@ -53,8 +54,10 @@ public record PartialApplication
Optional<IImageHash> CoverImage = default,
Optional<ApplicationFlags> Flags = default,
Optional<int> ApproximateGuildCount = default,
Optional<IReadOnlyList<Uri>> RedirectUris = default,
Optional<Uri> InteractionsEndpointUrl = default,
Optional<Uri> RoleConnectionsVerificationUrl = default,
Optional<IReadOnlyList<string>> Tags = default,
Optional<IApplicationInstallParameters> InstallParams = default,
Optional<Uri> CustomInstallUrl = default,
Optional<Uri> RoleConnectionsVerificationUrl = default
Optional<Uri> CustomInstallUrl = default
) : IPartialApplication;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
Expand All @@ -30,6 +32,7 @@
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Caching.Abstractions.Services;
using Remora.Discord.Rest.Extensions;
using Remora.Discord.Rest.Utility;
using Remora.Rest;
using Remora.Rest.Core;
using Remora.Rest.Extensions;
Expand Down Expand Up @@ -651,4 +654,67 @@ public Task<Result<IApplication>> GetCurrentApplicationAsync(CancellationToken c
ct: ct
);
}

/// <inheritdoc />
public async Task<Result<IApplication>> EditCurrentApplicationAsync
(
Optional<Uri> customInstallUrl = default,
Optional<string> description = default,
Optional<Uri> roleConnectionsVerificationUrl = default,
Optional<IApplicationInstallParameters> installParams = default,
Optional<ApplicationFlags> flags = default,
Optional<Stream> icon = default,
Optional<Stream> coverImage = default,
Optional<Uri> interactionsEndpointUrl = default,
Optional<IReadOnlyList<string>> tags = default,
CancellationToken ct = default
)
{
var packIcon = await ImagePacker.PackImageAsync(icon!, ct);
if (!packIcon.IsSuccess)
{
return Result<IApplication>.FromError(packIcon);
}

Optional<string> base64EncodedIcon = packIcon.Entity!;

var packCover = await ImagePacker.PackImageAsync(coverImage!, ct);
if (!packCover.IsSuccess)
{
return Result<IApplication>.FromError(packCover);
}

Optional<string> base64EncodedCover = packCover.Entity!;

return await this.RestHttpClient.PatchAsync<IApplication>
(
"applications/@me",
b =>
{
b.WithJson
(
json =>
{
json.Write("custom_install_url", customInstallUrl, this.JsonOptions);
json.Write("description", description, this.JsonOptions);
json.Write
(
"role_connections_verification_url",
roleConnectionsVerificationUrl,
this.JsonOptions
);
json.Write("install_params", installParams, this.JsonOptions);
json.Write("flags", flags, this.JsonOptions);
json.Write("icon", base64EncodedIcon, this.JsonOptions);
json.Write("cover_image", base64EncodedCover, this.JsonOptions);
json.Write("interactions_endpoint_url", interactionsEndpointUrl, this.JsonOptions);
json.Write("tags", tags, this.JsonOptions);
}
);
b.WithRateLimitContext(this.RateLimitCache);
},
ct: ct
);
}
}
Loading

0 comments on commit f8c454d

Please sign in to comment.