DEV Community

Cover image for Discord.py commands for cat image with breed information
Hazrat Ummar Shaikh
Hazrat Ummar Shaikh

Posted on

Discord.py commands for cat image with breed information

@commands.hybrid_command(name="cat", description = "Get a random cat image with description")
    @commands.cooldown(4, 20, commands.BucketType.guild)
    async def cat(self, ctx: commands.Context, user: discord.Member = None):
        api_key = os.getenv("CAT_API_KEY")
        url = "https://api.thecatapi.com/v1/images/search?has_breeds=1"
        reponse = requests.get(url, headers={'x-api-key': f"{api_key}"})

        data = reponse.json()

        breed = [breed['breeds'] for breed in data]
        image = [image['url'] for image in data]
        cat = breed[0][0]
        cat_name = cat['name']
        cat_description = cat['description']
        cat_temperament = cat['temperament']

        embed=discord.Embed(title=f"{cat_name}", description=f">>> {cat_description}", color=0x00FFFF)
        embed.add_field(name="Temperament", value=f">>> {cat_temperament}", inline=False)
        embed.set_image(url=f"{image[0]}")

        if user:
            await ctx.send(user.mention,embed=embed)
        else:
            await ctx.send(embed=embed)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)