Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda