From 61103377048550ea26b9087adc0e029788ce965d Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Wed, 1 May 2024 17:14:08 -0400 Subject: [PATCH] feat: allowedMentions client option --- src/builders/Attachment.ts | 8 +++++++- src/client/base.ts | 5 ++++- src/common/shorters/interaction.ts | 15 +++++++++------ src/common/shorters/messages.ts | 4 ++-- src/common/shorters/webhook.ts | 10 ++++++++-- src/structures/Interaction.ts | 9 +++++++-- src/structures/channels.ts | 3 ++- 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/builders/Attachment.ts b/src/builders/Attachment.ts index 2da3f11..8132eff 100644 --- a/src/builders/Attachment.ts +++ b/src/builders/Attachment.ts @@ -172,7 +172,13 @@ export async function resolveFiles(resources: (AttachmentBuilder | RawFile | Att * @param type - The type of the attachment data. * @returns The resolved attachment data. */ -export async function resolveAttachmentData(data: AttachmentResolvable, type: AttachmentDataType) { +export async function resolveAttachmentData( + data: AttachmentResolvable, + type: AttachmentDataType, +): Promise<{ + data: AttachmentResolvable; + contentType?: string | null; +}> { if (data instanceof AttachmentBuilder) { if (!data.data.resolvable) return throwError('The attachment type has been expressed as attachment but cannot be resolved as one.'); diff --git a/src/client/base.ts b/src/client/base.ts index 78c9052..e5ece87 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -26,7 +26,7 @@ import { type MakeRequired, } from '../common'; -import type { LocaleString } from 'discord-api-types/rest/v10'; +import type { LocaleString, RESTPostAPIChannelMessageJSONBody } from 'discord-api-types/rest/v10'; import type { DeepPartial, IntentStrings, OmitInsert, PermissionStrings, When } from '../common/types/util'; import { ComponentHandler } from '../components/handler'; import { LangsHandler } from '../langs/handler'; @@ -340,6 +340,9 @@ export interface BaseClientOptions { onAfterRun?: Command['onAfterRun']; }; }; + allowedMentions?: Omit, 'parse'> & { + parse?: ('everyone' | 'roles' | 'users')[]; //nice types, d-api + }; } export interface StartOptions { diff --git a/src/common/shorters/interaction.ts b/src/common/shorters/interaction.ts index 42e9f91..024d198 100644 --- a/src/common/shorters/interaction.ts +++ b/src/common/shorters/interaction.ts @@ -11,10 +11,13 @@ export class InteractionShorter extends BaseShorter { return this.client.proxy .interactions(id)(token) .callback.post({ - body: BaseInteraction.transformBodyRequest({ - type: body.type, - data, - }), + body: BaseInteraction.transformBodyRequest( + { + type: body.type, + data, + }, + this.client, + ), files: files ? await resolveFiles(files) : undefined, }); } @@ -33,7 +36,7 @@ export class InteractionShorter extends BaseShorter { .webhooks(this.client.applicationId)(token) .messages(messageId) .patch({ - body: BaseInteraction.transformBody(data), + body: BaseInteraction.transformBody(data, this.client), files: files ? await resolveFiles(files) : undefined, }); return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token); @@ -60,7 +63,7 @@ export class InteractionShorter extends BaseShorter { const apiMessage = await this.client.proxy .webhooks(this.client.applicationId)(token) .post({ - body: BaseInteraction.transformBody(body), + body: BaseInteraction.transformBody(body, this.client), files: files as RawFile[] | undefined, }); return new WebhookMessage(this.client, apiMessage, this.client.applicationId, token); diff --git a/src/common/shorters/messages.ts b/src/common/shorters/messages.ts index 597fde7..2ae1f9c 100644 --- a/src/common/shorters/messages.ts +++ b/src/common/shorters/messages.ts @@ -14,7 +14,7 @@ export class MessageShorter extends BaseShorter { async write(channelId: string, { files, ...body }: MessageCreateBodyRequest) { const parsedFiles = files ? await resolveFiles(files) : []; - const transformedBody = MessagesMethods.transformMessageBody(body); + const transformedBody = MessagesMethods.transformMessageBody(body, this.client); return this.client.proxy .channels(channelId) .messages.post({ @@ -33,7 +33,7 @@ export class MessageShorter extends BaseShorter { .channels(channelId) .messages(messageId) .patch({ - body: MessagesMethods.transformMessageBody(body), + body: MessagesMethods.transformMessageBody(body, this.client), files: parsedFiles, }) .then(async message => { diff --git a/src/common/shorters/webhook.ts b/src/common/shorters/webhook.ts index d941818..9016c6d 100644 --- a/src/common/shorters/webhook.ts +++ b/src/common/shorters/webhook.ts @@ -83,7 +83,10 @@ export class WebhookShorter extends BaseShorter { */ async writeMessage(webhookId: string, token: string, { body: data, ...payload }: MessageWebhookMethodWriteParams) { const { files, ...body } = data; - const transformedBody = MessagesMethods.transformMessageBody(body); + const transformedBody = MessagesMethods.transformMessageBody( + body, + this.client, + ); const parsedFiles = files ? await resolveFiles(files) : []; return this.client.proxy .webhooks(webhookId)(token) @@ -105,7 +108,10 @@ export class WebhookShorter extends BaseShorter { { messageId, body: data, ...json }: MessageWebhookMethodEditParams, ) { const { files, ...body } = data; - const transformedBody = MessagesMethods.transformMessageBody(body); + const transformedBody = MessagesMethods.transformMessageBody( + body, + this.client, + ); const parsedFiles = files ? await resolveFiles(files) : []; return this.client.proxy .webhooks(webhookId)(token) diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 157219d..e9c706a 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -57,6 +57,7 @@ import { User } from './User'; import channelFrom from './channels'; import { DiscordBase } from './extra/DiscordBase'; import { PermissionsBitField } from './extra/Permissions'; +import type { BaseClient } from '../client/base'; export type ReplyInteractionBody = | { type: InteractionResponseType.Modal; data: ModalCreateBodyRequest } @@ -114,7 +115,7 @@ export class BaseInteraction< this.user = this.member?.user ?? new User(client, interaction.user!); } - static transformBodyRequest(body: ReplyInteractionBody): APIInteractionResponse { + static transformBodyRequest(body: ReplyInteractionBody, self: BaseClient): APIInteractionResponse { switch (body.type) { case InteractionResponseType.ApplicationCommandAutocompleteResult: case InteractionResponseType.DeferredMessageUpdate: @@ -127,6 +128,8 @@ export class BaseInteraction< type: body.type, //@ts-ignore data: { + //@ts-ignore + allowed_mentions: self.options?.allowedMentions, ...(body.data ?? {}), //@ts-ignore components: body.data?.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, @@ -164,9 +167,11 @@ export class BaseInteraction< | MessageUpdateBodyRequest | MessageCreateBodyRequest | MessageWebhookCreateBodyRequest, + self: UsingClient, ) { const poll = (body as MessageWebhookCreateBodyRequest).poll; return { + allowed_mentions: self.options?.allowedMentions, ...body, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, embeds: body?.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined, @@ -181,7 +186,7 @@ export class BaseInteraction< //@ts-expect-error const data = body.data instanceof Modal ? body.data : rest; return (this.replied = this.__reply({ - body: BaseInteraction.transformBodyRequest({ data, type: body.type }), + body: BaseInteraction.transformBodyRequest({ data, type: body.type }, this.client), files: files ? await resolveFiles(files) : undefined, }).then(() => (this.replied = true))); } diff --git a/src/structures/channels.ts b/src/structures/channels.ts index 04e0fd5..3d51bfd 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -250,9 +250,10 @@ export class MessagesMethods extends DiscordBase { }; } - static transformMessageBody(body: MessageCreateBodyRequest | MessageUpdateBodyRequest) { + static transformMessageBody(body: MessageCreateBodyRequest | MessageUpdateBodyRequest, self: UsingClient) { const poll = (body as MessageCreateBodyRequest).poll; return { + allowed_mentions: self.options?.allowedMentions, ...body, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, embeds: body.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined,