Reputation: 254
I'm attempting to generate a random cat image and put it into an embed using discord.py, but I'm getting an error. Here's my code:
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))
And I'm getting this error:
Command raised an exception: AttributeError: 'Image' object has no attribute 'send'
How do I get the embed to send with the image?
Upvotes: 0
Views: 4913
Reputation: 149
Just in case you are interested this is how I added it to my bot.
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()
I am using the random API
Upvotes: 1
Reputation: 987
In method arguments, self
must goes first.
self is defining object itself: you're calling Image object as ctx name.
Solution:
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))
Upvotes: 0