Skip to content

Commit

Permalink
feat(EntitlementManager): Support get entitlement (#10606)
Browse files Browse the repository at this point in the history
* feat: support get entitlement

* docs: add return type

Co-authored-by: Danial Raza <[email protected]>

* fix: property typo

Co-authored-by: Almeida <[email protected]>

* fix: property typo

Co-authored-by: Almeida <[email protected]>

---------

Co-authored-by: Danial Raza <[email protected]>
Co-authored-by: Almeida <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Dec 5, 2024
1 parent 7678f11 commit a367e2c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
41 changes: 38 additions & 3 deletions packages/discord.js/src/managers/EntitlementManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class EntitlementManager extends CachedManager {
* @typedef {SKU|Snowflake} SKUResolvable
*/

/**
* Options used to fetch an entitlement
* @typedef {BaseFetchOptions} FetchEntitlementOptions
* @property {EntitlementResolvable} entitlement The entitlement to fetch
*/

/**
* Options used to fetch entitlements
* @typedef {Object} FetchEntitlementsOptions
Expand All @@ -45,6 +51,7 @@ class EntitlementManager extends CachedManager {
* @property {UserResolvable} [user] The user to fetch entitlements for
* @property {SKUResolvable[]} [skus] The SKUs to fetch entitlements for
* @property {boolean} [excludeEnded] Whether to exclude ended entitlements
* @property {boolean} [excludeDeleted] Whether to exclude deleted entitlements
* @property {boolean} [cache=true] Whether to cache the fetched entitlements
* @property {Snowflake} [before] Consider only entitlements before this entitlement id
* @property {Snowflake} [after] Consider only entitlements after this entitlement id
Expand All @@ -53,21 +60,49 @@ class EntitlementManager extends CachedManager {

/**
* Fetches entitlements for this application
* @param {FetchEntitlementsOptions} [options={}] Options for fetching the entitlements
* @returns {Promise<Collection<Snowflake, Entitlement>>}
* @param {EntitlementResolvable|FetchEntitlementOptions|FetchEntitlementsOptions} [options]
* Options for fetching the entitlements
* @returns {Promise<Entitlement|Collection<Snowflake, Entitlement>>}
*/
async fetch({ limit, guild, user, skus, excludeEnded, cache = true, before, after } = {}) {
async fetch(options) {
if (!options) return this._fetchMany(options);
const { entitlement, cache, force } = options;
const resolvedEntitlement = this.resolveId(entitlement ?? options);

if (resolvedEntitlement) {
return this._fetchSingle({ entitlement: resolvedEntitlement, cache, force });
}

return this._fetchMany(options);
}

async _fetchSingle({ entitlement, cache, force = false }) {
if (!force) {
const existing = this.cache.get(entitlement);

if (existing) {
return existing;
}
}

const data = await this.client.rest.get(Routes.entitlement(this.client.application.id, entitlement));
return this._add(data, cache);
}

async _fetchMany({ limit, guild, user, skus, excludeEnded, excludeDeleted, cache, before, after } = {}) {
const query = makeURLSearchParams({
limit,
guild_id: guild && this.client.guilds.resolveId(guild),
user_id: user && this.client.users.resolveId(user),
sku_ids: skus?.map(sku => resolveSKUId(sku)).join(','),
exclude_ended: excludeEnded,
exclude_deleted: excludeDeleted,
before,
after,
});

const entitlements = await this.client.rest.get(Routes.entitlements(this.client.application.id), { query });

return entitlements.reduce(
(coll, entitlement) => coll.set(entitlement.id, this._add(entitlement, cache)),
new Collection(),
Expand Down
6 changes: 6 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4336,19 +4336,25 @@ export interface UserEntitlementCreateOptions {
user: UserResolvable;
}

export interface FetchEntitlementOptions extends BaseFetchOptions {
entitlement: EntitlementResolvable;
}

export interface FetchEntitlementsOptions {
limit?: number;
guild?: GuildResolvable;
user?: UserResolvable;
skus?: readonly SKUResolvable[];
excludeEnded?: boolean;
excludeDeleted?: boolean;
cache?: boolean;
before?: Snowflake;
after?: Snowflake;
}

export class EntitlementManager extends CachedManager<Snowflake, Entitlement, EntitlementResolvable> {
private constructor(client: Client<true>, iterable: Iterable<APIEntitlement>);
public fetch(options: EntitlementResolvable | FetchEntitlementOptions): Promise<Entitlement>;
public fetch(options?: FetchEntitlementsOptions): Promise<Collection<Snowflake, Entitlement>>;
public createTest(options: GuildEntitlementCreateOptions | UserEntitlementCreateOptions): Promise<Entitlement>;
public deleteTest(entitlement: EntitlementResolvable): Promise<void>;
Expand Down

0 comments on commit a367e2c

Please sign in to comment.