在Discord.py中显示来自.json的数据



我在每个不同的服务器上做一个自定义前缀的bot,我想做一个命令,显示他们输入命令的服务器的前缀,我的json看起来像这样:

{
"863801417105014785": "l.",
"642736426374602346": "b.",
"387468726374623439": "t."
}

我也得到了自定义前缀代码,我希望它能帮助

这是我试过的代码

@client.command()
async def prf(ctx):
try:
with open('./config/prefixes.json', 'r') as f:
prfxs = json.load(f())[0]
await ctx.message.send(data['guild.id'])
except IOError:
return

您的代码中有逻辑错误。data['guild.id']不会吐出任何东西,因为它没有在代码中定义。你必须使用prfxs,因为这是你的json文件。

新命令/代码:

with open('./config/prefixes.json', 'r', encoding='utf-8') as fp: # Open the JSON-file/folder
cprefix = json.load(fp) # Load the JSON-file
try:
prefix = cprefix[f"{ctx.guild.id}"] # Get the data for the guild.id
await ctx.send(f'My prefix for this server is **{prefix}**') # Display the prefix
except KeyError:
return

最新更新