javascript - Discord.js TypeError:预期图像或画布
问题描述
解决方案
画布加载图像需要时间,因此需要异步
const Discord = require("discord.js");
const Canvas = require('canvas');
client.on('message', async message => {
if (message.content === '!test') {
const canvas = Canvas.createCanvas(934, 282);
const ctx = canvas.getContext('2d');
// we need to await the Promise gets resolved since loading of Image is async
const background = await Canvas.loadImage('./logo.jpg');
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'message.png'); 'logo.png');
msg.channel.send(`Testing...`, attachment);
}
});
编辑:使用 OPconst attachment = new Discord.Attachment(canvas.toBuffer(), 'logo.png');
而不是正确的版本const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'logo.png');
推荐阅读
- opencv - 我正在尝试制作 59 分钟和 59 秒的视频,但创建的视频总是持续时间较短
- swift - 如何在 SwiftUI 中将内容添加到收藏夹列表中?
- python - 如何使用 BeautifulSoup 循环链接并抓取新闻文章的内容
- powerapps - 使用下拉列表中的选定项目在 Power Apps 中显示项目
- reactjs - 如果可以被 3 整除,如何根据条件为列表中的特定项目分配不同的背景颜色?
- excel - 将工作表从另一个打开的工作簿复制到当前工作簿
- asp.net-core - ASP.NET Core 中的 HttpContext.TraceIdentifier 有多独特?
- sendgrid - Sendgrid:如何向电子邮件模板添加动态链接?
- css - 我有一个围绕 svg 旋转但不固定的 SVG
- r - 更改 boxpoints 的颜色并在 plotly boxplot 中添加分布曲线