discord.js 画布图像处理
Posted
技术标签:
【中文标题】discord.js 画布图像处理【英文标题】:discord.js canvas image manipulation 【发布时间】:2021-10-27 23:44:48 【问题描述】:我不想发出将用户头像放在我想要的图像中的命令
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 $2000.',
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 以查看客户端是否在聊天中返回想要的图像,但出现错误...
找不到模块 'C:\Users\pulse\OneDrive\Documentos\Cynthia\JS\src\commands\imgs\imgs'
【问题讨论】:
【参考方案1】:确保你的路径是正确的,路径似乎无法正确解析。要确定您的路径,请考虑使用。
const join = require("path");
const wantedImg = loadImage(join(__dirname, "imgs/wanted.png"));
【讨论】:
【参考方案2】:我建议改用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
它比画布有更多的选择。
【讨论】:
不支持的 MIME 类型:image/webp以上是关于discord.js 画布图像处理的主要内容,如果未能解决你的问题,请参考以下文章