首页 > 解决方案 > Discord Python 重写 - 帐户生成器

问题描述

我想用python和json制作一个不和谐的账户生成器,我可以让它生成,但我不能让它在生成后删除账户,请帮忙。

编码:

@client.command()
async def gentest(ctx):
    
    genembed = discord.Embed(
        title="Minecraft NFA",
        colour=discord.Color.green()
        )

    with open('alts.json', 'r') as f:
        alts = json.load(f)

    genembed.add_field(name="Account:", value=random.choice(alts), inline=False)

    with open('alts.json', 'w') as f:
        alts = alts.pop(alts)

    await ctx.author.send(embed=genembed)
    await ctx.send(f"{ctx.author.mention} Please check your DMs!")

但是当我尝试生成(使用 alts.pop)时,它会发送此错误:

命令引发异常:TypeError:“列表”对象不能解释为整数

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


Alts 只是 alts 的列表,它不是列表(整数)的索引,为此您必须执行以下操作:

@client.command()
async def gentest(ctx):
    
    genembed = discord.Embed(
        title="Minecraft NFA",
        colour=discord.Color.green()
        )

    with open('alts.json', 'r') as f:
        alts = json.load(f)
    
    choice = random.choice(alts)
    genembed.add_field(name="Account:", value=choice, inline=False)

    with open('alts.json', 'w') as f:
        del alts[alts.index(choice)]
        f.write(json.dumps(alts, indent=4))

    await ctx.author.send(embed=genembed)
    await ctx.send(f"{ctx.author.mention} Please check your DMs!")

推荐阅读