replacing badge names to words

if(!target) {
    message.reply(` Mention a user if you want to find their info. `)
} else {
  
    var member = message.guild.members.cache.get(target.id)  

    if (target.flags == "HOUSE_BALANCE") target.flags = "Hypesquad Balance";
    if (target.flags == "HOUSE_BRILLIANCE") target.flags = "Hypesquad Brillance";
    if (target.flags == "HOUSE_BRAVERY") target.flags = "Hypesquad Bravery";
    if (target.flags == "VERIFIED_BOT") target.flags = "Verified Bot";
    let flags = target.flags.toArray()

    let embed = new Discord.MessageEmbed()
        .setTitle(`Userinfo`) 
        .setColor(`BLUE`)
        .setThumbnail(target.avatarURL({ size: 2048, dynamic: true }))
        .addFields (
            {name: `Username + tag`, value: `` + target.tag, inline: true},
            {name: `Server join date`, value: `` + `${moment.utc(member.joinedAt).format("dddd, MMMM Do YYYY, h:mm:ss a")} UTC`, inline: true },
            {name: `User creation date`, value: `` + `${moment.utc(target.createdAt).format("dddd, MMMM Do YYYY, h:mm:ss a")} UTC`, inine: true},
            // {name: `Status`, value: ``+ member.status}
            {name: `Badges`, value: `` + flags, inline: true}
         
          )
    message.channel.send({embeds: })

}

I’m trying to get badges from a user. However, it will display like HOUSE_BALANCE for any user having the Hypesquad Balance badge, HOUSE_BRILLANCE for Hypesquad Brillance, etc. However, I would like to replace those with like Hypesquad Balance for Hypesquad Balance badge, Hypesquad Brillance for Hypesquad Brillance badge, etc. The code above displays my best try of replacement but was unsuccessfully done. What should I type instead?

Answer

As user15517071 said, UserFlags is a BitField. You should use the has method to check whether a user has a flag/badge.

Note that a user may have multiple flags (e.g. HOUSE_BRILLIANCE and EARLY_VERIFIED_BOT_DEVELOPER).

The code might look like this:

const badges = []
if (target.flags.has("HOUSE_BALANCE")) badges.push("Hypesquad Balance");
if (target.flags.has("HOUSE_BRILLIANCE")) badges.push("Hypesquad Brilliance");
if (target.flags.has("HOUSE_BRAVERY")) badges.push("Hypesquad Bravery");
if (target.flags.has("VERIFIED_BOT")) badges.push("Verified Bot");

const embed = new Discord.MessageEmbed()
    // ...
    .addFields (
        // ...
        {name: `Badges`, value: badges.join(', '), inline: true}
    );

Alternatively, you could use an object/map to store the badge names of each user flag, and use this to get the badge names of a user:

const flagToBadgeName = {
    HOUSE_BALANCE: "Hypesquad Balance",
    HOUSE_BRILLIANCE: "Hypesquad Brillance",
    HOUSE_BRAVERY: "Hypesquad Bravery",
    VERIFIED_BOT: "Verified Bot"
    // etc
};

const badges = target.flags
    .toArray()
    .map(flag => flagToBadgeName[flag])
    .filter(name => name !== undefined);

const embed = new Discord.MessageEmbed()
    // ...
    .addFields (
        // ...
        {name: `Badges`, value: badges.join(', '), inline: true}
    );