-1

how to fix await with forEach

const url = await Canvas.loadImage(getvalueof.displayAvatarURL( { format: "png", dynamic: true } ))
            ^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
client.on('voiceStateUpdate' , (Old,New) => {
    if(New.bot) return;
    if(Old.bot) return;

    if(New.channelId == voiceID) {
        New.guild.members.cache.filter(m => m.roles.cache.get("1026253368198451270")).forEach(async sa => {
            console.log("hi");  

            const w = ['4Skysupport.png'];

            let Image = Canvas.Image,
            canvas = new Canvas.createCanvas(1280, 720),
            ctx = canvas.getContext('2d');
            ctx.patternQuality = 'bilinear';
            ctx.filter = 'bilinear';
            ctx.antialias = 'subpixel';
            ctx.shadowColor = 'rgba(0, 0, 0, 0.4)';
            ctx.shadowOffsetY = 2;
            ctx.shadowBlur = 2;
            fs.readFile(`${w[Math.floor(Math.random() * w.length)]}`, function (err, Background) {
                if (err) return console.log(err);
                const BG = Canvas.Image;
                const ground = new Image;
                ground.src = Background;

                let user = New.member.user;
                var men = New.member.user;
                var heg;
                if(men) {
                    heg = men
                } else {
                    heg = New.member.user;
                }
                var mentionned = New.member.user;
                var h;
                if(mentionned) {
                    h = mentionned
                } else {
                    h = New.member.user;
                }
                var ment = New.member.user;
                var getvalueof;
                if(ment) {
                    getvalueof = New.member.user;
                } else {
                    getvalueof = New.member.user;
                }

                const url = await Canvas.loadImage(getvalueof.displayAvatarURL( { format: "png", dynamic: true } ))

                // Small Avatar
                let Avatar = Canvas.Image;
                let ava1 = new Avatar;
                ava1.src = url;
                ctx.beginPath();
                ctx.arc(400, 400, 0, 2 * Math.PI);
                ctx.drawImage(ava1, 450, 601, 80, 70);
                ctx.restore();
                ctx.font = '35px Arial Bold';
                ctx.fontSize = '40px';
                ctx.fillStyle = "#dadada";
                ctx.textAlign = "center";

                //background
                ctx.drawImage(ground, 0, 0, 1280, 720);

                var ment1 = sa.user;
                var getvalueof1;
                if(ment1) {
                    getvalueof1 = sa.user;
                } else {
                    getvalueof1 = sa.user;
                }
                const url1 = await Canvas.loadImage(getvalueof1.displayAvatarURL( { format: "png", dynamic: true } ))

                // Avatar
                let Avatar1 = Canvas.Image;
                let ava = new Avatar1;
                ava.src = url1;
                ctx.beginPath();
                ctx.drawImage(ava, 488, 50, 260, 250);
                ctx.font = '35px Arial Bold';
                ctx.fontSize = '40px';
                ctx.fillStyle = "#dadada";
                ctx.textAlign = "center";
                ctx.beginPath();
                ctx.stroke();
                sa.send({files: [canvas.toBuffer()]});
        
    
            })    

        })

    } 
});
Caladan
  • 2,261
  • 2
  • 6
  • 19
El3oso
  • 9
  • 1
  • 1
    In the first line, change `client.on('voiceStateUpdate' , (Old,New) => {` to `client.on('voiceStateUpdate' , async (Old,New) => {` – Caladan Oct 16 '22 at 08:31
  • 1
    still not working – El3oso Oct 16 '22 at 08:32
  • 2
    Does this answer your question? [await is only valid in async function](https://stackoverflow.com/questions/49432579/await-is-only-valid-in-async-function) – Zsolt Meszaros Oct 16 '22 at 08:33
  • 1
    Oh sorry, I didn't notice the `fs.readFile`. You will need to add the `async` keyword there. So, it would be like this => `fs.readFile(`${w[Math.floor(Math.random() * w.length)]}`, async function (err, Background) {` – Caladan Oct 16 '22 at 08:55

1 Answers1

1

for await is supposed to be used with asynchronous iterators

for await (const res of items.map(e => somethingAsync(e))) …

instead of

 New.guild.members.cache.filter(m => m.roles.cache.get("1026253368198451270")).forEach(async sa => 

you can use

   for await (const res of  New.guild.members.cache.filter(m => m.roles.cache.get("1026253368198451270"))) …