discord.py project 3: Random Dog Pics! 🐕
Tags: discord, bot, discordpy, python
Published: 2021-03-31 15:45:20

In this article, you're going to learn how to make a Discord bot that can:
- Send a picture of a dog in an embed
- Send a dog fact in an embed using footers

By the end of this post, you will know how to:
- Make REST API requests
- Parse JSON data
- Use the `footer` and `image` fields of an embed.

Wherever the command is used, it will send an embed similar to this:
![Picture of embed](https://mikey.has-no-bra.in/wBmMCj.png)

To start, we need to initialize our bot. This time, we're back using commands.Bot since we are going to have one function, our dog command.

```py
import discord
from discord.ext import commands

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
   print("Ready")

client.run('token')
```
So far, we have a bot with the prefix `!`. When it's ready, it will print `Ready` in the console!

___
Let's make the dog command now! Let's start by importing the modules we will need: `aiohttp` and `json`.
> *Note: type `pip install aiohttp` before running the code!*
```py
import discord
from discord.ext import commands
import aiohttp

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
   print("Ready")

client.run('token')
```

Now create the command:
```py
import discord
from discord.ext import commands
import aiohttp

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
   print("Ready")

@client.command()
async def dog(ctx):
   async with aiohttp.ClientSession() as session:
      request = await session.get('https://some-random-api.ml/img/dog')
      dogjson = await request.json()

client.run('token')
```
Now, `dogjson` will be a variable containing a **dictionary**, which is a list of aliases.

{% details What's a dictionary? %}
An example of where you would use a dictionary is in a substitution cipher. You might be making an a1z26 (where `a` becomes `1`, `b` becomes `2`, etc.). So, you'd make a dictionary to make it easier:
```py
azdict = {'a':'1', 'b':'2', 'c':'3'}
```

Now, you could use this to substitute:
```py
my_string = "abc abc"
for index, letter in enumerate(my_string): # Iterate through each letter
   if letter in azdict.keys(): # iterate through the key and not the value (e.g. a, b, c, and so on)
      my_string[index] = azdict[letter]
print(my_string)

>> '123 123'
```

___
{% enddetails %}

Knowing that the dictionary is structured like this:
![](https://mikey.has-no-bra.in/P3ty35.png)
we know that we want to use the `link` key.

Now, we can 
```py
import discord
from discord.ext import commands
import aiohttp

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
   print("Ready")

@client.command()
async def dog(ctx):
   async with aiohttp.ClientSession() as session:
      request = await session.get('https://some-random-api.ml/img/dog') # Make a request
      dogjson = await request.json() # Convert it to a JSON dictionary
   embed = discord.Embed(title="Doggo!", color=discord.Color.purple()) # Create embed
   embed.set_image(url=dogjson['link']) # Set the embed image to the value of the 'link' key
   await ctx.send(embed=embed) # Send the embed

client.run('token')
```

Now, when we use the bot command, we will get this output:
![](https://mikey.has-no-bra.in/CzaHtD.png)

___

Now, let's have it send a dog fact as well! SomeRandomAPI also has a dog facts endpoint, which will allow us to get a random fact about dogs!

```py
@client.command()
async def dog(ctx):
   async with aiohttp.ClientSession() as session:
      request = await session.get('https://some-random-api.ml/img/dog')
      dogjson = await request.json()
      # This time we'll get the fact request as well!
      request2 = await session.get('https://some-random-api.ml/facts/dog')
      factjson = await request2.json()

   embed = discord.Embed(title="Doggo!", color=discord.Color.purple())
   embed.set_image(url=dogjson['link'])
   embed.set_footer(text=factjson['fact'])
   await ctx.send(embed=embed)
```
This will produce the following!
![](https://mikey.has-no-bra.in/2lT6mC.png)

___

#### Have Questions? Have a suggestion about what to do for the next post?
Tell me in the comments and I'll reply!