Skulaurun

StackOverflow - "discord.js putting an image on another image"

Jul 29th, 2021
2,250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require("discord.js");
  2. const Jimp = require("jimp");
  3.  
  4. const client = new Discord.Client();
  5.  
  6. client.on("ready", async () => {
  7.     console.log(`Logged in as ${client.user.tag}!`);
  8. });
  9.  
  10. client.on("message", async (message) => {
  11.  
  12.     if (message.author.bot) return;
  13.  
  14.     const imageTemp = "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg";
  15.     const messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null;
  16.  
  17.     // Exit if there is no message attachment present in the message
  18.     if (!messageAttachment) return;
  19.  
  20.     // Resolve the temporary image url to Jimp object
  21.     const firstImage = await Jimp.read(imageTemp);
  22.     // Resolve the message attachment image url to Jimp object
  23.     const secondImage = await Jimp.read(messageAttachment);
  24.  
  25.     // Composite the two images together (modifies the firstImage object)
  26.     firstImage.composite(secondImage, 10, 10);
  27.  
  28.     // Save the resulting image to buffer
  29.     const buffer = await firstImage.getBufferAsync(Jimp.MIME_JPEG);
  30.  
  31.     // Send the buffer as a message attachment
  32.     message.channel.send("Combined image:", {files: [{ attachment: buffer }]});
  33.  
  34. });
  35.  
  36. client.login("...");
  37.  
RAW Paste Data