【问题标题】:Random Cat Generator Using API discord.py使用 API discord.py 的随机猫生成器
【发布时间】:2021-11-01 12:43:54
【问题描述】:

我正在尝试使用 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'

如何让嵌入与图像一起发送?

【问题讨论】:

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


    【解决方案1】:

    以防万一您感兴趣,这就是我将其添加到我的机器人的方式。

    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

    【讨论】:

      【解决方案2】:

      在方法参数中,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))
      

      【讨论】:

        猜你喜欢
        • 2014-05-21
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 2019-10-26
        • 2020-11-29
        相关资源
        最近更新 更多