简体   繁体   中英

Small discord.js Avatar image

Avatar issue example

在此处输入图片说明

I am trying to do the discord.js avatar command and it works. It sends the image it needs to send but the issue is that the image sent is small compared to other bots. Am using the command handler in the discord.js guide

const Discord = require('discord.js');
module.exports = {
    name: 'avatar',
    description: 'Get the avatar URL of the tagged user(s), or your own avatar.',
    aliases: ['av', 'a'],
    usage: '[commandname]',
    cooldown: 10,
    execute(message) {
        if (!message.mentions.users.size) {
            const embed = new Discord.MessageEmbed()
                .setTitle(message.author.username)
                .setColor(0x00ffff)
                .setImage(message.author.displayAvatarURL({ format: 'png' }));
            return message.channel.send(embed);
        }

        const mention = message.mentions.members.first();
        const Embed = new Discord.MessageEmbed()
            .setTitle(message.mentions.users.first().username)
            .setColor(0x00ffff)
            .setImage(mention.user.displayAvatarURL({ format: 'png' }));
        return message.channel.send(Embed);

    },
};

You can add a size option like how you did with you format

.displayAvatarURL({ format: 'png', size: size_you_want }));

the size as to be one of the following 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, For more info you can view the options here https://discord.js.org/#/docs/main/stable/typedef/ImageURLOptions

There are a few different solutions to this, but the one that I use and prefer is:

message.author.displayAvatarURL() + "?size=2048"

You can do whatever you want with this ^ if you want a full avatar command, its:

let embed = new Discord.MessageEmbed();
  if (!message.mentions.users.first()) {
    embed.setColor("00ff00");
    embed.setFooter("Your avatar!");
    embed.setImage(message.author.displayAvatarURL() + "?size=2048");
    message.channel.send(embed);
  } else {
    let user = message.mentions.users.first();
    embed.setFooter(`${user.tag}'s avatar!`);
    embed.setImage(message.mentions.users.first().displayAvatarURL() + "?size=2048");
    embed.setColor("#00ff00");
    message.channel.send(embed);
  }

( Discord.js v12 )

.setImage( user.displayAvatarURL({dynamic: true ,  size: 4096}))

While dynamic:true makes your avatar support all formats. If users avatar is a gif it will be a gif, if png it will be a png etc.

Without it users that have nitro animated avatar will have their avatar frozen on command.

Change the code from this:

const embed = new Discord.MessageEmbed()
                .setTitle(message.author.username)
                .setColor(0x00ffff)
                .setImage(message.author.displayAvatarURL({ format: 'png' }));
            return message.channel.send(embed);```

To this:

const embed = new Discord.MessageEmbed()
                .setTitle(message.author.username)
                .setColor(0x00ffff)
                .setImage(message.author.displayAvatarURL({ dynamic: true }));
            return message.channel.send(embed);```

You only need to add dynamic filter in the image and also remove format filter so it will work with gif image or the image will be static even the user has gif in pic.

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