feat: Interactions shorters (#187)

* feat: threads methods

* fix: typing

* fix: fixes

* feat: interaction shorter

* fix: files in followup
This commit is contained in:
Marcos Susaña 2024-04-19 14:37:43 -04:00 committed by GitHub
parent f9cc5da7cc
commit 20088e9275
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 65 additions and 25 deletions

View File

@ -9,6 +9,7 @@ import {
ChannelShorter, ChannelShorter,
EmojiShorter, EmojiShorter,
GuildShorter, GuildShorter,
InteractionShorter,
LogLevels, LogLevels,
Logger, Logger,
MemberShorter, MemberShorter,
@ -52,6 +53,7 @@ export class BaseClient {
reactions = new ReactionShorter(this); reactions = new ReactionShorter(this);
emojis = new EmojiShorter(this); emojis = new EmojiShorter(this);
threads = new ThreadShorter(this); threads = new ThreadShorter(this);
interactions = new InteractionShorter(this);
debugger?: Logger; debugger?: Logger;

View File

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

View File

@ -0,0 +1,56 @@
import { BaseInteraction, type RawFile, WebhookMessage, resolveFiles } from '../..';
import type { InteractionMessageUpdateBodyRequest, MessageWebhookCreateBodyRequest } from '../types/write';
import { BaseShorter } from './base';
export class InteractionShorter extends BaseShorter {
protected get appId() {
return this.client.applicationId;
}
fetchResponse(token: string, messageId: string) {
return this.client.webhooks.fetchMessage(this.appId, token, messageId);
}
fetchOriginal(token: string) {
return this.fetchResponse(token, '@original');
}
async editMessage(token: string, messageId: string, body: InteractionMessageUpdateBodyRequest) {
const { files, ...data } = body;
const apiMessage = await this.client.proxy
.webhooks(this.appId)(token)
.messages(messageId)
.patch({
body: BaseInteraction.transformBody(data),
files: files ? await resolveFiles(files) : undefined,
});
return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token);
}
editOriginal(token: string, body: InteractionMessageUpdateBodyRequest) {
return this.editMessage(token, '@original', body);
}
deleteResponse(interactionId: string, token: string, messageId: string) {
return this.client.proxy
.webhooks(this.appId)(token)
.messages(messageId)
.delete()
.then(() => this.client.components?.onMessageDelete(messageId === '@original' ? interactionId : messageId));
}
deleteOriginal(interactionId: string, token: string) {
return this.deleteResponse(interactionId, token, '@original');
}
async followup(token: string, { files, ...body }: MessageWebhookCreateBodyRequest) {
files = files ? await resolveFiles(files) : undefined;
const apiMessage = await this.client.proxy
.webhooks(this.appId)(token)
.post({
body: BaseInteraction.transformBody(body),
files: files as RawFile[] | undefined,
});
return new WebhookMessage(this.client, apiMessage, this.appId, token);
}
}

View File

@ -325,7 +325,7 @@ export class Interaction<
Type extends APIInteraction = APIInteraction, Type extends APIInteraction = APIInteraction,
> extends BaseInteraction<FromGuild, Type> { > extends BaseInteraction<FromGuild, Type> {
fetchMessage(messageId: string) { fetchMessage(messageId: string) {
return this.client.webhooks.fetchMessage(this.applicationId, this.token, messageId); return this.client.interactions.fetchResponse(this.token, messageId);
} }
fetchResponse() { fetchResponse() {
@ -363,16 +363,8 @@ export class Interaction<
return this.write(body as InteractionCreateBodyRequest, fetchReply); return this.write(body as InteractionCreateBodyRequest, fetchReply);
} }
async editMessage(messageId: string, body: InteractionMessageUpdateBodyRequest) { editMessage(messageId: string, body: InteractionMessageUpdateBodyRequest) {
const { files, ...data } = body; return this.client.interactions.editMessage(this.token, messageId, body);
const apiMessage = await this.api
.webhooks(this.applicationId)(this.token)
.messages(messageId)
.patch({
body: BaseInteraction.transformBody(data),
files: files ? await resolveFiles(files) : undefined,
});
return new Message(this.client, apiMessage);
} }
editResponse(body: InteractionMessageUpdateBodyRequest) { editResponse(body: InteractionMessageUpdateBodyRequest) {
@ -384,22 +376,11 @@ export class Interaction<
} }
deleteMessage(messageId: string) { deleteMessage(messageId: string) {
return this.api return this.client.interactions.deleteResponse(this.id, this.token, messageId);
.webhooks(this.applicationId)(this.token)
.messages(messageId)
.delete()
.then(() => this.client.components?.onMessageDelete(messageId === '@original' ? this.id : messageId));
} }
async createResponse({ files, ...body }: MessageWebhookCreateBodyRequest) { async followup(body: MessageWebhookCreateBodyRequest) {
files ??= files ? await resolveFiles(files) : undefined; return this.client.interactions.followup(this.token, body);
const apiMessage = await this.api
.webhooks(this.applicationId)(this.token)
.post({
body: BaseInteraction.transformBody(body),
files: files as RawFile[] | undefined,
});
return new Message(this.client, apiMessage);
} }
} }