Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

javascript - Avatar Command But With Id's and Tags

I am making an Avatar Command. It Works Fine. But I wanted to make it so that it listens to id's and tags as well. Like +avatar 656432172722290688. Mine only works with mentions right now. So what do I basically need to do, is to make it so that it also works with id's and tags.

My Code Right Now -:

    const user = message.mentions.users.first() || message.member.user;
let embed = new MessageEmbed()
    .setFooter(
        'Avatar Command',
        message.author.displayAvatarURL({ format: 'png', dynamic: true })
    )
    .setTimestamp()
    .setTitle(`${user.username}'s Avatar`)
    .setImage(user.displayAvatarURL({ format: 'png', dynamic: true }))
    .setColor('RANDOM');
message.channel.send(embed);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming you have an args variable containing the arguments of your command (where args[0] would contain the ID, tag, or mention), all you need to do is check if the first arg is an ID, a tag, or a mention; then, get the user using the first arg, with the way in which you do so differing based on what the first arg contained.

So here's an example:

var user;

if (!isNaN(args[0]) && args[0].length == 18) {
    //An ID was specified
    //Because first arg is a number and is 18 chars long

    console.log("Detected an ID");

    //Use message.member if a user with that ID is not found
    var member = message.guild.members.cache.get(args[0]) || message.member;
    user = member.user;
}
else if (args[0].match(/.+#[0-9]{4}/g)) {
    //A tag was specified
    //Because first arg is in format {username}#{four numbers}

    console.log("Detected a tag")

    //Use message.member if a user with that tag is not found
    var member = message.guild.members.cache.find(mem => mem.user.tag == args[0]) || message.member;
    user = member.user;
}
else {
    //Either a mention was specified or just use the message author

    console.log("Detected mention or miscellaneous");

    user = message.mentions.users.first() || message.author;
}

let embed = new MessageEmbed()
    .setFooter(
        'Avatar Command',
        message.author.displayAvatarURL({ format: 'png', dynamic: true })
    )
    .setTimestamp()
    .setTitle(`${user.username}'s Avatar`)
    .setImage(user.displayAvatarURL({ format: 'png', dynamic: true }))
    .setColor('RANDOM');
message.channel.send(embed);

This should work, or at the very least you can easily adjust it to work for you.

Now let me also note, for future reference, that you should not have asked this question on StackOverflow (or at the very least should have done a bit more before asking it). You didn't even try to make the code work with IDs and tags yourself, and if you did, you failed to include the code from your attempts in your question! We are here to help you if you're having a problem, not to write your bot for you. You should have looked at the docs and attempted this yourself; if your attempts didn't work, you should have included what you tried in your question and we would then be able to point out the issue. Please keep that in mind when asking other questions in the future, as it will certainly help you a ton both in getting answers quicker and in becoming a better programmer overall.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...