【问题标题】:Discord Python Rewrite - Account GeneratorDiscord Python 重写 - 帐户生成器
【发布时间】:2020-09-06 17:22:58
【问题描述】:

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

代码:

@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: 'list' object cannot be 解释为整数

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite


    【解决方案1】:

    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!")
    

    【讨论】:

    • 我试过你的代码,它生成后,它会删除整个列表。
    • 不应该这样,你从同一个 json 文件中弹出一个索引。您确定在测试之前文件中有更多内容吗?
    • 啊我明白了,我不经常使用 json 文件所以我犯了不使用 f 的错误,请现在尝试告诉我它是怎么回事!
    • 哦,我在 .json 文件的 alt 之前使用 []
    • 它得到了这个错误命令引发了一个异常:AttributeError: '_io.TextIOWrapper' object has no attribute 'pop'
    【解决方案2】:

    您可以添加到 JSON 文件。我这样做是为了让用户 id 成为键,值是数字 0。您可以轻松地对其进行编辑。

    这可能不是您想要的,但在我看来,这样做会更好。这是为用户创建一个帐户,而不是使用一定数量的帐户。

    @bot.command()
    async def gentest(ctx):
        genembed = discord.Embed(
            title="Minecraft NFA",
            colour=discord.Color.green()
        )
    
        with open('accounts.json', 'r') as f:
            accounts = json.load(f)
    
        genembed.add_field(
            name="Account:", value='Created Successfully', inline=False)
    
        accounts[ctx.author.id] = 0  # key is the id of the user and value is zero
    
        with open('accounts.json', 'w') as f:
            json.dump(accounts, f)
    
        await ctx.author.send(embed=genembed)
        await ctx.send(f"{ctx.author.mention} Please check your DMs!")
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2021-08-24
      • 2020-06-24
      • 2021-01-02
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多