I'm trying to create a command where a random meme is shown from a json.
Unfortunately I can't do it ... I can do it using random.choice normally but since I'm a lazy person to update the code I always created a command that would allow me to add them in a json. So I have to be able to randomly extract the main key of my json. How could I do?
code:
@client.command(aliases=["Meme","MEME"])
async def meme(ctx):
await ctx.message.delete()
meme_data_dict = meme_data.json
url = random.choice(list(meme_data_dict.keys))
print(url)
author = meme_data_dict[url]["autore"]
title = meme_data_dict[url]["titolo"]
channel = client.get_channel(638016878589116416)
if ctx.channel.id == channel.id:
embed = discord.Embed(
title=f'{title}',
color=0x003399
)
embed.set_author(name=f'fatto da {author}')
embed.set_footer(text=f'Richiesto da {ctx.author.name}')
embed.set_image(url=url)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
color=0xa61022
)
embed.set_author(
name=f"Per questo comando devi usare {channel.name}!",
)
await ctx.send(embed=embed, delete_after=10.0)
return
Json (your name is meme_data.json):
{"https://cdn.discordapp.com/avatars/491769129318088714/f23fd300d377ab133db1d6ac7db5d10b.webp?size=1024": {"autore": "10", "titolo": "sesso pazzoide"}, "https://cdn.discordapp.com/attachments/638016878589116416/839977460384923659/soviet_soldier_2.jpg": {"autore": "pene", "titolo": "ciaociao"}}
This is how my json is active:
@client.event
async def on_ready():
global meme_data
try:
with open('meme_data.json') as f:
meme_data = json.load(f)
except FileNotFoundError:
print("Impossibile caricare meme_data.json")
meme_data = {}
error:
Ignoring exception in command meme:
Traceback (most recent call last):
File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 2235, in meme
meme_data_dict = meme_data.json
AttributeError: 'dict' object has no attribute 'json'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\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: 'dict' object has no attribute 'json'
memeto something likememe_data, because there is this function calledmeme. Also what exactly is your error?discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'dict' object has no attribute 'json'