Canvas help (discord.js)


#1

i need help with the assets area
i want my bot to use canvas but it cant load the image i have in assets
Picture:https://gyazo.com/a14e423eee0f6765adff14fea3ac66a4
Picture 2:https://gyazo.com/b186d4a8c86de0dec7183452f1312ecd
Error:
(node:4749) UnhandledPromiseRejectionWarning: Error: error while reading from input stream

at Image.src (/rbd/pnpm-volume/bfc224d2-0f81-4f4f-aa60-bb35a754e1e3/node_modules/.registry.npmjs.org/canvas/2.0.0-alpha.12/node_modules/canvas/lib/image.js:30:17)

at Promise (/rbd/pnpm-volume/bfc224d2-0f81-4f4f-aa60-bb35a754e1e3/node_modules/.registry.npmjs.org/canvas/2.0.0-alpha.12/node_modules/canvas/index.js:34:15)

at new Promise (<anonymous>)

at Object.loadImage (/rbd/pnpm-volume/bfc224d2-0f81-4f4f-aa60-bb35a754e1e3/node_modules/.registry.npmjs.org/canvas/2.0.0-alpha.12/node_modules/canvas/index.js:23:10)

at Client.client.on (/app/server.js:41:37)

Code:
tconst Discord = require(‘discord.js’);
const client = new Discord.Client();
const Canvas = require(‘canvas’);
client.on(‘guildMemberAdd’, async member => {
const channel = member.guild.channels.find(ch => ch.name === ‘member-log’);
if (!channel) return;

const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d');

// Since the image takes time to load, you should await it
const background = await Canvas.loadImage('./wallpaper.jpg');
// This uses the canvas dimensions to stretch the image onto the entire canvas
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
// Use helpful Attachment class structure to process the file for you
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');

channel.send(`Welcome to the server, ${member}!`, attachment);

});


#2

If you are loading the file from the assets folder in the editor, that folder isn’t really a folder, it just lists all the your project files which are in the Glitch CDN, you need to get the link for the file which you want to use, and then use it in your project.


#3

this is the link
so do i just replace wallpaper.jpg with that?


#4

No, you will need to change the loadImage to something like this https://github.com/Automattic/node-canvas#imagesrc
the img.src thing


#5

oh ok ill try that out


#6

got it to work!!!
IM so happy


#7

@HunterWilson56 How did you get it too work. I can’t seem to make it work.


#8

I managed to get into contact with HunterWilson56. And he helped me fix it.