簡體   English   中英

如何在 discord.js 中為 discord bot 合並圖像?

[英]How do I merge images in discord.js for a discord bot?

我正在嘗試制作一個將頭像與圖像合並的命令,例如,如果有人輸入 =jail,則頭像上的監獄欄 - 我已經制作了一個對命令進行排序的命令處理程序。 但是,我無法弄清楚如何實現這一目標。 我使用的是 windows 平台(顯然這意味着我不能使用 node gd)

你可以使用Jimp。 這是 NPM 站點: https : //www.npmjs.com/package/jimp

它允許您修改圖片、添加文本等。您想要的看起來類似於:

//an array of all images we're using. MAKE SURE THEIR SIZES MATCH
var images = [<link to the user's avatar>, <link to an image of jail bars>]
var jimps = []
//turns the images into readable variables for jimp, then pushes them into a new array
for (var i = 0; i < images.length; i++){
    jimps.push(jimp.read(images[i]))
}
//creates a promise to handle the jimps
await Promise.all(jimps).then(function(data) {
    return Promise.all(jimps)
}).then(async function(data){
    // --- THIS IS WHERE YOU MODIFY THE IMAGES --- \\
    data[0].composite(data[1], 0, 0) //adds the second specified image (the jail bars) on top of the first specified image (the avatar). "0, 0" define where the second image is placed, originating from the top left corner
    //you CAN resize the second image to fit the first one like this, if necessary. The "100, 100" is the new size in pixels.
    //data[1].resize(100,100)

    //this saves our modified image
    data[0].write(<path on your local disk to save the image in>)
})

現在您要做的就是從本地磁盤發送圖像:

message.channel.send(`Jailed!`, {file: `${<path to the image>}`})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM