Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

725
Visualizações
Node.js: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

I'm trying to make a discord bot that saves images sent to it into a folder, and I'm getting this error:

TypeError: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined.

This is my code:

const Discord = require("discord.js");
const fs = require("fs");
const client = new Discord.Client();

var prefix = "."

client.on("ready", () => {
  client.user.setStatus('online');
  console.log("Bot is on.")
});

client.on("message", message => {
  console.log(__dirname)
  var image = message.attachments.first()
  fs.writeFileSync(`./${image.name}`, image.file);
})

client.login(process.env.token);

Note: I'm using Discord.js v12.5.3

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no file property on attachments. There is a url property though from where you can download the image. You can use node-fetch to fetch the file (you'll need to install it by running npm i node-fetch@2 in your command line). Once the file is fetched, you can use fs.writeFileSync().

The following code works fine:

// using discord.js v12
const { Client } = require('discord.js');
const fetch = require('node-fetch');
const fs = require('fs');

const client = new Client();
const prefix = '.';

client.on('message', async (message) => {
  if (message.author.bot) return;

  const image = message.attachments.first();

  if (!image)
    return console.log('No attached image found');

  // add more if needed
  const imageFiles = ['image/gif', 'image/jpeg', 'image/png', 'image/webp'];

  if (!imageFiles.includes(image.contentType))
    return console.log('Not an image file');

  try {
    const res = await fetch(image.url);
    const buffer = await res.buffer();

    fs.writeFileSync(`./${image.name}`, buffer);
  } catch (error) {
    console.log(`Error reading/writing image file:`, error);
  }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda