Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

728
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda