简体   繁体   中英

Image Size in Discord.js

I'm making a command to display someone's avatar in discord such as !avatar < @user >/< userID >. I want to display the image size as 256 x 256, not 128 x 128. I found a way but now I'm just confused on what's happening here, maybe it's the user.avatarURL .

the code:

const Discord = require('discord.js');
const Canvas = require('node-canvas');
module.exports = {
name: 'avatar',

async run(message, args) {
    const canvas = Canvas.createCanvas(256, 256);
    const context = canvas.getContext('2d');
    const user = message.mentions.users.first() || message.author;

const avatarEmbed = new Discord.MessageEmbed()
        const background = await Canvas.loadImage(user.avatarURL())
        context.drawImage(background, 0, 0, canvas.width, canvas.height)
        const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'avatar.png')
  .setTitle(user.username)
  .setImage(attachment)
        .setColor('RED');

    message.channel.send(avatarEmbed)
}

}

error:

(node:1464) UnhandledPromiseRejectionWarning: Error: Unsupported image type

the rest is just locations of package files. I appreciate your help!

You don't need to write image from discord.js into canvas and resend it.Discord embed allow you to using image url.

And to get specific size of avatar you can using options size

Bonus: options: dynamic is for gif avatar

message.mentions.users.first().displayAvatarURL({ size: 1024, dynamic: true })

Finally your code can be something look like

const user = message.mentions.users.first() || message.author;
const avatarEmbed = new Discord.MessageEmbed()
  .setTitle(user.username)
  .setImage(user.displayAvatarURL({ size: 1024, dynamic: true }))
        .setColor('RED');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM