Code a Pokémon Discord bot

In this post I will be going through the steps to create a Discord bot that returns some information about Pokemon trading cards. This is more of a building blocks approach so that you can then take the implementation further and make something cool!

If you’re more of a visual learner I’ve created a Youtube video of me going through the coding, you can come back here for extra information or to copy the code!

Setting up the bot

First step is actually setting up the bot, this is a pretty simple process and I cover this in a previous blog https://codeheir.com/2020/07/25/how-to-code-a-discord-bot/ it shouldn’t take you long to get up to speed!

The api we’re using

The API is https://pokemontcg.io/ a really simple to use endpoint that requires no keys or fluff, simply just call it and get the data:

There are also a tonne of wrappers for the API that can be found here on their GitHub – I don’t use them in my example, I think it’s pretty simple as it is!

Our plan

The idea for our bot is that you type in a command in discord !search {pokemonName} which will return some information about the different subtypes of that Pokémon, from that we’ll grab the ID for our next command !search {ID}. This command will return us some information about that specific Pokémon.

Calling the API – !search

Let’s create a function that takes in a name as an argument and calls the API with that name:

async function getPokemons(name) {
    try {
        const result = await axios.get(`https://api.pokemontcg.io/v1/cards?name=${name}`)
        const cards = result.data.cards;
        let output = '';
        for (let card of cards) {
            output += `${card.name} - ${card.id}\n`;
        }
        return output;
    } catch (err) {
        console.log(err);
    }
}

So if we call the function with a Pokemon name, the output should look like the following:

Ambipom - bw6-100
Ambipom - dp5-35
Ambipom - dp2-3
Ambipom - hgss4-13
Ambipom - xy11-91
Ambipom - sm12-170
Ambipom G - pl2-56

Where the second word in each line represents the ID of the pokemon in that particular subset.

Creating the Discord command

When a person sends a message that begins with !search we want to grab the second message they sent – the name of the Pokemon. Then we want to call our getPokemons function:

client.on('message', async msg => {
    if (msg.content.startsWith('!search')) {
        // !search ambipom
        let name = msg.content.split(' ')[1];
        let output = await getPokemons(name);
        msg.reply(output);
    }
});

Which should work like the following:

Creating the !show command

Next up we want to get a specific pokemon by its ID this is pretty much the same procedure as above:

async function showPokemon(id) {
    try {
        const result = await axios.get(`https://api.pokemontcg.io/v1/cards?id=${id}`)
        const card = result.data.cards[0];
        let output = '';
        output += `${card.name} - ${card.subtype}\n`;
        output += `${card.imageUrl}`;
        return output;
    } catch (err) {
        console.log(err);
    }
}

So we’re searching for the pokemon using the ID rather than the name, then we’re creating an output that consists of the name, the subtype and the imageUrl. There is a tonne of information that gets sent back to us at this point, it’s up to you what you display, I just chose these 3 properties for simplicity.

Updating the message handler

So when a user messages in the discord chat we also want to be looking for the keyword !show and then calling our showPokemon function

client.on('message', async msg => {
    if (msg.content.startsWith('!search')) {
        // !search ambipom
        let command = msg.content.split(' ')[1];
        let output = await getPokemons(command);
        msg.reply(output);
    } else if (msg.content.startsWith('!show')) {
        // !show pl2-56
        let command = msg.content.split(' ')[1];
        let output = await showPokemon(command);
        msg.reply(output);
    }
});

And the final flow of your discord bot should look like this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s