How to display content of a .json file using discord.js?

by wilmer.lemke , in category: Javascript , 9 days ago

How to display content of a .json file using discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 8 days ago

@wilmer.lemke 

To display the content of a .json file using Discord.js, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
const { Client, Intents } = require('discord.js');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.once('ready', () => {
    console.log('Bot is ready');
    
    // Read the content of the .json file
    const data = fs.readFileSync('data.json');
    
    // Parse the JSON data
    const jsonData = JSON.parse(data);
    
    // Display the content in a Discord channel
    const channel = client.channels.cache.get('channelID'); // Replace 'channelID' with the ID of the channel you want to send the message to
    channel.send(JSON.stringify(jsonData, null, 2)); // Use JSON.stringify to format the JSON data
});

client.login('YOUR_BOT_TOKEN');


Make sure to replace 'data.json' with the path to your .json file and 'channelID' with the ID of the channel where you want to display the content. This code will read the content of the .json file, parse it, and send it as a message in the specified Discord channel.