-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: premium application subscriptions (#9907)
* feat: premium application subscriptions * types: readonly array Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * fix: requested changes Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * fix: core client types --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
520d6f6
commit c4fcee3
Showing
33 changed files
with
914 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* eslint-disable jsdoc/check-param-names */ | ||
|
||
import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest'; | ||
import { | ||
Routes, | ||
type RESTGetAPIEntitlementsQuery, | ||
type RESTGetAPIEntitlementsResult, | ||
type RESTGetAPISKUsResult, | ||
type RESTPostAPIEntitlementBody, | ||
type RESTPostAPIEntitlementResult, | ||
type Snowflake, | ||
} from 'discord-api-types/v10'; | ||
|
||
export class MonetizationAPI { | ||
public constructor(private readonly rest: REST) {} | ||
|
||
/** | ||
* Fetches the SKUs for an application. | ||
* | ||
* @see {@link https://discord.com/developers/docs/monetization/skus#list-skus} | ||
* @param options - The options for fetching the SKUs. | ||
*/ | ||
public async getSKUs(applicationId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) { | ||
return this.rest.get(Routes.skus(applicationId), { signal }) as Promise<RESTGetAPISKUsResult>; | ||
} | ||
|
||
/** | ||
* Fetches the entitlements for an application. | ||
* | ||
* @see {@link https://discord.com/developers/docs/monetization/entitlements#list-entitlements} | ||
* @param applicationId - The application id to fetch entitlements for | ||
* @param query - The query options for fetching entitlements | ||
* @param options - The options for fetching entitlements | ||
*/ | ||
public async getEntitlements( | ||
applicationId: Snowflake, | ||
query: RESTGetAPIEntitlementsQuery, | ||
{ signal }: Pick<RequestData, 'signal'> = {}, | ||
) { | ||
return this.rest.get(Routes.entitlements(applicationId), { | ||
signal, | ||
query: makeURLSearchParams(query), | ||
}) as Promise<RESTGetAPIEntitlementsResult>; | ||
} | ||
|
||
/** | ||
* Creates a test entitlement for an application's SKU. | ||
* | ||
* @see {@link https://discord.com/developers/docs/monetization/entitlements#create-test-entitlement} | ||
* @param applicationId - The application id to create the entitlement for | ||
* @param body - The data for creating the entitlement | ||
* @param options - The options for creating the entitlement | ||
*/ | ||
public async createTestEntitlement( | ||
applicationId: Snowflake, | ||
body: RESTPostAPIEntitlementBody, | ||
{ signal }: Pick<RequestData, 'signal'> = {}, | ||
) { | ||
return this.rest.post(Routes.entitlements(applicationId), { | ||
body, | ||
signal, | ||
}) as Promise<RESTPostAPIEntitlementResult>; | ||
} | ||
|
||
/** | ||
* Deletes a test entitlement for an application's SKU. | ||
* | ||
* @see {@link https://discord.com/developers/docs/monetization/entitlements#delete-test-entitlement} | ||
* @param applicationId - The application id to delete the entitlement for | ||
* @param entitlementId - The entitlement id to delete | ||
* @param options - The options for deleting the entitlement | ||
*/ | ||
public async deleteTestEntitlement( | ||
applicationId: Snowflake, | ||
entitlementId: Snowflake, | ||
{ signal }: Pick<RequestData, 'signal'> = {}, | ||
) { | ||
await this.rest.delete(Routes.entitlement(applicationId, entitlementId), { signal }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/discord.js/src/client/actions/EntitlementCreate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const Events = require('../../util/Events'); | ||
|
||
class EntitlementCreateAction extends Action { | ||
handle(data) { | ||
const client = this.client; | ||
|
||
const entitlement = client.application.entitlements._add(data); | ||
|
||
/** | ||
* Emitted whenever an entitlement is created. | ||
* @event Client#entitlementCreate | ||
* @param {Entitlement} entitlement The entitlement that was created | ||
*/ | ||
client.emit(Events.EntitlementCreate, entitlement); | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
module.exports = EntitlementCreateAction; |
27 changes: 27 additions & 0 deletions
27
packages/discord.js/src/client/actions/EntitlementDelete.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const Events = require('../../util/Events'); | ||
|
||
class EntitlementDeleteAction extends Action { | ||
handle(data) { | ||
const client = this.client; | ||
|
||
const entitlement = client.application.entitlements._add(data, false); | ||
|
||
client.application.entitlements.cache.delete(entitlement.id); | ||
|
||
/** | ||
* Emitted whenever an entitlement is deleted. | ||
* <warn>Entitlements are not deleted when they expire. | ||
* This is only triggered when Discord issues a refund or deletes the entitlement manually.</warn> | ||
* @event Client#entitlementDelete | ||
* @param {Entitlement} entitlement The entitlement that was deleted | ||
*/ | ||
client.emit(Events.EntitlementDelete, entitlement); | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
module.exports = EntitlementDeleteAction; |
25 changes: 25 additions & 0 deletions
25
packages/discord.js/src/client/actions/EntitlementUpdate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const Events = require('../../util/Events'); | ||
|
||
class EntitlementUpdateAction extends Action { | ||
handle(data) { | ||
const client = this.client; | ||
|
||
const oldEntitlement = client.application.entitlements.cache.get(data.id)?._clone() ?? null; | ||
const newEntitlement = client.application.entitlements._add(data); | ||
|
||
/** | ||
* Emitted whenever an entitlement is updated - i.e. when a user's subscription renews. | ||
* @event Client#entitlementUpdate | ||
* @param {?Entitlement} oldEntitlement The entitlement before the update | ||
* @param {Entitlement} newEntitlement The entitlement after the update | ||
*/ | ||
client.emit(Events.EntitlementUpdate, oldEntitlement, newEntitlement); | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
module.exports = EntitlementUpdateAction; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_CREATE.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.EntitlementCreate.handle(packet.d); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_DELETE.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.EntitlementDelete.handle(packet.d); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.EntitlementUpdate.handle(packet.d); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.