#The image is not sent

30 messages · Page 1 of 1 (latest)

grave minnow
#

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.

heavy gateBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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
grave minnow
#


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);
        }
    },
};
coral gull
#

It takes one object

#

.send({ content: <String>, files: [ attachment ] })

#

Also, why save it to your system, you can just pass the buffer into an AttachmentBuilder

thorn horizonBOT
#

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]
});
coral gull
#

Just ignore the embed builder part

grave minnow
#

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?

coral gull
#
  1. Don’t use ai to generate code for this specific situation. It’s trained on old data, and old versions of djs.
  2. I basically just coded 70% of it for you, and gave you the rest of the 30%
coral gull
grave minnow
#

Can you just tell me if my reasoning is right please?

coral gull
grave minnow
#

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

coral gull
#

Also, why save it to your system, you can just pass the buffer into an AttachmentBuilder

#

you can just pass the buffer

grave minnow
#

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.

coral gull
#

Then please follow our rules, #rules . It’s for your own benefit since making a discord bot requires some knowledge of JavaScript

raw herald
grave minnow
grave minnow
thick burrow
#

That guide literally shows how to do that though

grave minnow
#

There's really something I don't understand, but in my code it doesn't work.

thick burrow
#

#1081585952654360687 as canvas still is not discord.js

#

Or follow #how-to-get-help if it i djs related

grave minnow
#

Bah nique ca mère je vais me débrouiller puisque personne ne veux m'aider ici

thick burrow
#

#rules 2