68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
import discord
|
|
from discord.ext import commands
|
|
import requests
|
|
import aiohttp
|
|
|
|
import random
|
|
import asyncio
|
|
|
|
durls = {"random": "https://dog.ceo/api/breeds/image/random",
|
|
"list": "https://dog.ceo/api/breeds/list",
|
|
"breed": "https://dog.ceo/api/breed/{}/images/random"}
|
|
|
|
|
|
class wp:
|
|
@staticmethod
|
|
async def jsondecoder(url, req):
|
|
req = req.replace(' ', '%20')
|
|
url = url.format(req)
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(url) as r:
|
|
if r.status == 200:
|
|
return await r.json()
|
|
|
|
|
|
class dogs:
|
|
def __init__(self, bot, breeds):
|
|
self.bot = bot
|
|
self.bot.breeds = breeds
|
|
|
|
@commands.command()
|
|
async def pug(self):
|
|
'''generates a random pug'''
|
|
em = discord.Embed(color=discord.Color(value=random.randint(000000,
|
|
999999)))
|
|
p = await wp.jsondecoder(durls["breed"], "pug")
|
|
em.set_image(url=p["message"])
|
|
await self.bot.say(embed=em)
|
|
|
|
@commands.command()
|
|
async def dog(self, breed=None):
|
|
"""generates a random picture of a dog use:
|
|
'&dog' to see a random dog of any bread,
|
|
'&dog list' will give u a list of callable breads and finally
|
|
'&dog [breedname]' will give u a dog of the selected bread"""
|
|
if breed is None:
|
|
color = discord.Color(value=random.randint(000000, 999999))
|
|
em = discord.Embed(color=color)
|
|
u = await wp.jsondecoder(durls["random"], "")
|
|
em.set_image(url=u["message"])
|
|
await self.bot.say(embed=em)
|
|
elif breed == "list":
|
|
await self.bot.say("**Here is a list of the valid breeds:**")
|
|
await self.bot.say(self.bot.breeds)
|
|
else:
|
|
if breed not in self.bot.breeds:
|
|
await self.bot.say("Learn to spell noob!")
|
|
else:
|
|
color = discord.Color(value=random.randint(000000, 999999))
|
|
em = discord.Embed(color=color)
|
|
d = await wp.jsondecoder(durls["breed"], breed)
|
|
em.set_image(url=d["message"])
|
|
await self.bot.say(embed=em)
|
|
|
|
|
|
def setup(bot):
|
|
breeds = requests.get(url=durls['list']).json()
|
|
bot.add_cog(dogs(bot, breeds["message"]))
|