Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've created a Discord bot that will give me Covid statistics on any country on command, however it's only displayed to me in raw text, I've seen images of embeds like this:

enter image description here

I'm interested to have my data displayed like this in my bots reply, this is the code I am using for it:

const axios = require('axios');
const countries = require("./countries.json");
const url = 'https://api.covid19api.com/total/country/';
const WAKE_COMMAND = 'cases';

client.on('message', async (msg) => {
  const content = msg.content.split(/[ ,]+/);
  if(content[0] === WAKE_COMMAND){
    if(content.length > 2){
      msg.reply("Too many arguments...")
    }
    else if(content.length === 1){
      msg.reply("Not enough arguments")
    }
    else if(!countries[content[1]]){
      msg.reply("Wrong country format")
    }
    else{
      const slug = content[1]
      const payload = await axios.get(`${url}${slug}`)
      const covidData = payload.data.pop();
      msg.reply(`Confirmed: ${covidData.Confirmed}, Deaths: ${covidData.Deaths}, Recovered: ${covidData.Recovered}, Active: ${covidData.Active} `)
    }
  }
});

Any help on how I should rearrange my code to look more like the embed above would be much appreciated.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
340 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can do this using the MessageEmbed class.

Here an example of it being used:

const embed = new Discord.MessageEmbed()
    .setColor("#0099ff")
    .setTitle("A title")
    .setDescription("A description")
    .setTimestamp()
message.channel.send(embed);

// You can also use the code below, in your case
msg.reply(embed);

You can find more examples of this here

If you want to create something like ProDyno has, you'll want to use the .addLine method on the MessageEmbed class, this will allow you to toggle things like inline to be true so you can put stats next to each other. For example:

.addFields(
    { name: 'Inline title', value: 'Inline text', inline: true },
    { name: 'Inline title', value: 'Inline text', inline: true },
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...