【问题标题】:Discord.py google images searchDiscord.py 谷歌图片搜索
【发布时间】:2020-07-18 02:05:41
【问题描述】:

我目前正在尝试制作一个不和谐机器人,它将根据特定命令从谷歌图像发送图像,所以如果我说/dog 它会从谷歌图像中随机发布一张狗的照片。我是创建不和谐机器人的新手,我打算用 Python 制作它。如果有人帮助我提供示例代码,让我了解如何从谷歌图片中获取图片,我将不胜感激。

【问题讨论】:

    标签: python python-3.x bots discord discord.py


    【解决方案1】:

    您可以使用 aiohttp 客户端会话通过 some-random-api 完成此操作

    对于示例代码,它应该看起来像这样

        @commands.command(name='dog')
        async def random_dog(self, ctx):
            async with aiohttp.ClientSession() as cs:
                async with cs.get("https://random.dog/woof.json") as r:
                    
                    data = await r.json()
                    embed = discord.Embed(
                        title="Doggo",
                        color = ctx.author.color
                    )
                    embed.set_image(url=data['url'])
                    await ctx.send(embed=embed)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多