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'
}