discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.38k stars 3.97k forks source link

Ability to send PDF & other file types with attachmentBuilder / Embed #9580

Closed jstdk closed 1 year ago

jstdk commented 1 year ago

Which application or package is this feature request for?

discord.js

Feature

Currently I am only able to send images using discord.js bots using attachmentBuilder. My users also have other filetypes to send.

Ideal solution or implementation

Use all filetypes in attachmentBuilder.

Alternative solutions or implementations

No response

Other context

No response

Jiralite commented 1 year ago

This sounds like you're doing something wrong. What are you attempting?

jstdk commented 1 year ago

@Jiralite. Imagine a downloadLink on AWS, can be any filetype. Below code works great for images, but for nothing else. On the discord.js Discord channel I was told that other file types are not supported, hence my feature request:

if (downloadLink !== null) {
            const file = new AttachmentBuilder(downloadLink);
            const embed = {
                title: messageMedia[0]['name'],
                image: {
                    url: 'attachment://' + messageMedia[0]['name'],
                },
            };
            sendMessage(discordBots[token], bodyText, messageId, channelId, embed, file);
        }
        else {
            console.log('DownloadLink is missing')
        }
async function sendMessage(discordBot: any, messageText: any, messageId: any, channelId: string, embed: any, file: any) {

    const channel = discordBot.channels.cache.get(channelId);
    try {
        const webhooks = await channel.fetchWebhooks();
        const webhook = webhooks.find(wh => wh.token);

        if (!webhook) {
            return console.log('No webhook was found that I can use!');
        }

        if (embed !== null && file !== null) {
            await webhook.send({
                content: messageText,
                embeds: [embed],
                files: [file]
            });
        }
        else {
            await webhook.send({
                content: messageText,
            });
        }

    } catch (error) {
        console.error('Error trying to send a message: ', error);
    }
Qjuh commented 1 year ago

Because you try to set it as image in an embed. You can only set images as image in embed. You can attach files of any kind though.

jstdk commented 1 year ago

Thanks @Qjuh , got it to work by removing the embed and only pushing the file in the message!