The image is not sent

Hello. I would like that when a person joins my discord server a welcome image generated with canvas is sent in the welcome channel. Problem: the text is sent in the channel but not the image. Moreover, no error is returned in Visual Studio code.
16 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
UnNainTrue
UnNainTrueOP9mo ago
const welcomeChannelId = '1221463992480960605'; // ID du canal de bienvenue
const backgroundImage = './assets/background.png';

module.exports = {
name: Discord.Events.GuildMemberAdd,
async run(client, member) {
try {
console.log("Début de la génération de l'image...");
const canvas = createCanvas(800, 400);
const ctx = canvas.getContext("2d");
const background = await loadImage(backgroundImage);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

ctx.font = "36px Arial";
ctx.fillStyle = "#ffffff";
ctx.fillText(`Bienvenue, ${member.user.username} !`, 50, 50);

const avatar = await loadImage(member.user.displayAvatarURL({ extension: 'jpg', size: 128 }));
ctx.drawImage(avatar, 50, 100, 100, 100);

const buffer = canvas.toBuffer("image/png");
console.log("Image générée avec succès.");

console.log("Début de l'enregistrement de l'image sur le disque...");
await fs.promises.writeFile("./welcome_image.png", buffer);
console.log("Image enregistrée avec succès.");

console.log("Tentative d'envoi de l'image sur Discord...");
const welcomeChannel = client.channels.cache.get(welcomeChannelId);
if (welcomeChannel) {
console.log("Canal de bienvenue trouvé. Tentative d'envoi de l'image...");
await welcomeChannel.send(`Bienvenue ${member.user} !`, {
files: [{ attachment: './welcome_image.png' }] // Assurez-vous que le chemin est correct
});
console.log("Image envoyée avec succès sur Discord.");
} else {
console.error("Canal de bienvenue introuvable.");
}
} catch (error) {
console.error("Une erreur s'est produite :", error);
}
},
};
const welcomeChannelId = '1221463992480960605'; // ID du canal de bienvenue
const backgroundImage = './assets/background.png';

module.exports = {
name: Discord.Events.GuildMemberAdd,
async run(client, member) {
try {
console.log("Début de la génération de l'image...");
const canvas = createCanvas(800, 400);
const ctx = canvas.getContext("2d");
const background = await loadImage(backgroundImage);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

ctx.font = "36px Arial";
ctx.fillStyle = "#ffffff";
ctx.fillText(`Bienvenue, ${member.user.username} !`, 50, 50);

const avatar = await loadImage(member.user.displayAvatarURL({ extension: 'jpg', size: 128 }));
ctx.drawImage(avatar, 50, 100, 100, 100);

const buffer = canvas.toBuffer("image/png");
console.log("Image générée avec succès.");

console.log("Début de l'enregistrement de l'image sur le disque...");
await fs.promises.writeFile("./welcome_image.png", buffer);
console.log("Image enregistrée avec succès.");

console.log("Tentative d'envoi de l'image sur Discord...");
const welcomeChannel = client.channels.cache.get(welcomeChannelId);
if (welcomeChannel) {
console.log("Canal de bienvenue trouvé. Tentative d'envoi de l'image...");
await welcomeChannel.send(`Bienvenue ${member.user} !`, {
files: [{ attachment: './welcome_image.png' }] // Assurez-vous que le chemin est correct
});
console.log("Image envoyée avec succès sur Discord.");
} else {
console.error("Canal de bienvenue introuvable.");
}
} catch (error) {
console.error("Une erreur s'est produite :", error);
}
},
};
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs9mo ago
Files in embeds should be attached via the message option object and referenced in the embed:
const attachment = new AttachmentBuilder('./image.png', { name: 'image1.png' });
const embed = new EmbedBuilder()
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

channel.send({
embeds: [embed],
files: [attachment]
});
const attachment = new AttachmentBuilder('./image.png', { name: 'image1.png' });
const embed = new EmbedBuilder()
.setTitle('Attachments')
.setImage(`attachment://${attachment.name}`);

channel.send({
embeds: [embed],
files: [attachment]
});
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
UnNainTrue
UnNainTrueOP9mo ago
I'm sorry I don't know the JS actually the code given was generated via chatGPT If I've understood correctly, all I have to do is attachment://${attachment.name}`); right?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
UnNainTrue
UnNainTrueOP9mo ago
Can you just tell me if my reasoning is right please?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
UnNainTrue
UnNainTrueOP9mo ago
Ho okey I think I just understood Ah well no I tried with
new: AttachmentBuilder('./welcome_image.png', { name: 'welcome_image.png' }), but it doesn't work
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
UnNainTrue
UnNainTrueOP9mo ago
Look, I don't know, like I told you, I don't know JS. I was just trying to make a bot that works with the means at my disposal.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
axiprime2.0
axiprime2.09mo ago
Have you look at the discord js guide about canvas ?
axiprime2.0
axiprime2.09mo ago
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
UnNainTrue
UnNainTrueOP9mo ago
Oh nan I had not seen this guide The guide is for people who want to create an order. I haven't figured out how to attach a canvas-generated file to my code. I'm sorry if I sound silly. There's really something I don't understand, but in my code it doesn't work. Bah nique ca mère je vais me débrouiller puisque personne ne veux m'aider ici Oui oui j'en parlerais a mon cheval
Want results from more Discord servers?
Add your server