How to post the json data in a embed in discord.js?

by haylee.mertz , in category: Javascript , 3 months ago

How to post the json data in a embed in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 3 months ago

@haylee.mertz 

To post JSON data in an embed in Discord.js, you can create a new RichEmbed object and add the JSON data as fields to the embed. Here is an example code snippet to achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', message => {
  if (message.content === '!json') {
    // Sample JSON data
    const jsonData = {
      "name": "John Doe",
      "age": 25,
      "gender": "male"
    };

    const embed = new Discord.MessageEmbed()
      .setTitle('JSON Data')
      .setColor('#0099ff')
      .addField('Name', jsonData.name)
      .addField('Age', jsonData.age)
      .addField('Gender', jsonData.gender);

    message.channel.send(embed);
  }
});

client.login('YOUR_BOT_TOKEN');


In this example, when a user sends the command !json in the chat, the bot will create a new embed with the JSON data and send it in the same channel. You can customize the embed further by adding more fields or formatting it to your liking.