定义的前缀不会从 json 文件 discord 加载

The defined prefix wont load from json file discord

所以我不知道这里有什么错误。如果您查看下面的代码,您会看到我将前缀定义为前缀,但它只显示了 str 前缀。帮助。

代码:

@client.command()
async def test(ctx):

    with open('main code\pp.json', 'r') as f:
        prefixes = json.load(f)


    prefixes[str(ctx.guild.id)] = prefix

    embed = discord.Embed(colour = discord.Color.red())

    embed.set_author(name="test")
    embed.add_field(name="testing", value=f"{prefix}")
   
    await ctx.send(embed=embed)

结果只是“前​​缀”而不是 json 文件中的前缀

在这一行:

prefixes[str(ctx.guild.id)] = prefix

反过来说:

prefix = prefixes[str(ctx.guild.id)]

尽管前缀仍不在范围内,但要解决此问题,您可以将代码移至 with 语句中,例如。

@client.command()
async def test(ctx):

    with open('main code\pp.json', 'r') as f:
        prefixes = json.load(f)


        prefixes[str(ctx.guild.id)] = prefix

        embed = discord.Embed(colour = discord.Color.red())

        embed.set_author(name="test")
        embed.add_field(name="testing", value=f"{prefix}")
    
        await ctx.send(embed=embed)