gpt4 book ai didi

javascript - await 仅在异步函数 Canvas Discord JS 中有效

转载 作者:行者123 更新时间:2023-12-04 00:07:23 34 4
gpt4 key购买 nike

我对编码还很陌生,并遵循了有关如何使用 Canvas 进行图像处理的在线教程,但我遇到了这个错误,抱歉打扰了!感谢任何帮助

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);
}

最佳答案

正是错误所说的。您在其中等待任何其他函数的函数必须是异步函数。

例如:

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

不过

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

关于javascript - await 仅在异步函数 Canvas Discord JS 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61274842/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com