我正在尝试创建一个命令,其中显示来自 json
的随机模因。
不幸的是我不能这样做......我可以正常使用 random.choice
来完成,但由于我是一个懒惰的人来更新代码,所以我总是创建一个命令,允许我将它们添加到 json 中。所以我必须能够随机提取我的json的主键。我该怎么办?
代码:
@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(你的名字是 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"}}
这是我的 json 的活动方式:
@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 = {}
错误:
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'
答案 0 :(得分:1)
要获取字典的随机对象(导入的 json 文件),请使用 url = random.choice(list(meme_data_dict.keys))
。在您的代码中将是:
@client.command(aliases=["Meme","MEME"])
async def meme(ctx):
await ctx.message.delete()
meme_data_dict = meme_data #you have to rename your variable meme, for example to meme_data, because there is already the function called meme
url = random.choice(list(meme_data_dict.keys()))
print(url)
author = meme_data_dict[url]["autore"]
title = meme_data_dict[url]["titolo"]
#the rest of your code can stay the same
如果这不能回答您的问题,请进一步说明问题所在。