Automattic / node-canvas

Node canvas is a Cairo backed Canvas implementation for NodeJS.
10.14k stars 1.17k forks source link

Errore: tipo di immagine non supportato #2346

Closed AFDavi06 closed 3 months ago

AFDavi06 commented 8 months ago

`const Discord = require("discord.js") const client = new Discord.Client({ intents: Object.values(Discord.IntentsBitField.Flags)})

client.once("ready", () => { console.log(Logged in as ${client.user.tag}!); console.log(client.user.tag) }); client.login("token")

const { createCanvas, loadImage, MessageAttachment } = require('canvas');

client.on('guildMemberAdd', async (member) => { const welcomeChannel = client.channels.cache.get('1188962541842485353'); const rulesChannel = client.channels.cache.get('1192109584777760868'); const verifyChannel = client.channels.cache.get('1193611427450200078'); const infoChannel = client.channels.cache.get('1192102711718596688');

if (welcomeChannel && rulesChannel && verifyChannel && infoChannel) { console.log("Sto per inviare il messaggio di benvenuto a:", member.user.tag);

const canvas = createCanvas(1024, 500);
const ctx = canvas.getContext('2d');

const background = await loadImage("/static(1).png").catch(error => {
  console.error("Errore nel caricamento dell'immagine:", error);
});

if (background) {
  ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

  ctx.font = '72px sans-serif';
  ctx.fillStyle = '#ffffff';
  ctx.fillText('Welcome', 360, 360);

  ctx.beginPath();
  ctx.arc(512, 166, 128, 0, Math.PI * 2, true);
  ctx.stroke();
  ctx.fill();

  ctx.font = '42px sans-serif';
  ctx.textAlign = 'center';
  ctx.fillText(member.user.tag.toUpperCase(), 512, 410);
  ctx.font = '32px sans-serif';
  ctx.fillText(`You are the ${member.guild.memberCount}th`, 512, 455);

  ctx.beginPath();
  ctx.arc(512, 166, 119, 0, Math.PI * 2, true);
  ctx.closePath();
  ctx.clip();

  const avatar = await loadImage(member.user.displayAvatarURL({ format: 'png', size: 1024 }));
  ctx.drawImage(avatar, 393, 47, 238, 238);

  const attachment = new MessageAttachment(canvas.toBuffer(), `welcome-${member.id}.png`);

  try {
    welcomeChannel.send(`:wave: Hello ${member}, welcome to ${member.guild.name}!`, { files: [attachment] });
    welcomeChannel.send(`Don't forget to read the rules in ${rulesChannel} and verify yourself in ${verifyChannel}. For more information, check out ${infoChannel}.`);

    console.log("Messaggio di benvenuto inviato con successo a:", member.user.tag);
  } catch (error) {
    console.error("Errore nell'invio del messaggio di benvenuto", error);
  }
}

} }); ` node:events:497 throw er; // Unhandled 'error' event ^

Error: Unsupported image type at setSource (C:\Users\Utente\Desktop\DS\node_modules\canvas\lib\image.js:94:13) at C:\Users\Utente\Desktop\DS\node_modules\canvas\lib\image.js:62:11 at C:\Users\Utente\Desktop\DS\node_modules\simple-get\index.js:97:7 at IncomingMessage. (C:\Users\Utente\Desktop\DS\node_modules\simple-concat\index.js:8:13) at Object.onceWrapper (node:events:633:28) at IncomingMessage.emit (node:events:531:35) at endReadableNT (node:internal/streams/readable:1696:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:402:10) at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

Node.js v21.5.0

zbjornson commented 3 months ago

This is probably not an issue with node-canvas. More likely, the image is corrupted. If you still need help, can you post the image that's not loading please?