1

I am making a Discord bot, and i want it to send a picture at a specific event (that is irrelevant). I'm facing a weird problem: whenever i try to use Canvas.createCanvas(x,y) my Node project exits without any error in logs (as im using it through batch, it just restarts and prints the 'ready' line). I think that the code is irrelevant here, because it just fails to do createCanvas.

I have tried adding console.log('line N') on each line with text, and from my testing it only comes to line 2. So line 3 (createCanvas) is not processed.

client.on('guildMemberAdd', async member => { //canvas IS defined earlier 
const channel = 'channel id';
if (!channel) return;

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

const background = await loadImage('./images/welcome.png');
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

ctx.strokeStyle = '#676767';
ctx.strokeRect(0, 0, canvas.width, canvas.height);

ctx.beginPath();
ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();

const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
const avatar = await loadImage(buffer);
ctx.drawImage(avatar, 25, 25, 200, 200);

const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');

channel.send(attachment);
});

I expect it to actually create the canvas and do the operations, but instead it just restarts the bot. Any help here?

loib
  • 23
  • 4
  • You are throwing an exception, but can't see what it is. Try wrapping the entire body of your async handler in a try/catch and printing the stack trace (ie `console.log(error.stack);`). Then you'll know what problem you need to fix. – Elliot Nelson Dec 24 '18 at 21:02
  • i tried it, well it didn't work. as i said, there is no exception, it looks as if its running out of memory or something – loib Dec 25 '18 at 12:22
  • on the other hand, i actually tested it in the 'eval' command, and it worked UNTIL i added an image with drawImage. so there could be a problem with that too – loib Dec 25 '18 at 12:57

0 Answers0