首页 > 解决方案 > Discord.js TypeError:预期图像或画布

问题描述

标签: javascriptdiscorddiscord.js

解决方案


画布加载图像需要时间,因此需要异步

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


推荐阅读