1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/royalpack/commands/dog.py

33 lines
1.1 KiB
Python
Raw Normal View History

2020-05-19 16:46:49 +00:00
from typing import *
import royalnet.commands as rc
import aiohttp
import io
2020-07-18 14:29:43 +00:00
class DogCommand(rc.Command):
name: str = "dog"
2020-05-19 16:46:49 +00:00
2020-07-18 14:29:43 +00:00
description: str = "Invia un cane della razza specificata in chat."
2020-05-19 16:46:49 +00:00
2020-07-18 14:29:43 +00:00
syntax: str = "[razza]"
2020-05-19 16:46:49 +00:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
2020-07-18 14:29:43 +00:00
breed = args.joined()
if breed:
url = f"https://dog.ceo/api/breed/{breed}/images/random"
else:
url = f"https://dog.ceo/api/breeds/image/random"
2020-05-19 16:46:49 +00:00
async with aiohttp.ClientSession() as session:
2020-07-18 14:29:43 +00:00
async with session.get(url) as response:
if response.status >= 400:
raise rc.ExternalError(f"Request returned {response.status}")
2020-05-19 16:46:49 +00:00
result = await response.json()
assert "status" in result
assert result["status"] == "success"
assert "message" in result
url = result["message"]
async with session.get(url) as response:
img = await response.content.read()
await data.reply_image(image=io.BytesIO(img))