feat: monetization (#227)

* feat: monetization

* chore: apply formatting

* fix: query list entitlements
This commit is contained in:
Marcos Susaña 2024-07-27 22:05:18 -04:00 committed by GitHub
parent 026530fd39
commit 11c72e66f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 88 additions and 7 deletions

View File

@ -30,6 +30,11 @@ import type {
RESTPostAPIApplicationEmojiResult,
RESTPatchAPIApplicationEmojiResult,
RESTDeleteAPIApplicationEmojiResult,
RESTGetAPIEntitlementsResult,
RESTGetAPIEntitlementsQuery,
RESTPostAPIEntitlementBody,
RESTPostAPIEntitlementResult,
RESTGetAPISKUsResult,
} from '../../types';
import type { ProxyRequestMethod } from '../Router';
@ -118,5 +123,25 @@ export interface ApplicationRoutes {
args?: RestArguments<ProxyRequestMethod.Post, RESTPostAPIApplicationEmojiJSONBody>,
): Promise<RESTPostAPIApplicationEmojiResult>;
};
entitlements: {
get(
args?: RestArguments<ProxyRequestMethod.Get, RESTGetAPIEntitlementsQuery>,
): Promise<RESTGetAPIEntitlementsResult>;
post(
args: RestArguments<ProxyRequestMethod.Post, RESTPostAPIEntitlementBody>,
): Promise<RESTPostAPIEntitlementResult>;
(
id: string,
): {
delete(args?: RestArguments<ProxyRequestMethod.Delete>): Promise<never>;
consume: {
post(args?: RestArguments<ProxyRequestMethod.Post>): Promise<never>;
};
};
};
skus: {
get(args?: RestArguments<ProxyRequestMethod.Get>): Promise<RESTGetAPISKUsResult>;
};
};
}

View File

@ -15,6 +15,7 @@ import type {
import { IgnoreCommand, type InferWithPrefix, type MiddlewareContext } from '../commands/applications/shared';
import { CommandHandler } from '../commands/handler';
import {
ApplicationShorter,
ChannelShorter,
EmojiShorter,
GuildShorter,
@ -56,6 +57,7 @@ export class BaseClient {
rest!: ApiHandler;
cache!: Cache;
applications = new ApplicationShorter(this);
users = new UsersShorter(this);
channels = new ChannelShorter(this);
guilds = new GuildShorter(this);

View File

@ -16,6 +16,7 @@ export * from './shorters/users';
export * from './shorters/threads';
export * from './shorters/webhook';
export * from './shorters/interaction';
export * from './shorters/application';
//
export * from './types/options';
export * from './types/resolvables';

View File

@ -0,0 +1,31 @@
import { Entitlement } from '../../structures/Entitlement';
import type { APIEntitlement, RESTGetAPIEntitlementsQuery, RESTPostAPIEntitlementBody } from '../../types';
import { BaseShorter } from './base';
export class ApplicationShorter extends BaseShorter {
async listEntitlements(applicationId: string, query?: RESTGetAPIEntitlementsQuery) {
return this.client.proxy
.applications(applicationId)
.entitlements.get({ query })
.then(et => et.map(e => new Entitlement(this.client, e)));
}
async consumeEntitlement(applicationId: string, entitlementId: string) {
return this.client.proxy.applications(applicationId).entitlements(entitlementId).consume.post();
}
async createTestEntitlement(applicationId: string, body: RESTPostAPIEntitlementBody) {
return this.client.proxy
.applications(applicationId)
.entitlements.post({ body })
.then(et => new Entitlement(this.client, et as APIEntitlement));
}
async deleteTestEntitlement(applicationId: string, entitlementId: string) {
return this.client.proxy.applications(applicationId).entitlements(entitlementId).delete();
}
async listSKUs(applicationId: string) {
return this.client.proxy.applications(applicationId).skus.get();
}
}

View File

@ -1,15 +1,15 @@
import type { APIEntitlement } from '../../types';
import { toCamelCase } from '../../common';
import type { UsingClient } from '../../commands';
import { Entitlement } from '../../structures/Entitlement';
export const ENTITLEMENT_CREATE = (_: UsingClient, data: APIEntitlement) => {
return toCamelCase(data);
export const ENTITLEMENT_CREATE = (client: UsingClient, data: APIEntitlement) => {
return new Entitlement(client, data);
};
export const ENTITLEMENT_UPDATE = (_: UsingClient, data: APIEntitlement) => {
return toCamelCase(data);
export const ENTITLEMENT_UPDATE = (client: UsingClient, data: APIEntitlement) => {
return new Entitlement(client, data);
};
export const ENTITLEMENT_DELETE = (_: UsingClient, data: APIEntitlement) => {
return toCamelCase(data);
export const ENTITLEMENT_DELETE = (client: UsingClient, data: APIEntitlement) => {
return new Entitlement(client, data);
};

View File

@ -0,0 +1,19 @@
import type { ObjectToLower } from '../common';
import type { APIEntitlement } from '../types';
import { DiscordBase } from './extra/DiscordBase';
export interface Entitlement extends ObjectToLower<APIEntitlement> {}
export class Entitlement extends DiscordBase<APIEntitlement> {
get startsAtTimestamp() {
return this.startsAt ? Date.parse(this.startsAt) : null;
}
get endsAtTimestamp() {
return this.endsAt ? Date.parse(this.endsAt) : null;
}
consume() {
return this.client.applications.consumeEntitlement(this.applicationId, this.id);
}
}

View File

@ -68,6 +68,7 @@ import {
type OptionResolverStructure,
} from '../client/transformers';
import { mix } from '../deps/mixer';
import { Entitlement } from './Entitlement';
export type ReplyInteractionBody =
| { type: InteractionResponseType.Modal; data: ModalCreateBodyRequest }
@ -123,6 +124,8 @@ export class BaseInteraction<
this.channel = channelFrom(interaction.channel, client);
}
this.user = this.member?.user ?? Transformers.User(client, interaction.user!);
this.entitlements = interaction.entitlements.map(e => new Entitlement(this.client, e));
}
static transformBodyRequest(