我正在为我的机器人发出经济命令。我认为该错误是在open_account()
函数中。当您运行balance命令时,会看到我没有帐户,并按原样写入JSON文件,但未发送嵌入文件。我检查了嵌入,效果很好。这是调用嵌入程序之前的事情。另外,beg命令不会将您制作的金额添加到JSON文件中。我花了几天的时间试图理解问题出在哪里,但是没有运气。任何人都可以帮助我弄清楚我搞砸了吗?
@client.command()
async def balance(ctx):await open_account(ctx.author)
users = await get_bank_data()
wallet_amt = users[str(user.id)]["wallet"]
bank_amt = users[str(user.id)]["bank"]
embed=discord.Embed(title="{}s balance:".format(member.name), color=0xe20303)
embed.add_field(name="Wallet:", value=wallet_amt, inline=False)
embed.add_field(name="Bank:", value=bank_amt, inline=False)
await ctx.send(embed=embed)
async def open_account(user):
users = await get_bank_data()
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 0
users[str(user.id)]["bank"] = 0
with open("bank.json", "w") as f:
json.dump(users, f)
return True
async def get_bank_data():
with open("bank.json", "r") as f:
users = json.load(f)
return users
@client.command()
async def beg(ctx):
users = await get_bank_data()
user = ctx.author
earnings = random.randrange(2000)
if earnings == 0:
await ctx.send(f"How unlucky... You didn't get anything...")
elif earnings > 50:
await ctx.send(f"Nice you got ${earnings} from a cool dude")
elif earnings > 100:
await ctx.send(f"Someone felt nice and gave you ${earnings}")
elif earnings > 500:
await ctx.send(f"You seem to have a way with people! Someone gave you ${earnings}")
elif earnings > 800:
await ctx.send(f"What a lucky day!! Someone gave you ${earnings}")
elif earnings > 1500:
await ctx.send(f"A rich man passed by you and felt bad. So ha gave you ${earnings}")
elif earnings > 2000:
await ctx.send(f"A shady man walked up to you and said 'I know how tough it can be out here' before giving you ${earnings}")
users[srt(user.id)]["wallet"] += earnings
with open("bank.json", "r") as f:
users = json.load(f)
答案 0 :(得分:0)
函数open_account和get_bank_data不是异步函数。尝试先删除异步/等待。
答案 1 :(得分:0)
此外,在开始时尝试将async def balance(ctx):await open_account(ctx.author)
间隔为await open_account应该在另一行。
答案 2 :(得分:0)
在最后三行中,您将str
拼错为srt
答案 3 :(得分:0)
首先,您需要从 get_bank_data 和 open_account 中删除 await
和 async
其次,您需要将 user = ctx.author
存入余额
第三,您需要修复您的错字in beg command users[srt(user.id)]["wallet"] += earnings
into users[str(user.id)]["wallet"] += earnings
第四个你需要使用
with open("bank.json", "w") as f:
json.dump(users, f)
代替
with open("bank.json", "r") as f:
users = json.load(f)
就是这样。它应该可以工作,如果不行,你可以在这个答案下写,我们可以编辑它。
E:
我也用了 embed=discord.Embed(title=f"{ctx.author.name}s balance:", color=0xe20303)
来平衡