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

feat: consumable entitlements #10235

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions packages/core/src/api/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ 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 });
}
}
20 changes: 20 additions & 0 deletions packages/discord.js/src/structures/Entitlement.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

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

/**
Expand Down Expand Up @@ -91,6 +92,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 +170,15 @@ 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() {
await this.client.rest.post(Routes.consumeEntitlement(this.applicationId, this.id));
}
}

exports.Entitlement = Entitlement;
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
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
Loading