-3

i started working on a discord bot with discordjs and i wanted to get some data from the steam api and then to embed it

    const gtaUrl = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=KEY&format=json&appid=271590'
let gtaData ='';
request(gtaUrl,function(err,res,body){

    if(!err&&res.statusCode<400){
        gtaData +=body;
        }
})
const exampleEmbed =  new MessageEmbed()
.setColor('#0099ff')
.setTitle('Showing concurrent player numbers for some games')
.setDescription('the game')
.setThumbnail('https://i.imgur.com/FNviTdG.jpeg')
.addFields(
    { name: 'tf2 ', value: gtaData },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/AfFp7pu.png')
.setTimestamp()
message.channel.send({ embeds: [exampleEmbed] });

[the request and the embeding code ] but when i run the code it crashed and says that the embded fields are empty i think i know what the problem is when i send the request it takes time for the data to arrive and the code moves on so the strings stay empty but i dont really know what to do with is i still dont fully understand the whole js thing and im using express for the request part; the terminal error:

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

  • me having a grammatical error is not me not showing "professional courtesy" I speak 3 languages (english as the third) plus i have dyslexia, you dont have to be rude about it @isherwood – Constantine Jon Jun 28 '22 at 20:15

1 Answers1

0

You can just move your code inside request's callback

for example:

const gtaUrl =
  "https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=KEY&format=json&appid=271590";
let gtaData = "";
request(gtaUrl, function (err, res, body) {
  if (!err && res.statusCode < 400) {
    gtaData += body;

    const exampleEmbed = new MessageEmbed()
      .setColor("#0099ff")
      .setTitle("Showing concurrent player numbers for some games")
      .setDescription("the game")
      .setThumbnail("https://i.imgur.com/FNviTdG.jpeg")
      .addFields({ name: "tf2 ", value: gtaData })
      .addField("Inline field title", "Some value here", true)
      .setImage("https://i.imgur.com/AfFp7pu.png")
      .setTimestamp();
  }
});

Another way to do this is with Promises and Fetch or a library like Axios

Here's an example with Axios

const gtaUrl =
    "https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=KEY&format=json&appid=271590";
  
const response = await axios.get(gtaUrl);
gtaData = JSON.stringify(response.data);

const exampleEmbed = new MessageEmbed()
    .setColor("#0099ff")
    .setTitle("Showing concurrent player numbers for some games")
    .setDescription("the game")
    .setThumbnail("https://i.imgur.com/FNviTdG.jpeg")
    .addFields({ name: "tf2 ", value: gtaData })
    .addField("Inline field title", "Some value here", true)
    .setImage("https://i.imgur.com/AfFp7pu.png")
    .setTimestamp();
Jacob Stephenson
  • 544
  • 3
  • 13