discord.js canvas 图片处理

discord.js canvas image manipulation

我不想发出将用户头像放在我想要的图像中的命令

    const { createCanvas, loadImage } = require('canvas')
    const { MessageAttachment } = require('discord.js');
    const Command = require('../../structures/Command')

    module.exports = class extends Command {
        constructor(client) {
            super(client, {
                name: 'wanted',
                description: 'Coloca a cabeça de um usuário valendo 00.',
                options: [
                    {
                        name: 'user',
                        description: 'Usuário que terá a cabeça posta num cartaz de procurado',
                        type: 'USER',
                        required: true
                    }
                ]
            })
        }

        run = async (interaction) => {
            const canvas = createCanvas(239, 338)
            const ctx = canvas.getContext('2d')
            const wantedImg = await loadImage("imgs/wanted.png")
            const userAv = interaction.options.getUser('user')
            const Avatar = userAv.avatarURL()
            interaction.reply(wantedImg)
        }
    }

我将 wantedImg 放在 interaction.reply 中以查看客户端 return 聊天中是否有想要的图像,但出现错误...

Cannot find module 'C:\Users\pulse\OneDrive\Documentos\Cynthia\JS\src\commands\imgs\imgs'

确保你的路径是正确的,路径似乎无法正确解析。要确定您的路径,请考虑使用。

const {join} = require("path");

const wantedImg = loadImage(join(__dirname, "imgs/wanted.png"));

我建议改用 jimp

const user = message.mentions.users.first() //get The first user mentioned
if (!user) return message.reply("Who is wanted?")//return if no user was mentioned

var wantedImage = "wanted image url goes here"
var pfp = user.avatarURL({ format: 'png', dynamic: true, size: 128 }) //Get link of profile picture
var image = await Jimp.read(pfp)//read the profile picture, returns a Jimp object

//Composite resized bars on profile picture
image.composite((await Jimp.read(bars)).resize(128, 128), 0, 0)

//Create and attachment using buffer from edited picture and sending it
var image = new Discord.MessageAttachment(await image.getBufferAsync(Jimp.MIME_PNG))

message.reply(image) //replies to original command with image

它的选项比 canvas 多得多。