Creating an embed for API data in a Discord bot

I’m working on a Discord bot and I’ve got an API response working. Now I want to make it look nice by putting it in an embed when it’s sent to Discord. Here’s what I’ve got so far:

const apiStuff = require('../utils/SomeAPI')
const api = new apiStuff()

module.exports = {
  group: 'PlayerInfo',
  info: 'Gets player info from GameDB.',
  params: '<player name>',
  minParams: 1,
  maxParams: 1,
  action: async ({chat, params}) => {
    const playerName = params[0]
    
    const playerInfo = await api.getPlayerInfo(playerName)
    if(!playerInfo || playerInfo.data == []) return 'Player not found'
    return JSON.stringify(playerInfo.data[0], null, 2)
  }
}

How can I turn this basic output into a nice embed? I’m pretty new to this stuff, so sorry if it’s a simple fix. Any help would be great!

hey Sky24, i’ve messed with embeds before. here’s a quick way to do it:

import EmbedBuilder from discord.js
then in ur action function:

const embed = new EmbedBuilder()
.setTitle(${playerName} Info)
.setColor(‘#00FF00’)
.addFields(
{ name: ‘Level’, value: playerInfo.data[0].level },
{ name: ‘XP’, value: playerInfo.data[0].xp }
)

return { embeds: [embed] }

adjust fields as needed. gl with ur bot!

I’ve been in your shoes before, and creating embeds can be a bit tricky at first. Here’s how I solved it:

First, import the Discord.js library at the top of your file:

const { EmbedBuilder } = require('discord.js');

Then, in your action function, replace the return statement with something like this:

const embed = new EmbedBuilder()
    .setColor('#0099ff')
    .setTitle(`Player Info: ${playerName}`)
    .addFields(
        { name: 'Level', value: playerInfo.data[0].level.toString() },
        { name: 'Rank', value: playerInfo.data[0].rank },
        { name: 'Total Score', value: playerInfo.data[0].totalScore.toString() }
    )
    .setTimestamp();

return { embeds: [embed] };

This creates a basic embed with some fields. You’ll need to adjust the field names and values based on what’s actually in your playerInfo.data[0] object. Also, make sure your bot has the necessary permissions to send embeds in the channel.

Hope this helps! Let me know if you need any clarification.

To create an embed for your Discord bot, you’ll need to utilize the EmbedBuilder class from discord.js. Here’s how you can modify your existing code:

Import EmbedBuilder at the top of your file:
const { EmbedBuilder } = require(‘discord.js’);

In your action function, replace the return statement with the following:

const embed = new EmbedBuilder()
.setColor(‘#FF5733’)
.setTitle(${playerName}'s Info)
.setDescription(‘Player data from GameDB’)
.addFields(
Object.entries(playerInfo.data[0]).map(([key, value]) => ({
name: key,
value: String(value),
inline: true
}))
)
.setFooter({ text: ‘Data retrieved from GameDB API’ })
.setTimestamp();

return { embeds: [embed] };

This will create a dynamic embed with all fields from your API response. Adjust colors and titles as needed. Remember to handle potential errors and edge cases in your implementation.