python - 使用 API discord.py 的随机猫生成器
问题描述
我正在尝试生成一个随机猫图像并使用 discord.py 将其放入嵌入中,但出现错误。这是我的代码:
from aiohttp import ClientSession
import discord
import requests
from discord.ext import commands
import random
import os
import keep_alive
import asyncio
import json
import io
import contextlib
import datetime
async def get(session: object, url: object) -> object:
async with session.get(url) as response:
return await response.text()
class Image(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def cat(ctx, self):
response = requests.get('https://aws.random.cat/meow')
data = response.json()
embed = discord.Embed(
title = 'Kitty Cat ',
description = 'Cats :star_struck:',
colour = discord.Colour.purple()
)
embed.set_image(url=data['file'])
embed.set_footer(text="")
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Image(bot))
我收到了这个错误:
Command raised an exception: AttributeError: 'Image' object has no attribute 'send'
如何让嵌入与图像一起发送?
解决方案
以防万一您感兴趣,这就是我将其添加到我的机器人的方式。
from typing import Text
import discord
import aiohttp
@client.command(help = "It shows you a cat photo as well as a fact")
async def cat(ctx):
async with aiohttp.ClientSession() as session:
request = await session.get('https://some-random-api.ml/img/cat')
dogjson = await request.json()
# This time we'll get the fact request as well!
request2 = await session.get('https://some-random-api.ml/facts/cat')
factjson = await request2.json()
我正在使用随机 API