【问题标题】:Discord.js TypeError: Image or Canvas expectedDiscord.js TypeError:预期图像或画布
【发布时间】:2021-05-21 19:52:36
【问题描述】:

我训练使用文档中的示例制作 Canvas

但由于某种原因,示例中的代码不起作用。

怎么了?

■■■■■■错误■■■■■■■■

/discord-bot/index.js:143
    ctx.drawImage(background, 0, 0, canvas.width, canvas.height);  
        ^

TypeError: Image or Canvas expected
    at Client.client.on.message (/discord-bot/index.js:143:9)
    at Client.emit (events.js:194:15)
    at MessageCreateAction.handle (/discord-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/discord-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/discord-bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver.receiverOnMessage (/discord-bot/node_modules/ws/lib/websocket.js:825:20)

■■■■■■代码■■■■■■■■

const Discord = require("discord.js");
const Canvas = require('canvas');

   client.on('message', 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 = Canvas.loadImage('./logo.jpg');
        
        ctx.drawImage(background, 0, 0, canvas.width, canvas.height);  
        const attachment = new Discord.Attachment(canvas.toBuffer(), 'logo.png');
        msg.channel.send(`Testing...`, attachment);
        }
    });

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    canvas加载图片需要时间,所以需要异步

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

    编辑:OP 使用 const attachment = new Discord.Attachment(canvas.toBuffer(), 'logo.png'); 而不是正确的版本 const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'logo.png');

    【讨论】:

    • 新错误 — UnhandledPromiseRejectionWarning: TypeError: Discord.Attachment is not a constructor
    • @Andryxa,对,应该是const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'logo.png');
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2013-02-27
    • 2017-01-06
    • 2021-07-06
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多