From 332cc8549cd02c7e456a7bff6393b8bded1607c2 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 27 Apr 2024 19:09:54 +0100 Subject: [PATCH] feat: consumable entitlements --- packages/core/src/api/monetization.ts | 17 ++++++++++++++++ .../discord.js/src/structures/Entitlement.js | 20 +++++++++++++++++++ packages/discord.js/typings/index.d.ts | 2 ++ 3 files changed, 39 insertions(+) diff --git a/packages/core/src/api/monetization.ts b/packages/core/src/api/monetization.ts index 12e7f5c66b83..e64f20f9aefe 100644 --- a/packages/core/src/api/monetization.ts +++ b/packages/core/src/api/monetization.ts @@ -77,4 +77,21 @@ export class MonetizationAPI { ) { await this.rest.delete(Routes.entitlement(applicationId, entitlementId), { signal }); } + + /** + * Marks a given entitlement for the user as consumed. Only available for One-Time Purchase consumable SKUs. + * + * @see {@link https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement} + * @param applicationId - The application id to consume the entitlement for + * @param entitlementId - The entitlement id to consume + * @param options - The options for consuming the entitlement + */ + public async consumeEntitlement( + applicationId: Snowflake, + entitlementId: Snowflake, + { signal }: Pick = {}, + ) { + // await this.rest.post(Routes.consumeEntitlement(applicationId, entitlementId), { signal }); + await this.rest.post(`/applications/${applicationId}/entitlements/${entitlementId}/consume`, { signal }); + } } diff --git a/packages/discord.js/src/structures/Entitlement.js b/packages/discord.js/src/structures/Entitlement.js index 9560a97709d9..58e73b755967 100644 --- a/packages/discord.js/src/structures/Entitlement.js +++ b/packages/discord.js/src/structures/Entitlement.js @@ -91,6 +91,16 @@ class Entitlement extends Base { } else { this.endsTimestamp ??= null; } + + if ('consumed' in data) { + /** + * Whether this entitlement has been consumed + * @type {boolean} + */ + this.consumed = data.consumed; + } else { + this.consumed ??= false; + } } /** @@ -159,6 +169,16 @@ class Entitlement extends Base { fetchUser() { return this.client.users.fetch(this.userId); } + + /** + * Marks this entitlement as consumed + * Only available for One-Time Purchase consumable SKUs. + * @returns {Promise} + */ + async consume() { + // Return this.client.rest.post(Routes.consumeEntitlement(this.applicationId, this.id)); + await this.client.rest.post(`/applications/${this.applicationId}/entitlements/${this.id}/consume`); + } } exports.Entitlement = Entitlement; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index c247d4f61883..5168c674de09 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1343,12 +1343,14 @@ export class Entitlement extends Base { public guildId: Snowflake | null; public applicationId: Snowflake; public type: EntitlementType; + public consumed: boolean; public deleted: boolean; public startsTimestamp: number | null; public endsTimestamp: number | null; public get guild(): Guild | null; public get startsAt(): Date | null; public get endsAt(): Date | null; + public consume(): Promise; public fetchUser(): Promise; public isActive(): boolean; public isTest(): this is this & {