I've begun developing a Discord bot using Discord.js

I have started creating a Discord bot utilizing Discord.js, and I’m looking to retrieve data from the Steam API to incorporate it into an embedded message. Here’s a code snippet I’ve been working on:

const steamApiUrl = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=YOUR_API_KEY&format=json&appid=271590';
let playerData = '';
request(steamApiUrl, function(error, response, data) {
    if (!error && response.statusCode < 400) {
        playerData += data;
    }
});

const embedMessage = new MessageEmbed()
    .setColor('#0099ff')
    .setTitle('Current player counts for various games')
    .setDescription('Game Details')
    .setThumbnail('https://i.imgur.com/FNviTdG.jpeg')
    .addFields(
        { name: 'TF2', value: playerData },
    )
    .addField('Field Title', 'Value Here', true)
    .setImage('https://i.imgur.com/AfFp7pu.png')
    .setTimestamp();

message.channel.send({ embeds: [embedMessage] });

I’m facing an issue when I run the code, as it crashes and indicates that the embed fields are blank. I suspect this is due to the time it takes for the data to be returned from the request, as my code continues running, leaving the strings empty. I’m still trying to grasp JavaScript fully and I’m using Express for this request portion. The error I receive in the terminal is:

if (!allowEmpty && data.length === 0) throw new error(errorMessage);
RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values must be non-empty strings.
 at Util.verifyString (D:\Code\discordBot\node_modules\discord.js\src\util\Util.js:428:49)
 at MessageEmbed.normalizeField (D:\Code\discordBot\node_modules\discord.js\src\structures\MessageEmbed.js:544:19)
 at D:\Code\discordBot\node_modules\discord.js\src\structures\MessageEmbed.js:565:14
 at Array.map ()
 at MessageEmbed.normalizeFields (D:\Code\discordBot\node_modules\discord.js\src\structures\MessageEmbed.js:564:8)
 at MessageEmbed.addFields (D:\Code\discordBot\node_modules\discord.js\src\structures\MessageEmbed.js:328:42)
 at steamStatus (D:\Code\discordBot\src\bot.js:112:3)
 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
 [Symbol(code)]: 'EMBED_FIELD_VALUE'
}

hey emma! to fix this, try using promises or async/await for your req.response. the request method is async, so the rest of the code executes before playerData is set. check out node-fetch or axios, they’re quite popular and easier to use for fetching data. good luck!

hey there! have you tried using json.parse on the data to get the player count specifically? might be why the field shows empty if you’re not extracting the right part of the response. also check network errors in your console, sometimes it’s something small. gr8 work so far!

You might also want to utilize a JavaScript promise to handle the asynchronous operation better. The request library, although functional, is pretty outdated these days, and using a more contemporary library like node-fetch or axios can provide a promise-based API automatically. By using async/await in your function, you can ensure that your Discord embed only gets constructed and sent once the player data has arrived. Moreover, make sure to safeguard against any potential API call failures by adding error handling around your fetch operation. This will make your bot more robust and prevent the crash if the Steam API becomes unavailable or returns unexpected data.