feat: allowedMentions client option

This commit is contained in:
MARCROCK22 2024-05-01 17:14:08 -04:00
parent 5ad5132fa5
commit 6110337704
7 changed files with 39 additions and 15 deletions

View File

@ -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.');

View File

@ -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<NonNullable<RESTPostAPIChannelMessageJSONBody['allowed_mentions']>, 'parse'> & {
parse?: ('everyone' | 'roles' | 'users')[]; //nice types, d-api
};
}
export interface StartOptions {

View File

@ -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);

View File

@ -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<RESTPostAPIChannelMessageJSONBody>(body);
const transformedBody = MessagesMethods.transformMessageBody<RESTPostAPIChannelMessageJSONBody>(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<RESTPatchAPIChannelMessageJSONBody>(body),
body: MessagesMethods.transformMessageBody<RESTPatchAPIChannelMessageJSONBody>(body, this.client),
files: parsedFiles,
})
.then(async message => {

View File

@ -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<RESTPostAPIWebhookWithTokenJSONBody>(body);
const transformedBody = MessagesMethods.transformMessageBody<RESTPostAPIWebhookWithTokenJSONBody>(
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<RESTPostAPIWebhookWithTokenJSONBody>(body);
const transformedBody = MessagesMethods.transformMessageBody<RESTPostAPIWebhookWithTokenJSONBody>(
body,
this.client,
);
const parsedFiles = files ? await resolveFiles(files) : [];
return this.client.proxy
.webhooks(webhookId)(token)

View File

@ -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)));
}

View File

@ -250,9 +250,10 @@ export class MessagesMethods extends DiscordBase {
};
}
static transformMessageBody<T>(body: MessageCreateBodyRequest | MessageUpdateBodyRequest) {
static transformMessageBody<T>(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,