0
votes
@bot.command()
async def catto(ctx):
  choice = ["1","2"]
  print(random.choice(choice))
  if choice == 1:
    r = requests.get("APILINK").json()
    cat_embed = discord.Embed()
    cat_embed.set_image(url=r["url"])
    await ctx.send(embed=cat_embed)
  else:
    file = discord.File("FILEPATH")
    file2 = discord.File("FILEPATH")
    cats = (file, file2)
    await ctx.send(embed = random.choice(cats))

It's in the else bit Im having problems where it gives me the error as said in the title.

Full Traceback as suggested:

Ignoring exception in command catto:

Traceback (most recent call last): File "C:\Users\OneDrive\Documents\Programming-Extra\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "c:\Users\OneDrive\Documents\Programming-Extra\Discord Bot\BOTNAME", line 46, in catto await ctx.send(embed = random.choice(cats)) File "C:\Users\OneDrive\Documents\Programming-Extra\lib\site-packages\discord\abc.py", line 1017, in send embed = embed.to_dict() AttributeError: 'File' object has no attribute 'to_dict'

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

Traceback (most recent call last): File "C:\Users\OneDrive\Documents\Programming-Extra\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke await ctx.command.invoke(ctx) File "C:\Users\OneDrive\Documents\Programming-Extra\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\OneDrive\Documents\Programming-Extra\lib\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: 'File' object has no attribute 'to_dict

1
Can you display the full traceback?Daniyal Warraich
The last line should be await ctx.send(file=random.choice(cats)), try it out...Ghost Ops
you are using to_dict() attribute to a file instead of embed and its not in your above mentioned codeGhost Ops

1 Answers

0
votes

Attention!



Txt "to_dict"

>>> import json
>>> File = open(r"C:\\Path\to\file.json", 'r')
>>> json.load(File)
{"Key":"Value"}
>>> File.read()
'{'Key':'Value'}'

json.load(File) will return a Python object (also a dictionary if present) from a .txt or .json file.