0
votes

So I am writing a Discord bot which gets and displays a bunch of data from the Hypixel API (an API for a popular Minecraft server). I have coded it so that it will display certain player statistics in the form of emebds. However, the API does not store data on statistics that have no value i.e. 0. This means that when a user tries to see that embed that bot errors as it cannot send a field with no value.

Originally I had an idea to store each variable of the API in an array and loop through that. This way, I could check so if any of these are undefined and replace them with 0 or N/A in the embed. However, this is a very long and messy process, so I was wondering whether it's possible to loop through the field values of an existing embed and check it that way.

This is the code I have as an example:

var bedwars_solos = new Discord.MessageEmbed()
        .setTitle(rank + ' ' + player.displayname + ' | Solo Bedwars')
        .setDescription('If a stat says `undefined`, the Hypixel API is currently not detecting it.')
        .addFields(
          { name: 'Games Played', value: (player.stats.Bedwars.eight_one_games_played_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Wins', value: (player.stats.Bedwars.eight_one_wins_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Winstreak', value: (player.stats.Bedwars.eight_one_winstreak).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Kills', value: (player.stats.Bedwars.eight_one_kills_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Void Kills', value: (player.stats.Bedwars.eight_one_void_kills_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Final Kills', value: (player.stats.Bedwars.eight_one_final_kills_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Deaths', value: (player.stats.Bedwars.eight_one_deaths_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Void Deaths', value: (player.stats.Bedwars.eight_one_void_deaths_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'Final Deaths', value: (player.stats.Bedwars.eight_one_final_deaths_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
          { name: 'K/D', value: (player.stats.Bedwars.eight_one_kills_bedwars / player.stats.Bedwars.eight_one_deaths_bedwars).toFixed(2), inline: true},
          { name: 'Final K/D', value: (player.stats.Bedwars.eight_one_final_kills_bedwars / player.stats.Bedwars.eight_one_final_deaths_bedwars).toFixed(2), inline: true},
          { name: 'Beds Broken', value: (player.stats.Bedwars.eight_one_beds_broken_bedwars).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), inline: true},
        )
        .setColor(0xFF5100)
        .setTimestamp()
        .setThumbnail('./thumbnails/BedWars.png')
        .setFooter('HypStats by SirArchibald')

The bot errors when it cannot find a value in the API as it cannot use .toString() on nothing.

1

1 Answers

1
votes

Instead of doing:

value: (player.stats.Bedwars.eight_one_games_played_bedwars).toString()

You can do:

value: (player.stats.Bedwars.eight_one_games_played_bedwars || 0).toString()

Note the || 0 part as 0 will be the value to be used for .toString when the previous field is undefined or equivalent to false.