Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send file from Buffer with utf-8 encoding issue on Discord channel view #10229

Closed
viruspowaa opened this issue Apr 25, 2024 · 1 comment
Closed

Comments

@viruspowaa
Copy link

Which package is this bug report for?

discord.js

Issue description

Hello,

I try to send file from Buffer, but I have an issue with encoding. My file content is not encode in 'utf-8'. Can you help me ?

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

Code sample

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>>");

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"

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMessages

I have tested this issue on a development release

No response

@Jiralite
Copy link
Member

When you download the file, it is fine.

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.

This is probably a bug. You can try submitting an issue to Discord via https://github.com/discord/discord-api-docs.

Overall: not a discord.js issue.

@Jiralite Jiralite closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants