Skip to content

Commit

Permalink
feat: end
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Aug 27, 2024
1 parent 14c6999 commit 7d35560
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/api/Routes/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { RESTPostAPIInteractionCallbackJSONBody, RESTPostAPIInteractionCallbackResult } from '../../types';
import type {
RESTPostAPIInteractionCallbackJSONBody,
RESTPostAPIInteractionCallbackQuery,
RESTPostAPIInteractionCallbackResult,
} from '../../types';
import type { ProxyRequestMethod } from '../Router';
import type { RestArguments } from '../api';

export interface InteractionRoutes {
interactions: (id: string) => (token: string) => {
callback: {
post(
args: RestArguments<ProxyRequestMethod.Post, RESTPostAPIInteractionCallbackJSONBody>,
args: RestArguments<
ProxyRequestMethod.Post,
RESTPostAPIInteractionCallbackJSONBody,
RESTPostAPIInteractionCallbackQuery
>,
): Promise<RESTPostAPIInteractionCallbackResult | undefined>;
};
};
Expand Down
73 changes: 61 additions & 12 deletions src/structures/Interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ import {
type RESTPostAPIInteractionCallbackJSONBody,
type RESTAPIAttachment,
type APIEntryPointCommandInteraction,
type InteractionCallbackData,
type InteractionCallbackResourceActivity,
} from '../types';

import type { RawFile } from '../api';
import { ActionRow, Embed, Modal, PollBuilder, resolveAttachment, resolveFiles } from '../builders';
import type { ContextOptionsResolved, UsingClient } from '../commands';
import type {
ObjectToLower,
OmitInsert,
ToClass,
When,
ComponentInteractionMessageUpdate,
InteractionCreateBodyRequest,
InteractionMessageUpdateBodyRequest,
MessageCreateBodyRequest,
MessageUpdateBodyRequest,
MessageWebhookCreateBodyRequest,
ModalCreateBodyRequest,
import {
type ObjectToLower,
type OmitInsert,
type ToClass,
type When,
type ComponentInteractionMessageUpdate,
type InteractionCreateBodyRequest,
type InteractionMessageUpdateBodyRequest,
type MessageCreateBodyRequest,
type MessageUpdateBodyRequest,
type MessageWebhookCreateBodyRequest,
type ModalCreateBodyRequest,
toCamelCase,
} from '../common';
import { channelFrom, type AllChannels } from './';
import { DiscordBase } from './extra/DiscordBase';
Expand All @@ -76,6 +79,7 @@ export type ReplyInteractionBody =
type: InteractionResponseType.ChannelMessageWithSource | InteractionResponseType.UpdateMessage;
data: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest | ComponentInteractionMessageUpdate;
}
| { type: InteractionResponseType.LaunchActivity }
| Exclude<RESTPostAPIInteractionCallbackJSONBody, APIInteractionResponsePong>;

export type __InternalReplyFunction = (_: { body: APIInteractionResponse; files?: RawFile[] }) => Promise<any>;
Expand Down Expand Up @@ -162,6 +166,8 @@ export class BaseInteraction<
: [],
},
};
case InteractionResponseType.LaunchActivity:
return body;
default:
return body;
}
Expand Down Expand Up @@ -493,11 +499,54 @@ export class EntryPointInteraction<FromGuild extends boolean = boolean> extends
FromGuild,
APIEntryPointCommandInteraction
> {
async withReponse(body: InteractionCreateBodyRequest | { type: InteractionResponseType.LaunchActivity }) {
if ('type' in body) {
const response = await this.client.proxy
.interactions(this.id)(this.token)
.callback.post({
body,
query: { with_response: true },
});

const result: EntryPointWithResponseResult = {
//@ts-expect-error
resource: {
type: response!.resource?.type!,
},
interaction: toCamelCase(response!.interaction),
};
if (response?.resource?.message) {
// @ts-expect-error
result.resource!.message = Transformers.WebhookMessage(
this.client,
response.resource.message as any,
this.id,
this.token,
);
} else {
// @ts-expect-error
result.resource!.activityInstance = response!.resource?.activity_instance!;
}
return result;
}
return this.write(body);
}

isEntryPoint(): this is EntryPointInteraction {
return true;
}
}

export interface EntryPointWithResponseResult {
interaction: ObjectToLower<InteractionCallbackData>;
resource?:
| { type: InteractionResponseType.LaunchActivity; activityInstance: InteractionCallbackResourceActivity }
| {
type: Exclude<InteractionResponseType, InteractionResponseType.LaunchActivity>;
message: WebhookMessageStructure;
};
}

export interface ComponentInteraction
extends ObjectToLower<
Omit<
Expand Down
7 changes: 6 additions & 1 deletion src/types/payloads/_interactions/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type APIInteractionResponse =
| APIInteractionResponsePong
| APIInteractionResponseUpdateMessage
| APIModalInteractionResponse
| APIInteractionResponseLaunchActivity
| APIPremiumRequiredInteractionResponse;

export interface APIInteractionResponsePong {
Expand Down Expand Up @@ -65,6 +66,10 @@ export interface APIInteractionResponseUpdateMessage {
data?: APIInteractionResponseCallbackData;
}

export interface APIInteractionResponseLaunchActivity {
type: InteractionResponseType.LaunchActivity;
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type
*/
Expand Down Expand Up @@ -106,7 +111,7 @@ export enum InteractionResponseType {
/**
* Launch the Activity associated with the app. Only available for apps with Activities enabled
*/
LaunchActivity = 14,
LaunchActivity = 12,
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/types/rest/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ export type RESTPutAPIApplicationGuildCommandsResult = RESTAPIApplicationGuildCo
*/
export type RESTPostAPIInteractionCallbackJSONBody = APIInteractionResponse;

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#create-interaction-response-query-string-params
*/
export type RESTPostAPIInteractionCallbackQuery = {
/**
* Whether to include a RESTPostAPIInteractionCallbackResult as the response instead of a 204.
*/
with_response?: boolean;
};

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
*/
Expand Down

0 comments on commit 7d35560

Please sign in to comment.