@commands.command(aliases=['gt'])
async def cat(self, ctx):
"""Outputs image from r/greentext"""
async with ctx.typing():
async with aiohttp.ClientSession() as cs:
async with cs.get("https://www.reddit.com/r/greentext/hot/.json") as r:
data = await r.json()
embed = discord.Embed(title = "r/greentext", color = 0xFF0000)
embed.set_image(url = data["url"])
embed.set_footer(text = "r/greentext")
await ctx.send(embed = embed)
I know that data["url"]should be correct as that's what the image file is saved as on the website as seen in this screenshot: https://i.sstatic.net/EZB3U.jpg the whole website json is here: https://www.reddit.com/r/greentext/hot/.json and if anyone can help me, I can't find an aiohttp help server and the discord.py server doesn't help me at all because they all make you feel stupid for wanting help
There is no url
key at the top level of the reddit response; the images you are referring to are preview images and these are per post so you need to iterate through posts and extract the images:
data = await r.json()
for post in data["data"]["children"]:
images = post.get("preview", {}).get("images", [])
if not images:
print("no preview images for %s..." % post["data"]["title"])
continue
image = images[0] # grab the first image
embed = discord.Embed(title = "r/greentext", color = 0xFF0000)
embed.set_image(url = image["source"]["url"])
embed.set_footer(text = "r/greentext")
In order to get more accustomed to responses returned by reddit you can open the response in a JSON viewer and analyze them.