Skip to content

Commit 0de148d

Browse files
committed
feat: consumable entitlements
1 parent c7adce3 commit 0de148d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

packages/core/src/api/monetization.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ export class MonetizationAPI {
7777
) {
7878
await this.rest.delete(Routes.entitlement(applicationId, entitlementId), { signal });
7979
}
80+
81+
/**
82+
* Marks a given entitlement for the user as consumed. Only available for One-Time Purchase consumable SKUs.
83+
*
84+
* @see {@link https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement}
85+
* @param applicationId - The application id to consume the entitlement for
86+
* @param entitlementId - The entitlement id to consume
87+
* @param options - The options for consuming the entitlement
88+
*/
89+
public async consumeEntitlement(
90+
applicationId: Snowflake,
91+
entitlementId: Snowflake,
92+
{ signal }: Pick<RequestData, 'signal'> = {},
93+
) {
94+
await this.rest.post(Routes.consumeEntitlement(applicationId, entitlementId), { signal });
95+
}
8096
}

packages/discord.js/src/structures/Entitlement.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { Routes } = require('discord-api-types/v10');
34
const Base = require('./Base');
45

56
/**
@@ -91,6 +92,16 @@ class Entitlement extends Base {
9192
} else {
9293
this.endsTimestamp ??= null;
9394
}
95+
96+
if ('consumed' in data) {
97+
/**
98+
* Whether this entitlement has been consumed
99+
* @type {boolean}
100+
*/
101+
this.consumed = data.consumed;
102+
} else {
103+
this.consumed ??= false;
104+
}
94105
}
95106

96107
/**
@@ -159,6 +170,15 @@ class Entitlement extends Base {
159170
fetchUser() {
160171
return this.client.users.fetch(this.userId);
161172
}
173+
174+
/**
175+
* Marks this entitlement as consumed
176+
* <info>Only available for One-Time Purchase consumable SKUs.</info>
177+
* @returns {Promise<void>}
178+
*/
179+
async consume() {
180+
await this.client.rest.post(Routes.consumeEntitlement(this.applicationId, this.id));
181+
}
162182
}
163183

164184
exports.Entitlement = Entitlement;

packages/discord.js/typings/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,12 +1343,14 @@ export class Entitlement extends Base {
13431343
public guildId: Snowflake | null;
13441344
public applicationId: Snowflake;
13451345
public type: EntitlementType;
1346+
public consumed: boolean;
13461347
public deleted: boolean;
13471348
public startsTimestamp: number | null;
13481349
public endsTimestamp: number | null;
13491350
public get guild(): Guild | null;
13501351
public get startsAt(): Date | null;
13511352
public get endsAt(): Date | null;
1353+
public consume(): Promise<void>;
13521354
public fetchUser(): Promise<User>;
13531355
public isActive(): boolean;
13541356
public isTest(): this is this & {

0 commit comments

Comments
 (0)