Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
Why is Canvas not working inside of an If statement?

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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')]
      })
    }
  })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda