Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  6] [ 4]  / answers: 1 / hits: 13075  / 4 Years ago, tue, february 18, 2020, 12:00:00

Currently my code reads out as



if(message.content.startsWith(prefix + 'readback')) {

fs.readFile('msgs.json', (err, data) => {
if (err) throw err;
let _msgs = JSON.parse(data);
message.channel.send(_msgs);
console.log(_msgs)
});
}


with all appropriate constants and variables



While using prefix + 'readback, the Discord Bot displays _msgs as [object Object] - to make sure it wasn't an issue with the code, I added console.log(_msgs), and the correct JSON file content displays in the console. Is there a way to get the Bot to display the contents of the JSON file in a channel?


More From » node.js

 Answers
3

You can take advantage of discord markdown.


Code block, as on Stack Overflow, can be used to format code and even add color:


```json  
{
"some": "json"
}
```

will render like this:

enter


Now, as stated by Xander Kyle in comment, you want to use JSON.stringify as message.channel.send use toString to convert variable to string and it doesn't work "as you want" for objects.


fs.readFile('msgs.json', (err, data) => {
if (err) { throw err; }
const _msgs = JSON.stringify(JSON.parse(data), null, 2);
message.channel.send('```jsonn' + _msgs + 'n```');
console.log(_msgs)
});

The 2 in stringify make the json indented (documentation)


[#4717] Thursday, February 13, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
;