discord / discord-api-docs

Official Discord API Documentation
https://discord.com/developers/docs/intro
Other
5.95k stars 1.26k forks source link

Wrong encoding in file preview message with Buffer #6823

Open viruspowaa opened 6 months ago

viruspowaa commented 6 months ago

Description

Hello,

I try to send file from Buffer, but I have an issue with encoding. My file content is encode in 'utf-8' but Discord message preview is not. When Discord unfurls the attachment as a preview, it looks like it's content sniffing. In this case, it appears to be text/plain; charset=ISO-8859-1.

bot.ts

import { AttachmentBuilder, Client, Events, GatewayIntentBits, TextChannel } from "discord.js";
import { Buffer } from "node:buffer";

const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

client.once(Events.ClientReady, () => {
  // 🤖 Discord bot is ready! 🤖
});

client.on(Events.GuildAvailable, async (guild) => {
  const bufTest = Buffer.from("Récapitulatif", "utf-8");
  console.log("bufTest :>> ", bufTest);
  console.log("bufTest value :>> ", bufTest.toString());
  const channel = guild.channels.cache.get("<<CHANNEL_ID>>") as TextChannel;
  const bTst = new AttachmentBuilder(bufTest, {
    name: "tst.log",
    description: "Test",
  });
  channel.send({ content: "test 2", files: [bTst] });
});

client.login("<<TOKEN>>");

Console result

bufTest :>>  <Buffer 52 c3 a9 63 61 70 69 74 75 6c 61 74 69 66>
bufTest value :>>  Récapitulatif

Discord result

image

Steps to Reproduce

Run code of 'bot.ts' previously write

Expected Behavior

I want that Discord show the file in the good encoding

Current Behavior

Wrong encoding in preview

Screenshots/Videos

image

Client and System Information

Versions

"discord.js": "^14.12.1", "dotenv": "^16.4.5", "@types/node": "^20.11.20", "tsup": "^8.0.2", "tsx": "^4.7.1", "typescript": "^5.3.3"

gateway intents

Guilds, GuildMessages

afgiel commented 6 months ago

do you have a repro that does not involve using a library? (eg curl)

viruspowaa commented 6 months ago

No ! I have not ! But I can try to make an example with it ! But how do it ? How can I use curl to send a message with buffered file ?

linus-jansson commented 4 months ago

do you have a repro that does not involve using a library? (eg curl)

Kinda still not plain curl but I also kind of experience this, when writing to a file, specifically in a github action running a javascript file using node 20. It seems to not be able to encode properly.

for example https://github.com/linus-jansson/wordgametester/blob/782d4e2ccd33751f6110f7a991d0880ad3c8ec52/wordguesser/main.ts

which I then run in: https://github.com/linus-jansson/wordgametester/blob/782d4e2ccd33751f6110f7a991d0880ad3c8ec52/.github/workflows/get-word.yml

The step Save example file in the workflow sends it correctly to the discord webhook and I get a "content_type":"text/plain; charset=utf-8" as a response from the API. When sending the file from the "node saved file" I get a "content_type":"text/plain; charset=ISO-8859-9" response from the API.

Maybe there's a bug in the fs module in node? Any ideas how to mitigate this?