@client.command(aliases=['memes'])
async def meme(ctx):
embed = discord.Embed(title="Meme", description=None)
async with aiohttp.ClientSession() as cs:
async with cs.get('https://www.reddit.com/r/wholesomememes/new.json?sort=hot') as r:
res = await r.json()
embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
await ctx.send(embed=embed, content=None)
我真的很想知道它是如何工作的,所以我从stackoverflow问题中找到了这段代码。我觉得这可以增进我对discord.py的理解: 因此,如果您不介意并且没有时间和知识,请回答! :D
答案 0 :(得分:0)
所有这些操作就是将GET请求发送到给定的url,该URL本身返回JSON数据。如果通过任何浏览器发送请求,您将看到您以JSON格式接收数据。例如,如果您使用Firefox,它将自动为您提供将其作为JSON对象进行检查的选项。为了更好地理解,您可以单击“键”下的
['data']['children'] [random.randint(0, 25)]['data']['url']
因此,您进入数据->子级->选择一个随机数->数据->网址 您将看到最终结果是一个URL字符串,该字符串将您定向到一个模因图片。
代码段的最后一步是,它将您需要的网址发送到某个地方(我猜是在不和谐的聊天中)