From 42873982a28be46fa5d5a6a17daec40e67220ed7 Mon Sep 17 00:00:00 2001 From: Fleny Date: Thu, 21 Sep 2023 16:05:28 +0200 Subject: [PATCH] Use `desiredProperties` in `transformAttachment` (#3134) * Use desiredProperties in transformAttachment * remove not necessary payload checks * add checks for payload --- packages/bot/src/transformers/attachment.ts | 55 +++++++++++++++------ 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/bot/src/transformers/attachment.ts b/packages/bot/src/transformers/attachment.ts index 31fcd990a..a8c7fb61f 100644 --- a/packages/bot/src/transformers/attachment.ts +++ b/packages/bot/src/transformers/attachment.ts @@ -1,23 +1,48 @@ import type { DiscordAttachment } from '@discordeno/types' import type { Bot } from '../index.js' -import type { Optionalize } from '../optionalize.js' // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function transformAttachment(bot: Bot, payload: DiscordAttachment) { - const attachment = { - id: bot.transformers.snowflake(payload.id), - filename: payload.filename, - contentType: payload.content_type, - size: payload.size, - url: payload.url, - proxyUrl: payload.proxy_url, - height: payload.height ?? undefined, - width: payload.width ?? undefined, - ephemeral: payload.ephemeral, - description: payload.description, - } + const props = bot.transformers.desiredProperties.attachment + const attachment = {} as Attachment - return attachment as Optionalize + if (props.id && payload.id) attachment.id = bot.transformers.snowflake(payload.id) + if (props.filename && payload.filename) attachment.filename = payload.filename + if (props.contentType && payload.content_type) attachment.contentType = payload.content_type + if (props.size && payload.size) attachment.size = payload.size + if (props.url && payload.url) attachment.url = payload.url + if (props.proxyUrl && payload.proxy_url) attachment.proxyUrl = payload.proxy_url + if (props.height && payload.height) attachment.height = payload.height + if (props.width && payload.width) attachment.width = payload.width + if (props.ephemeral && payload.ephemeral) attachment.ephemeral = payload.ephemeral + if (props.description && payload.description) attachment.description = payload.description + + return attachment } -export interface Attachment extends ReturnType {} +export interface Attachment { + /** Name of file attached */ + filename: string + /** The attachment's [media type](https://en.wikipedia.org/wiki/Media_type) */ + contentType?: string + /** Size of file in bytes */ + size: number + /** Source url of file */ + url: string + /** A proxied url of file */ + proxyUrl: string + /** Attachment id */ + id: bigint + /** description for the file (max 1024 characters) */ + description?: string + /** Height of file (if image) */ + height?: number + /** Width of file (if image) */ + width?: number + /** + * whether this attachment is ephemeral. + * Ephemeral attachments will automatically be removed after a set period of time. + * Ephemeral attachments on messages are guaranteed to be available as long as the message itself exists. + */ + ephemeral?: boolean +}