Discord.js – PokeAPI integration with bot

Discord.js – PokeAPI integration with bot

I came across a funny little piece of code that integrates a HUGE database of all the different Pokémons. You can make API calls and get back data from the site in JSON format. I don’t remember where I found the snippets, but I managed to get it working and now have a command called pokemon <search_name>.

As always, this article assumes you’re using dynamic commands from discordjs.guide, and have each command in separate files.


const fetch = require('node-fetch');
const API_URL = 'https://pokeapi.co/api/v2/pokemon';
    async execute(message, args) {
        const pokemon = message.content.toLowerCase().split(" ")[1];
        try {
            const pokeData = await getPokemon(pokemon);
            const {
            } = pokeData;
            const embed = new Discord.MessageEmbed();
            embed.setTitle(`${name} #${id}`);
            if (sprites.front_default) {
            stats.forEach(stat => embed.addField(stat.stat.name, stat.base_stat, true));
            types.forEach(type => embed.addField('Type', type.type.name, true));
            embed.addField('Weight', weight);
            embed.addField('Base Experience', base_experience);
            if (sprites.other['official-artwork'].front_default) {
            await message.channel.send(embed);
            guildID = message.guild.id;
            const conf = await config.findOne({
                where: {
                    guildId: guildID
            let dato = new Date();
            dato.setHours(dato.getHours() + 2);
            if (conf) {
                logchannel = conf.logchannel;
                message.client.channels.cache.get(logchannel).send(`${message.author} asked for info on Pokèmon: ${name} #${id}! ` + dato.toLocaleTimeString() + dato.toLocaleDateString());
        catch (err) {
            message.channel.send(`Pokemon ${pokemon} #${id} does not exist.`);

async function getPokemon(pokemon) {
    let response = await fetch(`${API_URL}/${pokemon}`);
    return await response.json();

The basics of this code is the API_URL and pokemon, which is taken from args.
I also adhere to my logging, and have configured a log- and welcomechannel in my database. Each guild my bot is inn can use, or not, these features.

If you have questions about this code or anything else I’ve posted, please join my Discord server and ask in a channel there.

Leave a Reply

Your email address will not be published. Required fields are marked *