feat: interactions.reply method

This commit is contained in:
MARCROCK22 2024-04-21 12:21:27 -04:00
parent cf6d1ac819
commit dff87e4b5e
2 changed files with 22 additions and 25 deletions

View File

@ -1,14 +1,24 @@
import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles } from '../..'; import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles, type ReplyInteractionBody } from '../..';
import type { InteractionMessageUpdateBodyRequest, MessageWebhookCreateBodyRequest } from '../types/write'; import type { InteractionMessageUpdateBodyRequest, MessageWebhookCreateBodyRequest } from '../types/write';
import { BaseShorter } from './base'; import { BaseShorter } from './base';
export class InteractionShorter extends BaseShorter { export class InteractionShorter extends BaseShorter {
protected get appId() { async reply(id: string, token: string, body: ReplyInteractionBody) {
return this.client.applicationId; //@ts-expect-error
const { files, ...data } = body.data ?? {};
return this.client.proxy
.interactions(id)(token)
.callback.post({
body: BaseInteraction.transformBodyRequest({
type: body.type,
data,
}),
files: files ? await resolveFiles(files) : undefined,
});
} }
fetchResponse(token: string, messageId: string) { fetchResponse(token: string, messageId: string) {
return this.client.webhooks.fetchMessage(this.appId, token, messageId); return this.client.webhooks.fetchMessage(this.client.applicationId, token, messageId);
} }
fetchOriginal(token: string) { fetchOriginal(token: string) {
@ -18,7 +28,7 @@ export class InteractionShorter extends BaseShorter {
async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) { async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) {
const { files, ...data } = body; const { files, ...data } = body;
const apiMessage = await this.client.proxy const apiMessage = await this.client.proxy
.webhooks(this.appId)(token) .webhooks(this.client.applicationId)(token)
.messages(messageId) .messages(messageId)
.patch({ .patch({
body: BaseInteraction.transformBody(data), body: BaseInteraction.transformBody(data),
@ -33,7 +43,7 @@ export class InteractionShorter extends BaseShorter {
deleteResponse(interactionId: string, token: string, messageId: string) { deleteResponse(interactionId: string, token: string, messageId: string) {
return this.client.proxy return this.client.proxy
.webhooks(this.appId)(token) .webhooks(this.client.applicationId)(token)
.messages(messageId) .messages(messageId)
.delete() .delete()
.then(() => this.client.components?.onMessageDelete(messageId === '@original' ? interactionId : messageId)); .then(() => this.client.components?.onMessageDelete(messageId === '@original' ? interactionId : messageId));
@ -46,11 +56,11 @@ export class InteractionShorter extends BaseShorter {
async followup(token: string, { files, ...body }: MessageWebhookCreateBodyRequest) { async followup(token: string, { files, ...body }: MessageWebhookCreateBodyRequest) {
files = files ? await resolveFiles(files) : undefined; files = files ? await resolveFiles(files) : undefined;
const apiMessage = await this.client.proxy const apiMessage = await this.client.proxy
.webhooks(this.appId)(token) .webhooks(this.client.applicationId)(token)
.post({ .post({
body: BaseInteraction.transformBody(body), body: BaseInteraction.transformBody(body),
files: files as RawFile[] | undefined, files: files as RawFile[] | undefined,
}); });
return new WebhookMessage(this.client, apiMessage, this.appId, token); return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token);
} }
} }

View File

@ -38,7 +38,7 @@ import {
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import { mix } from 'ts-mixer'; import { mix } from 'ts-mixer';
import type { RawFile } from '../api'; import type { RawFile } from '../api';
import { ActionRow, Embed, Modal, resolveAttachment, resolveFiles } from '../builders'; import { ActionRow, Embed, Modal, resolveAttachment } from '../builders';
import { OptionResolver, type ContextOptionsResolved, type UsingClient } from '../commands'; import { OptionResolver, type ContextOptionsResolved, type UsingClient } from '../commands';
import type { ObjectToLower, OmitInsert, ToClass, When } from '../common'; import type { ObjectToLower, OmitInsert, ToClass, When } from '../common';
import type { import type {
@ -170,32 +170,19 @@ export class BaseInteraction<
} as T; } as T;
} }
private matchReplied(data: ReplyInteractionBody, type: InteractionResponseType, filesParsed: RawFile[] | undefined) { private matchReplied(data: ReplyInteractionBody) {
this.replied = (this.__reply ?? this.api.interactions(this.id)(this.token).callback.post)({ return (this.replied = this.client.interactions.reply(this.id, this.token, data).then(() => (this.replied = true)));
// @ts-expect-error
body: BaseInteraction.transformBodyRequest({ data, type }),
files: filesParsed,
}).then(() => (this.replied = true));
} }
async reply(body: ReplyInteractionBody) { async reply(body: ReplyInteractionBody) {
if (this.replied) { if (this.replied) {
throw new Error('Interaction already replied'); throw new Error('Interaction already replied');
} }
await this.matchReplied(body);
// @ts-expect-error
if (body.data?.files) {
// @ts-expect-error
const { files, ...rest } = body.data;
this.matchReplied(rest, body.type, await resolveFiles(files));
// @ts-expect-error
} else this.matchReplied(body.data, body.type);
// @ts-expect-error // @ts-expect-error
if (body.data instanceof Modal && body.data.__exec) if (body.data instanceof Modal && body.data.__exec)
// @ts-expect-error // @ts-expect-error
this.client.components.modals.set(this.user.id, (body.data as Modal).__exec); this.client.components.modals.set(this.user.id, (body.data as Modal).__exec);
await this.replied;
} }
deferReply(flags?: MessageFlags) { deferReply(flags?: MessageFlags) {