Node-fetch export

Hi! I am using the twitch api along with discord.js to make a command that gets results from the twitch api and sends them in a message (this is probably a bad idea but its not a big bot anyways :P). This works great (I use a ```json codeblock), except when I am using, say, https://api.twitch.tv/helix/search/categories?query=something, and the result is over 2000 characters and I get a big nasty error in terminal. I was wondering if you could somehow (preferably without using fs but if thats not possible it’s already imported) that I could export the raw JSON file and send that in a message (message.channel.send({files: res.json or whatever its called}))

My current code is this:

fetch('https://id.twitch.tv/oauth2/token?client_id=' + process.env.clientId + "&client_secret=" + process.env.clientSecret + "&grant_type=client_credentials", {
    method: 'POST',
})
 .then(res => {
   res.json()
   message.reply({files: res.json})
 })
            .then(res => {
            
                    var token = res.access_token
                    try {
                    fetch(url + game, {
                            method: 'GET',
                            headers: {
                                'Client-ID': process.env.clientId,
                                'Authorization': 'Bearer ' + token, 

                            }
                        })
                        .then(res => res.json())
                        .then(res => {
                         var result = JSON.stringify(res, null, 4)
                          if(result.length > 2000){
                           //export the file
                          } else {
                          message.channel.send("```json\n" + result + "\n```")

                          })

Any help is appreciated

Sure.

But your problem is a discord problem.

Not a TwitchAPI problem.

You need to lookup what the Discord library you are using supports for uploading files.

That’ll tell you if you need to use fs or not (it’ll probably support fs or a buffer but that’s discord library problem). personally I’d probably write it to a file with FS. Then call that file and upload via whatever discord library I’m using.

Edit: A quick check suggests that Discord.JS would do something like

message.channel.send("Testing message.", { files: ["./images/headpat1.png"] });

So you’d need to have written the file to disc first (not tested)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.