DEV Community

loading...

discord.py project 3: Random Dog Pics! πŸ•

mikeywastaken profile image Mikey ・Updated on ・3 min read

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

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.

import discord
from discord.ext import commands

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

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

client.run('token')
Enter fullscreen mode Exit fullscreen mode

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!

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')
Enter fullscreen mode Exit fullscreen mode

Now create the command:

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')
Enter fullscreen mode Exit fullscreen mode

Now, dogjson will be a variable containing a dictionary, which is a list of aliases.

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:
azdict = {'a':'1', 'b':'2', 'c':'3'}
Enter fullscreen mode Exit fullscreen mode

Now, you could use this to substitute:

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'
Enter fullscreen mode Exit fullscreen mode

Knowing that the dictionary is structured like this:

we know that we want to use the link key.

Now, we can

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')
Enter fullscreen mode Exit fullscreen mode

Now, when we use the bot command, we will get this output:


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!

@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)
Enter fullscreen mode Exit fullscreen mode

This will produce the following!


Have Questions? Have a suggestion about what to do for the next post?

Tell me in the comments and I'll reply!

Discussion (3)

pic
Editor guide
Collapse
sarmqn profile image
Sarmqn

This is awesome man!
Has helped me a lot thanks

Collapse
mikeywastaken profile image
Mikey Author

No Problem! Do you have any suggestions for the next article

Collapse
sarmqn profile image
Sarmqn

How about a command that shows users information, like when they joined discord, joining the server, what they are doing etc..