I'm trying to make my code only show a piece of text when the database is gives a certain value.
const canvas = Canvas.createCanvas(250, 250);
const ctx = canvas.getContext('2d');
ctx.fillStyle = message.content;
ctx.fillRect(0, 0, canvas.height, canvas.width)
db.get("label"+message.author.id).then(value => {
console.log(value)
if(value == 'on') {
console.log('true')
ctx.font = '40px Poppins';
ctx.fillStyle = '#ffffff';
ctx.strokeStyle = '#000000'
ctx.lineWidth = 1;
ctx.fillText(message.content, canvas.width/10, canvas.height / 2 + 20);
ctx.strokeText(message.content, canvas.width/10, canvas.height / 2 + 20);
}
})
This is what I have so far. Unfortunately, no matter the value in the database, it won't add the text. I've tested it without this stuff, and it adds the text. Can someone tell me how to fix this?
Edit: Here's my complete and total code for this problem:
if(message.content.startsWith('#') && message.content.length == 7) {
const canvas = Canvas.createCanvas(250, 250);
const ctx = canvas.getContext('2d');
ctx.fillStyle = message.content;
ctx.fillRect(0, 0, canvas.height, canvas.width)
db.get("label"+message.author.id).then(value => {
console.log(value)
if(value == 'on') {
console.log('true')
ctx.font = '40px Poppins';
ctx.fillStyle = '#ffffff';
ctx.strokeStyle = '#000000'
ctx.lineWidth = 1;
ctx.fillText(message.content, canvas.width/10, canvas.height / 2 + 20);
ctx.strokeText(message.content, canvas.width/10, canvas.height / 2 + 20);
}
})
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'color.png');
message.channel.send(attachment)
}
You aren't sending the Canvas image, you're just building it. When you've finished building the image, you should use Canvas#toBuffer to make it compatible with discordjs#MessageAttachment.
const { MessageAttachment } = require('discord.js')
const canvas = Canvas.createCanvas(250, 250);
const ctx = canvas.getContext('2d');
ctx.fillStyle = message.content
ctx.fillRect(0, 0, canvas.height, canvas.width)
db.get('label' + message.author.id).then(value => {
console.log(value)
if (value == 'on') {
console.log('true')
ctx.font = '40px Poppins'
ctx.fillStyle = '#ffffff'
ctx.strokeStyle = '#000000'
ctx.lineWidth = 1
ctx.fillText(message.content, canvas.width / 10, canvas.height / 2 + 20)
ctx.strokeText(message.content, canvas.width / 10, canvas.height / 2 + 20)
message.reply({
files: [new MessageAttachment(canvas.toBuffer(), 'fileName.png')]
})
}
})