I'm working on a discord bot and I decided to make a quote command, I'm using this quote API: https://github.com/lukePeavey/quotable - and this is the link that the bot accesses: https://api.quotable.io/random.
the bot successfully sends a quote, here's an example of what it sent:
{'_id': 'O_jlFdjUtHPT', 'tags': ['famous-quotes'], 'content': 'Every person, all the events of your life are there because you have drawn them there. What you choose to do with them is up to you.', 'author': 'Richard Bach', 'length': 132}
the problem is, I can't figure out how I would make it so it just includes the quote and the author.
this is the code I have that sends what's above.
@commands.command()
async def quote(self, ctx):
"""fetches a random quote."""
async with aiohttp.ClientSession() as session:
async with session.get('https://api.quotable.io/random') as q:
if q.status == 200:
js = await q.json()
await ctx.send(js)
I tried changing it to:
@commands.command()
async def quote(self, ctx):
"""fetches a random quote."""
async with aiohttp.ClientSession() as session:
async with session.get('https://api.quotable.io/random') as q:
if q.status == 200:
js = await q.json()
await ctx.send(f'> {js.content}
- {js.author}')
but that just returns the error:
gnoring exception in command quote:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/Users/Goldilocks/Desktop/CodeStuff/b1nzyBotRepo/cogs/bettersimple.py", line 108, in quote
await ctx.send(f'{js.content}
-{js.author}')
AttributeError: 'dict' object has no attribute 'content'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'dict' object has no attribute 'content'
what am I doing wrong here and what should I be changing to fix it?
question from:
https://stackoverflow.com/questions/65832171/removing-parts-of-a-quote-pulled-from-an-api-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…