Skip to content

Commit

Permalink
feat: add premium subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Sep 27, 2023
1 parent 5759ce5 commit e12fbf0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export enum InteractionResponseType {
/** Responds to an autocomplete interaction request. */
APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8,
/** Respond to an interaction with a popup modal. */
MODAL = 9
MODAL = 9,
/** Respond to an interaction with prompt for a premium subscription. */
PREMIUM_REQUIRED = 10
}

/** Message flags for interaction responses. */
Expand Down Expand Up @@ -355,6 +357,7 @@ export interface DMModalSubmitRequestData {
user: CommandUser;
message?: MessageData;
app_permissions?: string;
entitlements: AppEntitlement[];
data: {
custom_id: string;
components: ComponentActionRow[];
Expand All @@ -378,6 +381,7 @@ export interface GuildModalSubmitRequestData {
member: CommandMember;
message?: MessageData;
app_permissions?: string;
entitlements: AppEntitlement[];
data: {
custom_id: string;
components: ComponentActionRow[];
Expand Down Expand Up @@ -405,6 +409,7 @@ export interface DMInteractionRequestData {
user: CommandUser;
channel: CommandChannel;
app_permissions?: string;
entitlements: AppEntitlement[];
data: CommandData;
}

Expand All @@ -425,6 +430,7 @@ export interface GuildInteractionRequestData {
member: CommandMember;
channel: CommandChannel;
app_permissions?: string;
entitlements: AppEntitlement[];
data: CommandData;
}

Expand Down Expand Up @@ -469,6 +475,7 @@ export interface DMMessageComponentRequestData {
user: CommandUser;
channel: CommandChannel;
app_permissions?: string;
entitlements: AppEntitlement[];
data: {
custom_id: string;
component_type: ComponentType;
Expand All @@ -491,6 +498,7 @@ export interface GuildMessageComponentRequestData {
guild_id: string;
member: CommandMember;
channel: CommandChannel;
entitlements: AppEntitlement[];
app_permissions?: string;
data: {
custom_id: string;
Expand All @@ -499,6 +507,22 @@ export interface GuildMessageComponentRequestData {
};
}

export interface AppEntitlement {
id: string;
sku_id: string;
user_id?: string;
guild_id?: string;
application_id: string;
type: EntitlementType;
consumed: false;
starts_at?: string;
ends_at?: string;
}

export enum EntitlementType {
APPLICATION_SUBSCRIPTION = 8
}

/**
* Any message component interaction.
* @private
Expand Down
5 changes: 4 additions & 1 deletion src/structures/interfaces/baseInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlashCreator } from '../../creator';
import { Member } from '../member';
import { User } from '../user';
import { Permissions } from '../permissions';
import { AttachmentData } from '../../constants';
import { AppEntitlement, AttachmentData } from '../../constants';
import { Collection } from '../../util/collection';
import { Channel } from '../channel';
import { Message } from '../message';
Expand Down Expand Up @@ -35,6 +35,8 @@ export class BaseInteractionContext {
readonly invokedAt: number = Date.now();
/** The permissions the application has. */
readonly appPermissions?: Permissions;
/** The entitlements the invoking user has. */
readonly entitlements: AppEntitlement[];

/** The resolved users of the interaction. */
readonly users = new Collection<string, User>();
Expand Down Expand Up @@ -66,6 +68,7 @@ export class BaseInteractionContext {
this.user = new User('guild_id' in data ? data.member.user : data.user, this.creator);
this.channel = new Channel(data.channel);
this.appPermissions = data.app_permissions ? new Permissions(BigInt(data.app_permissions)) : undefined;
this.entitlements = data.entitlements;

if (data.data.resolved) {
if (data.data.resolved.users)
Expand Down
22 changes: 22 additions & 0 deletions src/structures/interfaces/messageInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ export class MessageInteractionContext extends BaseInteractionContext {
return false;
}

/**
* Creates a message that prompts the user for a premium subscription.
* @returns Whether the message passed
*/
async promptPremium(): Promise<boolean> {
if (!this.initiallyResponded && !this.deferred) {
this.initiallyResponded = true;
this.deferred = true;
clearTimeout(this._timeout);
await this._respond({
status: 200,
body: {
type: InteractionResponseType.PREMIUM_REQUIRED,
data: {}
}
});
return true;
}

return false;
}

/**
* Registers a component callback from the initial message.
* This unregisters automatically when the context expires.
Expand Down

0 comments on commit e12fbf0

Please sign in to comment.