问题
I Want to make a discord account generator using python and json, i can make it gen but i cant make it delete the account after genned, please help.
The code:
@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!")
but when i tried to gen (with the alts.pop) it sends this error:
Command raised an exception: TypeError: 'list' object cannot be interpreted as an integer
回答1:
Alts is just the list of alts, it's not an index of the list (integer) for this you would have to do something like:
@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!")
回答2:
You can add to the JSON file. I made it so that the user id the the key and the value is a number 0. You can easily edit that.
This might not be what you wanted but making it like this is better in my opinion. Which is make an account for the user instead of using a set number of accounts.
@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!")
来源:https://stackoverflow.com/questions/63760658/discord-python-rewrite-account-generator