Skip to content
Advertisement

How do I use an API in Discord.js?

I was looking for a cool way I could automate some interesting news articles on my discord server. I wanted to use webhooks at first but then decided to go with APIs. I looked all around and saw that I should go with New York Times API but when I went to code it, it came up with a few errors.

JavaScript

This is my code, I know this is probably riddled with mistakes but I am just starting out with node.js I looked at the example from the discord.js website and took some stuff from there. I don’t know what I should do and if you could explain it out a little to help me learn that would be great. I keep getting the Unhandled Rejection Promise Warning and the Cannot send an empty message errors. I am using Visual Studio Code.

Advertisement

Answer

You can use the async/await to get the results array from the API response, then send the details in embeds. You can either send the first article, a random article or more than one articles.

The following should work, it sends the first three articles:

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement