Alice Bardout
Alice Bardout

Reputation: 31

Await is only valid in async function Canvas Discord JS

I'm quite new to coding and followed a tutorial online on how to use canvas for image manipulation but I am getting this error, sorry to bother! Any help is appreciated

if (message.author.bot) return;
    msg = message.content.toLowerCase();
    if(msg.includes("test")) {
        const sayMessage = message.content.split(' ').slice(2).join(' ')

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

        const background = await Canvas.loadImage('./wallpaper.jpg');
        ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

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

        ctx.font = applyText(canvas, sayMessage);
        ctx.fillStyle = '#ffffff';
        ctx.fillText(sayMessage, canvas.width / 2.5, canvas.height / 1.8);

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

        const avatar = await Canvas.loadImage(member.user.displayAvatarURL({ format: 'jpg' }));
        ctx.drawImage(avatar, 25, 25, 200, 200);

        const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'test.png');

        channel.send(attachment);
    } 

Upvotes: 2

Views: 358

Answers (2)

Aci
Aci

Reputation: 566

You most likely need to add an async to your .onMessage event. It should look somewhat like this:

bot.on('message', async message => {
    //do your commands here
})

Notice the async in front of the message => {} part. That's what allows you to use await in the onMessage event.

Upvotes: 2

Kelsnare
Kelsnare

Reputation: 705

exactly what the error says. The function inside which you await any other function must be an async one.

e.g:

async function wait() {
    await somefunction() // correct because function is async
}

However

function wait() {
    await somefunction() // error
}

Upvotes: 5

Related Questions