Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

discord - Removing parts of a quote pulled from an API python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

from the error, q is a dictionary. A value is retrieved from a dictionary by specifying its corresponding key in square brackets, not with a dot. so, to get the quote's content, you would have to do this: q['content'], not q.content. so this would be the ideal code:

@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']}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...