Skip to content

Commit

Permalink
Add one time purchase SKUs support (#651)
Browse files Browse the repository at this point in the history
* add one time purchase skus support

* fix EntitlementType.premiumSubscription wording

* remove 'Entitlement' suffix from `EntitlementManager.consume`
  • Loading branch information
MCausc78 authored Apr 25, 2024
1 parent efdf25c commit b874066
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/src/http/managers/entitlement_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class EntitlementManager extends ReadOnlyManager<Entitlement> {
guildId: maybeParse(raw['guild_id'], Snowflake.parse),
applicationId: Snowflake.parse(raw['application_id']!),
type: EntitlementType.parse(raw['type'] as int),
isConsumed: raw['consumed'] as bool,
isConsumed: raw['consumed'] as bool? ?? false,
isDeleted: raw['deleted'] as bool? ?? false,
startsAt: maybeParse(raw['starts_at'], DateTime.parse),
endsAt: maybeParse(raw['ends_at'], DateTime.parse),
);
Expand Down Expand Up @@ -101,4 +102,15 @@ class EntitlementManager extends ReadOnlyManager<Entitlement> {
await client.httpHandler.executeSafe(request);
cache.remove(id);
}

/// Marks a entitlement for the user as consumed.
Future<void> consume(Snowflake id) async {
final route = HttpRoute()
..applications(id: applicationId.toString())
..entitlements(id: id.toString())
..consume();
final request = BasicRequest(route, method: 'POST');

await client.httpHandler.executeSafe(request);
}
}
3 changes: 3 additions & 0 deletions lib/src/http/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ extension RouteHelpers on HttpRoute {
/// Adds the [`skus`](https://discord.com/developers/docs/monetization/skus#list-skus) part to this [HttpRoute].
void skus() => add(HttpRoutePart('skus'));

/// Adds the [`consume`](https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement) part to this [HttpRoute].
void consume() => add(HttpRoutePart('consume'));

/// Adds the [`avatar-decorations`](https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints) part to this [HttpRoute].
void avatarDecorations({String? id}) => add(HttpRoutePart('avatar-decorations', [if (id != null) HttpRouteParam(id)]));

Expand Down
29 changes: 29 additions & 0 deletions lib/src/models/entitlement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PartialEntitlement extends ManagedSnowflakeEntity<Entitlement> {
/// Create a new [PartialEntitlement].
/// @nodoc
PartialEntitlement({required this.manager, required super.id});

/// Marks a entitlement for the user as consumed.
Future<void> consume() => manager.consume(id);
}

/// {@template entitlement}
Expand All @@ -34,6 +37,9 @@ class Entitlement extends PartialEntitlement {
/// The type of this entitlement.
final EntitlementType type;

/// Whether entitlement was deleted.
final bool isDeleted;

/// Whether this entitlement is consumed.
final bool isConsumed;

Expand All @@ -54,6 +60,7 @@ class Entitlement extends PartialEntitlement {
required this.applicationId,
required this.type,
required this.isConsumed,
required this.isDeleted,
required this.startsAt,
required this.endsAt,
});
Expand All @@ -70,6 +77,28 @@ class Entitlement extends PartialEntitlement {

/// The type of an [Entitlement].
enum EntitlementType {
/// Entitlement was purchased by user.
purchase._(1),

/// Entitlement was granted by Discord Nitro subscription.
premiumSubscription._(2),

/// Entitlement was gifted by developer.
developerGift._(3),

/// Entitlement was purchased by a dev in application test mode.
testModePurchase._(4),

/// Entitlement was granted when the SKU was free.
freePurchase._(5),

/// Entitlement was gifted by another user.
userGift._(6),

/// Entitlement was claimed by user for free as a Nitro Subscriber.
premiumPurchase._(7),

/// Entitlement was purchased as an app subscription.
applicationSubscription._(8);

final int value;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/models/sku.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Sku with ToStringHelper {

/// The type of an [Sku].
enum SkuType {
/// Durable one-time purchase.
durable._(2),

/// Consumable one-time purchase.
consumable._(3),
subscription._(5),
subscriptionGroup._(6);

Expand Down

0 comments on commit b874066

Please sign in to comment.