Home > Back-end >  How to return a png file using FS
How to return a png file using FS

Time:10-17

I was creating a DiscordJS Bot that is going to return a file saved in my local folder using FS. It can open and close folders, but the problem is, I need to return a png file, not its name. Right now, it only returns the name of the file, rather than the file itself. Is there any way I can return the file rather than its name? If needed, I will add the code itself, however, I think there is no need for the code since this is a theoretical question rather than a practical question.

CodePudding user response:

This just worked for me locally as it would read in the file buffer and send it as an attachment to the corresponding discord server channel.

const { Client, Intents } = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS] })
const fs = require('fs')

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`)

  client.guilds.cache
    .get('760214655120965664')
    .channels.cache.get('760214655653117963')
    .send({
      content: 'anyFile',
      files: [
        { name: 'file.png', attachment: fs.readFileSync('./packages/client/public/fav.png') },
      ],
    })
})

client.login('yourtoken')

Outcome:

outcome picture

Documentation/Reference: https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send

  • Related