DEV Community

Ty
Ty

Posted on

Using API with Discord Bots

Welcome back to my weekly discord bot update. This time, I'll be using MyAnimeList api to write a cool search function with our discord bot!

Unfortunately, there is no real api for myanimelist yet. But there are current plans for an early official release. Our discord bot will be making use of Jikan which you can find the documentation here.

Taking a look at this unofficial api, there has been already implementation for nodejs for this api!Thank you to xy137 for creating the github and making it easy for us to use Jikan!
NodeJS Github.

Installation of this api is easy as pie! First we would want to install the packages with

npm install jikan-node --save

And you're done! In order to use the api you'll only need to add two lines of code which are

const Jikan = require('jikan-node');
const mal = new Jikan();

We're just calling jikan through mal and wanting to use their search anime function.

Here is a basic fetch in order to grab an anime I want to search up.

mal.search("anime", args, "page")
.then(info => console.log(info)

With this one function, we're able to search up an anime with what the user typed.

I'll be breaking down the function! mal is Jikan and we would want to use the search function. anime is the a string that we want the type for. This can be interchangeable with people or manga as well. args is what the user searched for, and page is the result we would want about the anime.

This would bring us a result of of what we had search for. Since this is going to bring us an array of animes. You can look at your console.log and see what would you want to do with the results you have received.

Since I just want to find the first result of an anime and put it into a embed message, my code looks like

mal.search("anime", args, "page")
.then(info => {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle(`${info.results[0].title}`)
.setURL(`${info.results[0].url}`)
.setAuthor('Chulainn')
.setDescription(`${info.results[0].synopsis}`)
.addFields(
{ name: 'Rating', value: `${info.results[0].score}` },
)
.setImage(`${info.results[0].image_url}`)
.setTimestamp()
.setFooter('Brought to you by Chulain');
message.channel.send(exampleEmbed)
})
.catch(err => console.log(err))

My output looks like this!

Top comments (0)