diff --git a/src/models/rest.ts b/src/models/rest.ts index 9d00f62..f14e81d 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -1,4 +1,6 @@ import * as baseEndpoints from '../consts/urlsAndVersions.ts' +import { Embed } from '../structures/embed.ts' +import { MessageAttachment } from '../structures/message.ts' import { Collection } from '../utils/collection.ts' import { Client } from './client.ts' @@ -283,11 +285,46 @@ export class RESTManager { headers['X-Audit-Log-Reason'] = encodeURIComponent(body.reason) } - if (body?.file !== undefined) { + let _files: undefined | MessageAttachment[] + if (body?.embed?.files !== undefined && Array.isArray(body?.embed?.files)) { + _files = body?.embed?.files + } + if (body?.embeds !== undefined && Array.isArray(body?.embeds)) { + const files1 = body?.embeds + .map((e: Embed) => e.files) + .filter((e: MessageAttachment[]) => e !== undefined) + for (const files of files1) { + for (const file of files) { + if (_files === undefined) _files = [] + _files?.push(file) + } + } + } + + if ( + body?.file !== undefined || + body?.files !== undefined || + _files !== undefined + ) { + const files: Array<{ blob: Blob; name: string }> = [] + if (body?.file !== undefined) files.push(body.file) + if (body?.files !== undefined && Array.isArray(body.files)) { + for (const file of body.files) { + files.push(file) + } + } + if (_files !== undefined) { + for (const file of _files) { + files.push(file) + } + } const form = new FormData() - form.append('file', body.file.blob, body.file.name) + for (const file of files) { + form.append(file.name, file.blob, file.name) + } const json = JSON.stringify(body) form.append('payload_json', json) + if (body === undefined) body = {} body.file = form } else if ( body !== undefined && diff --git a/src/structures/embed.ts b/src/structures/embed.ts index 1936ddf..b2d8053 100644 --- a/src/structures/embed.ts +++ b/src/structures/embed.ts @@ -10,6 +10,7 @@ import { EmbedVideo } from '../types/channel.ts' import { Colors, ColorUtil } from '../utils/colorutil.ts' +import { MessageAttachment } from './message.ts' /** Message Embed Object */ export class Embed { @@ -26,6 +27,7 @@ export class Embed { provider?: EmbedProvider author?: EmbedAuthor fields?: EmbedField[] + files: MessageAttachment[] = [] constructor(data?: EmbedPayload) { this.title = data?.title @@ -74,6 +76,13 @@ export class Embed { return this } + attach(...files: MessageAttachment[]): Embed { + for (const file of files) { + this.files.push(file) + } + return this + } + /** Set Embed Type */ setType(type: EmbedTypes): Embed { this.type = type diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 5350479..7d89da2 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -79,6 +79,7 @@ export class TextChannel extends Channel { content: content, embed: option?.embed, file: option?.file, + files: option?.files, tts: option?.tts, allowed_mentions: option?.allowedMentions } diff --git a/src/test/index.ts b/src/test/index.ts index 528982c..d4263df 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -152,6 +152,22 @@ client.on('messageCreate', async (msg: Message) => { 'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1' ) }) + } else if (msg.content === '!emattach') { + msg.channel.send( + new Embed() + .attach( + await MessageAttachment.load( + 'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1', + 'file1.png' + ), + await MessageAttachment.load( + 'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1', + 'file2.png' + ) + ) + .setImage('attachment://file1.png') + .setThumbnail('attachment://file2.png') + ) } else if (msg.content === '!textfile') { msg.channel.send({ file: new MessageAttachment('hello.txt', 'hello world') diff --git a/src/types/channel.ts b/src/types/channel.ts index 25606a0..e1d44e0 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -159,7 +159,8 @@ export interface MessagePayload { export interface MessageOptions { tts?: boolean embed?: Embed - file?: MessageAttachment | string + file?: MessageAttachment + files?: MessageAttachment[] allowedMentions?: { parse?: 'everyone' | 'users' | 'roles' roles?: string[]