当我的不和谐机器人发送我的图像时,它为什么是空白?

时间:2020-06-29 02:26:33

标签: javascript node.js discord

我一直在尝试创建一个Discord机器人,该机器人现在显示一个带有两个主题的图表。出于某种原因,当我运行代码时,一切正常,直到尝试显示图像为止。图像只是……什么都没有。那里什么都没有。我的尺寸有误吗?我是在错误的时间显示内容,还是代码中的内容混乱?我怎样才能解决这个问题?我实际上并没有试图显示这两个主题,我只是想让它在此时显示和成像。

请耐心等待,我是一个初学者。

任何一种简化代码的方法都可以使用,谢谢。

const Discord = module.require("discord.js");
const bot = new Discord.Client();
const Canvas = require('canvas');
const prefix = '!';

bot.login('TOKEN');

//console.log: ready
bot.once('ready', () => {
    console.log('TCHART Ready!');
});



//tchart
bot.on('message', async message => {

    const topics = message.content.slice(prefix.length).split(' ');
    const command = topics.shift().toLowerCase();
    const topicsAB = topics.join(' ');

    //Define the images:
    const chartcanvas = Canvas.createCanvas(1920, 1080);

    const backgroundImageCTX = chartcanvas.getContext('2d');
    const tchartImageCTX = chartcanvas.getContext('2d');
    const topicATextCTX = chartcanvas.getContext('2d');
    const topicBTextCTX = chartcanvas.getContext('2d');

    const tchartImage = await Canvas.loadImage('./chart.png');
    const backgroundImage = await Canvas.loadImage('./blackboard.jpg');

    const chartattachment = new Discord.MessageAttachment(chartcanvas.toBuffer(), 'blackboard.png');


    //Defining Text:

    topicATextCTX.font = ('100px sans-serif');
    topicATextCTX.fillStyle = '#000000';
    topicATextCTX.fillText = (topics[0], chartcanvas.width, chartcanvas.height);

    topicBTextCTX.font = ('100px sans-serif');
    topicBTextCTX.fillStyle = '#000000';
    topicBTextCTX.fillText = (topics[1], chartcanvas.width, chartcanvas.height);



    //Scale and size of the tchart:
    const scale = Math.min(chartcanvas.width / chartcanvas.width, chartcanvas.height / chartcanvas.height);
    const w = chartcanvas.width * scale;
    const h = chartcanvas.height * scale;

    //Offset of the tchart
    const leftOffset = chartcanvas.width / 2 - w/2;
    const topOffset = chartcanvas.height / 2 - h/2;

    backgroundImageCTX.drawImage(backgroundImage, 100, 100, chartcanvas.height, chartcanvas.width);

    tchartImageCTX.drawImage(tchartImage, 100, 100, chartcanvas.height, chartcanvas.width);


    if (!message.content.startsWith(prefix) || message.author.bot) return;

    else if (command === 'tchart') {
        if (!topics.length) {
            return message.channel.send(`You didn't provide any equations, ${message.author}!`);
        }

            message.channel.send(`Command name: ${command}\nArguments: ${topicsAB}`);
            message.reply('Creating the chart...');
            message.channel.send(chartattachment);
    }



});```

0 个答案:

没有答案