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

Attachment names are incorrectly parsed, preventing file sending #3341

Closed MysteryPancake closed 5 years ago

MysteryPancake commented 5 years ago

Please describe the problem you are having in as much detail as possible: Discord.js does not correctly deal with special characters (such as \n) in attachment file names, causing the API error "Cannot send an empty message", and failing to send the attachment.

Include a reproducible code sample here, if possible:

// DEFAULT BEHAVIOR (BROKEN):

message.channel.send({
    files: [{
        attachment: "anyfile.png",
        name: "test\ntest.png"
    }]
})

// POSSIBLE FIX:

message.channel.send({
    files: [{
        attachment: "anyfile.png",
        name: "test\ntest.png".replace(/[^a-z0-9]/gi, "_")
    }]
})

Further details:

monbrey commented 5 years ago

You could always not put a special character in the filename though. I don't think it's the responsibility of Discord.js to sanitize your filenames for the API. That's why it returns API errors to you.

MysteryPancake commented 5 years ago

I agree - I noticed that the filenames are also cut to a certain length, and I thought this was a feature of Discord.js rather than Discord's API itself.

It would be good to have a more descriptive error though - I only named a file this way by accident, and it took a lot of trial and error to work out the issue.