Skip to content

Commit

Permalink
feat: consumable entitlements
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Apr 27, 2024
1 parent 14f9ff7 commit 332cc85
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/api/monetization.ts
Expand Up @@ -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<RequestData, 'signal'> = {},
) {
// await this.rest.post(Routes.consumeEntitlement(applicationId, entitlementId), { signal });
await this.rest.post(`/applications/${applicationId}/entitlements/${entitlementId}/consume`, { signal });
}
}
20 changes: 20 additions & 0 deletions packages/discord.js/src/structures/Entitlement.js
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -159,6 +169,16 @@ class Entitlement extends Base {
fetchUser() {
return this.client.users.fetch(this.userId);
}

/**
* Marks this entitlement as consumed
* <info>Only available for One-Time Purchase consumable SKUs.</info>
* @returns {Promise<void>}
*/
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;
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -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<void>;
public fetchUser(): Promise<User>;
public isActive(): boolean;
public isTest(): this is this & {
Expand Down

0 comments on commit 332cc85

Please sign in to comment.