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] More store stuff #2919

Merged
merged 1 commit into from
Apr 28, 2024
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
38 changes: 38 additions & 0 deletions src/Discord.Net.Core/Entities/AppSubscriptions/EntitlementType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
namespace Discord;

/// <summary>
/// Represents the type of entitlement.
/// </summary>
public enum EntitlementType
{
/// <summary>
/// The entitlement was purchased by user.
/// </summary>
Purchase = 1,

/// <summary>
/// Entitlement for Discord Nitro subscription.
/// </summary>
PremiumSubscription = 2,

/// <summary>
/// Entitlement was gifted by developer.
/// </summary>
DeveloperGift = 3,

/// <summary>
/// Entitlement was purchased by a dev in application test mode.
/// </summary>
TestModePurchase = 4,

/// <summary>
/// Entitlement was granted when the SKU was free.
/// </summary>
FreePurchase = 5,

/// <summary>
/// Entitlement was gifted by another user.
/// </summary>
UserGift = 6,

/// <summary>
/// Entitlement was claimed by user for free as a Nitro Subscriber.
/// </summary>
PremiumPurchase = 7,

/// <summary>
/// The entitlement was purchased as an app subscription.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Discord.Net.Core/Entities/AppSubscriptions/SKUFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace Discord;

/// <summary>
/// SKU flags for subscriptions.
/// </summary>
[Flags]
public enum SKUFlags
{
/// <summary>
/// The SKU is a guild subscription.
/// </summary>
GuildSubscription = 1 << 7,

/// <summary>
/// The SKU is a user subscription.
/// </summary>
UserSubscription = 1 << 8
}
12 changes: 11 additions & 1 deletion src/Discord.Net.Core/Entities/AppSubscriptions/SKUType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ namespace Discord;

public enum SKUType
{
/// <summary>
/// Durable one-time purchase.
/// </summary>
Durable = 2,

/// <summary>
/// Consumable one-time purchase.
/// </summary>
Consumable = 3,

/// <summary>
/// Represents a recurring subscription.
/// </summary>
Subscription = 5,

/// <summary>
/// System-generated group for each <see cref="SKUType.Subscription"/> SKU created.
/// System-generated group for each <see cref="Subscription"/> SKU created.
/// </summary>
SubscriptionGroup = 6,
}
7 changes: 7 additions & 0 deletions src/Discord.Net.Core/IDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,12 @@ IAsyncEnumerable<IReadOnlyCollection<IEntitlement>> GetEntitlementsAsync(int? li
/// Returns all SKUs for a given application.
/// </summary>
Task<IReadOnlyCollection<SKU>> GetSKUsAsync(RequestOptions options = null);

/// <summary>
/// Marks a given one-time purchase entitlement for the user as consumed.
/// </summary>
/// <param name="entitlementId">The id of the entitlement.</param>
/// <param name="options">The options to be used when sending the request.</param>
Task ConsumeEntitlementAsync(ulong entitlementId, RequestOptions options = null);
}
}
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/API/Common/Entitlement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class Entitlement
public EntitlementType Type { get; set; }

[JsonProperty("consumed")]
public bool IsConsumed { get; set; }
public Optional<bool> IsConsumed { get; set; }

[JsonProperty("starts_at")]
public Optional<DateTimeOffset> StartsAt { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Discord.Net.Rest/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ IAsyncEnumerable<IReadOnlyCollection<IEntitlement>> IDiscordClient.GetEntitlemen
/// </summary>
Task<IReadOnlyCollection<SKU>> IDiscordClient.GetSKUsAsync(RequestOptions options) => Task.FromResult<IReadOnlyCollection<SKU>>(Array.Empty<SKU>());

/// <summary>
/// Marks a given one-time purchase entitlement for the user as consumed.
/// </summary>
Task IDiscordClient.ConsumeEntitlementAsync(ulong entitlementId, RequestOptions options) => Task.CompletedTask;

#endregion
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ public static async Task<IReadOnlyCollection<SKU>> ListSKUsAsync(BaseDiscordClie
return models.Select(x => new SKU(x.Id, x.Type, x.ApplicationId, x.Name, x.Slug)).ToImmutableArray();
}

public static Task ConsumeEntitlementAsync(BaseDiscordClient client, ulong entitlementId, RequestOptions options = null)
=> client.ApiClient.ConsumeEntitlementAsync(entitlementId, options);

#endregion
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,9 @@ public Task<Entitlement[]> ListEntitlementAsync(ListEntitlementsParams args, Req
public Task<SKU[]> ListSKUsAsync(RequestOptions options = null)
=> SendAsync<SKU[]>("GET", () => $"applications/{CurrentApplicationId}/skus", new BucketIds(), options: options);

public Task ConsumeEntitlementAsync(ulong entitlementId, RequestOptions options = null)
=> SendAsync("POST", () => $"applications/{CurrentApplicationId}/entitlements/{entitlementId}/consume", new BucketIds(), options: options);

#endregion
}
}
4 changes: 4 additions & 0 deletions src/Discord.Net.Rest/DiscordRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ public IAsyncEnumerable<IReadOnlyCollection<IEntitlement>> GetEntitlementsAsync(
public Task<IReadOnlyCollection<SKU>> GetSKUsAsync(RequestOptions options = null)
=> ClientHelper.ListSKUsAsync(this, options);

/// <inheritdoc />
public Task ConsumeEntitlementAsync(ulong entitlementId, RequestOptions options = null)
=> ClientHelper.ConsumeEntitlementAsync(this, entitlementId, options);

#endregion

#region IDiscordClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal void Update(Model model)
: null;
ApplicationId = model.ApplicationId;
Type = model.Type;
IsConsumed = model.IsConsumed;
IsConsumed = model.IsConsumed.GetValueOrDefault(false);
StartsAt = model.StartsAt.IsSpecified
? model.StartsAt.Value
: null;
Expand Down
4 changes: 4 additions & 0 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ public IAsyncEnumerable<IReadOnlyCollection<IEntitlement>> GetEntitlementsAsync(
public Task<IReadOnlyCollection<SKU>> GetSKUsAsync(RequestOptions options = null)
=> ClientHelper.ListSKUsAsync(this, options);

/// <inheritdoc />
public Task ConsumeEntitlementAsync(ulong entitlementId, RequestOptions options = null)
=> ClientHelper.ConsumeEntitlementAsync(this, entitlementId, options);

/// <summary>
/// Gets entitlements from cache.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal void Update(Model model)

ApplicationId = model.ApplicationId;
Type = model.Type;
IsConsumed = model.IsConsumed;
IsConsumed = model.IsConsumed.GetValueOrDefault(false);
StartsAt = model.StartsAt.IsSpecified
? model.StartsAt.Value
: null;
Expand Down