support multiple attachments

This commit is contained in:
DjDeveloperr 2021-01-28 19:55:37 +05:30
parent 87c15a9283
commit 60e2655085
5 changed files with 67 additions and 3 deletions

View file

@ -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 &&

View file

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

View file

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

View file

@ -151,6 +151,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')

View file

@ -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[]