所以我按照youtube教程做了这个“
@client.command()
@commands.cooldown(1, 6.0, commands.BucketType.user)
async def work(ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
earnings = random.randrange(1000)
await ctx.send(f"You worked on cleaning a toilet, got yourself covered with toilet water and poo, but you managed to get $ `{earnings}`")
wallet_amount = users[str(user.id)]["Wallet"] + earnings
with open("bank.json", "w") as f:
json.dump(users,f)
这个以前有一个"+="
但它不起作用,我的朋友说它会像x = a + x
,它是有意义的,我决定把它变成一个+
,但是,现在它不增加收入金额。我必须改变什么?我的许多其他类似代码都有同样的问题
编辑:
似乎
json
在字典中保持整数键为字符串,所以我不得不将str()
放回users[str(user.id)]
我不能测试你的代码,但它只需要
但是这个
user.id
可能不存在于users
中,因此您应该检查它并使用0
创建钱包编辑:
问题还在于您在
get_bank_data()
中做了什么,因为它为users = ...
赋值,它可能替换数据,也可能删除新值编辑:
使用命令
!work
和!wallet
的最小工作示例如果它在开始时找不到
bank.json
,那么它将创建它相关问题 更多 >
编程相关推荐