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 { BaseShorter } from './base';
export class InteractionShorter extends BaseShorter {
protected get appId() {
return this.client.applicationId;
async reply(id: string, token: string, body: ReplyInteractionBody) {
//@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) {
return this.client.webhooks.fetchMessage(this.appId, token, messageId);
return this.client.webhooks.fetchMessage(this.client.applicationId, token, messageId);
}
fetchOriginal(token: string) {
@ -18,7 +28,7 @@ export class InteractionShorter extends BaseShorter {
async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) {
const { files, ...data } = body;
const apiMessage = await this.client.proxy
.webhooks(this.appId)(token)
.webhooks(this.client.applicationId)(token)
.messages(messageId)
.patch({
body: BaseInteraction.transformBody(data),
@ -33,7 +43,7 @@ export class InteractionShorter extends BaseShorter {
deleteResponse(interactionId: string, token: string, messageId: string) {
return this.client.proxy
.webhooks(this.appId)(token)
.webhooks(this.client.applicationId)(token)
.messages(messageId)
.delete()
.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) {
files = files ? await resolveFiles(files) : undefined;
const apiMessage = await this.client.proxy
.webhooks(this.appId)(token)
.webhooks(this.client.applicationId)(token)
.post({
body: BaseInteraction.transformBody(body),
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';
import { mix } from 'ts-mixer';
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 type { ObjectToLower, OmitInsert, ToClass, When } from '../common';
import type {
@ -170,32 +170,19 @@ export class BaseInteraction<
} as T;
}
private matchReplied(data: ReplyInteractionBody, type: InteractionResponseType, filesParsed: RawFile[] | undefined) {
this.replied = (this.__reply ?? this.api.interactions(this.id)(this.token).callback.post)({
// @ts-expect-error
body: BaseInteraction.transformBodyRequest({ data, type }),
files: filesParsed,
}).then(() => (this.replied = true));
private matchReplied(data: ReplyInteractionBody) {
return (this.replied = this.client.interactions.reply(this.id, this.token, data).then(() => (this.replied = true)));
}
async reply(body: ReplyInteractionBody) {
if (this.replied) {
throw new Error('Interaction already replied');
}
// @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);
await this.matchReplied(body);
// @ts-expect-error
if (body.data instanceof Modal && body.data.__exec)
// @ts-expect-error
this.client.components.modals.set(this.user.id, (body.data as Modal).__exec);
await this.replied;
}
deferReply(flags?: MessageFlags) {