How do I get my bot to acknowledge that the user sent an image?

Asked

Viewed 50 times

0

I’m doing a bot on Discord, and one of the functions I want on it is this: I want my bot when receiving an image sent by a user to edit it and return as a meme. From of course a specific command. In question of the editing itself I’ve done, I used npm jimp which edits images by js, any image inside my folder that I put as a variable will be edited and answered by the user, but I want the edited image to be one of the user’s choice, I have already read the documentation of Discordjs and I have not found any hint about this command, anyway in case you want me to be more specific just ask here, I think, here is my code.

  module.exports = (client,msg)=>{ //exportando o client do meu bot e outros comandos
    var jimp = require('jimp'); //npm que eu usei para editar as imagens por javascript

    async function edicao(){ 
      const bolsonaro = await jimp.read('bolsonaro2.jpeg')// imagem que vai servir de base para o meme
      const image = await jimp.read('img.jpg')// imagem que vai ser editada
      
      image.resize(308, 171)
      image.rotate(-1);
      bolsonaro.composite(image, 320,181).write('beta.png')
      msg.reply(``, { files: ["beta.png"] })//resposta para o usuario que chamou pelo comando 
      console.log('Imagem enviada para o Discord')

      .catch(err => {
      console.log('error avatar')
      })
    }
    return edicao();
  }

1 answer

1

In the documentation of discord.js it is said that there is a property attachments in the received message. You can, for example, list the images with the code below:

mensagem.attachments.each(console.log);

If we consider that the code you provided is correct (which I think is unlikely because I think there were errors in timing), we will have something similar to the following:

const jimp = require('jimp');

const editar = async (anexo) => {
  // imagem que vai servir de base para o meme
  const imgBase = await jimp.read('bolsonaro2.jpeg');
  // imagem que vai ser editada
  const imgAnexo = await jimp.read(anexo.url);
  imgAnexo.resize(308, 171)
  imgAnexo.rotate(-1);
  const resultado = imgBase.composite(image, 320,181);
  const destino = `${anexo.name}-editada.png`;
  await resultado.writeAsync(destino);
  return destino;
};

const analisar = async (mensagem) => {
  const promessas = mensagem
    .attachments
    .map(editar);

  const imgsEditadas = await Promise.all(promessas);

  // resposta para o usuario que chamou pelo comando
  mensagem.reply('', { files: imgsEditadas });
};

module.exports = analisar;

Note that in my change only the message is required as a parameter, then it will be necessary to change in the place where the function is also called.


.attachments

A Collection of attachments in the message - e.g. Pictures - Mapped by their ids

In free translation:

A collection of attachments present in the message - e.g. Images - mapped by their ids

  • Actually, it seems that attachments realize that it is an image, and this even printing the information of it, but he error when trying to edit the image, I think he did not take directly the image, only the ID, the name, the link... Any suggestions?

  • @Muriilo just use the property url of the object in question in the function jimp.read

  • guy like me would put it in code, I’m pretty beginner yet, sorry.

  • @Muriilo does not know how to be clearer than in the previous comment. You have a part that reads the image and assigns the result to a variable (await jimp.read). Instead of passing the name of the image you will pass what is in the attribute url of the object contained in the collection attachments. So that I can help you more than that, at the very least you have to inform me the outcome of your console.log that I put in response

  • @Muriilo I made an edit in my reply to try to give an example of how your code would look after modified, but as you did not provide an executable example I will hardly be able to help more than that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.