簡體   English   中英

如何為我的 Discord 機器人提供的統計信息創建嵌入(Covid Statistics)

[英]How to create an embed for statistical information provided by my Discord bot (Covid Statistics)

我創建了一個 Discord 機器人,它將根據命令為我提供任何國家/地區的 Covid 統計數據,但是它僅以原始文本顯示給我,我見過這樣的嵌入圖像:

在此處輸入圖像描述

我有興趣在我的機器人回復中像這樣顯示我的數據,這是我正在使用的代碼:

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} `)
    }
  }
});

任何關於我應該如何重新排列我的代碼看起來更像上面嵌入的幫助將不勝感激。

謝謝!

您可以使用MessageEmbed class 執行此操作。

這是一個使用它的例子:

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);

您可以在此處找到更多示例

如果你想創建像 ProDyno 一樣的東西,你需要在 MessageEmbed class 上使用.addLine方法,這將允許你將inline之類的東西切換為true ,這樣你就可以將統計信息放在一起。 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM