Skip to content

Instantly share code, notes, and snippets.

@cborac
Forked from AppleCaps/myProblem
Last active June 29, 2020 02:01
Show Gist options
  • Save cborac/fa8c256504b266bb68905cb8c2e049ad to your computer and use it in GitHub Desktop.
Save cborac/fa8c256504b266bb68905cb8c2e049ad to your computer and use it in GitHub Desktop.
//C:\Users\BBY1\Desktop\Blackboard Bot
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!');
});
//Pass the entire Canvas object as you need to access its context and dimensions
//apply text
const applyText = (chartcanvas, text) => {
const ctx = chartcanvas.getContext('2d');
//Declare base size of font
let fontSize = 70;
do {
//Assign the font to the context and decrement it so it can be measured again
ctx.font = `${fontSize -= 10}px sans-serif`;
//Compare pixel width of the text to the canvas minus the approximate avatar fontSize
} while (ctx.measureText(text).width > chartcanvas.width - 300);
//Return the result to use in the actual Canvas
return ctx.font;
};
//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 = applyText(chartcanvas, topics[0]);
topicATextCTX.fillStyle = '#000000';
topicATextCTX.fillText = (topics[0], chartcanvas.width, chartcanvas.height);
topicBTextCTX.font = applyText(chartcanvas, topics[1]);
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;
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.channel.send(chartattachment);
}
backgroundImageCTX.drawImage(backgroundImage, 0, 0, chartcanvas.width, chartcanvas.height);
tchartImageCTX.drawImage(tchartImage, 100, 100, w, h);
message.reply('Creating the chart...');
message.channel.send(chartattachment);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment