使用API​​ discord.py的随机Cat生成器

时间:2020-11-10 13:46:34

标签: python discord discord.py discord.py-rewrite

我正在尝试生成随机的猫图像,并使用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'

如何获取嵌入图片?

1 个答案:

答案 0 :(得分:0)

在方法参数中,self必须先行。 self定义对象本身:您将Image对象称为ctx名称。

解决方案:

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(self, ctx):
      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))