Splode
Splode

Reputation: 43

I tried making a random quote command, but it does not work?

So, I'm trying to make a command where it will give a random anime quote, here's the code:

@client.command()
async def animequote(ctx):
  response = requests.get("https://some-random-api.ml/animu/quote")
  data = response.json()
  jsontxt = json.loads(data)
  quote = jsontxt["sentence"]
  char = jsontxt["characther"]
  anime = jsontxt["anime"]
  embed = discord.Embed(title="Quote", description=f"{quote} - {char} from {anime}")
  await ctx.send(embed=embed)

When I run the code and try out the command, it gives me this error:

Ignoring exception in command animequote:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 130, in animequote
    jsontxt = json.loads(data)
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: TypeError: the JSON object must be str, bytes or bytearray, not dict

Does anyone know what this means or how I can fix this?

Upvotes: 0

Views: 108

Answers (1)

Ali Hakan Kurt
Ali Hakan Kurt

Reputation: 1037

You are trying to load dict object from dict. So you need to remove json.loads(data) from your code.

@client.command()
async def animequote(ctx):
    response = requests.get("https://some-random-api.ml/animu/quote")
    data = response.json()
    quote = data["sentence"]
    char = data["characther"]
    anime = data["anime"]
    embed = discord.Embed(
        title = "Quote",
        description = f"{quote} - {char} from {anime}"
    )
    await ctx.send(embed=embed)

Upvotes: 2

Related Questions