我有这段代码(Python 3.6),如果用户有钱包,应该显示出余额。如果他们没有钱包,请建立一个钱包并显示余额。
我有一个名为apps.json的文件,带有用户ID。
代码总是跳到声明用户没有帐户的语句,而实际上我却拥有该帐户,并给出错误消息:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: BlockIoAPIError: Failed: Label already exists on your account for Network=DOGE.
我该如何解决它,以免每次执行余额命令时都不会尝试制作钱包?
代码:
@client.command(pass_context=True)
async def balance(ctx):
user_id = ctx.message.author.id
global amounts
if user_id not in amounts:
block_io.get_new_address(label=user_id)
knee = block_io.get_address_balance(label=user_id)
s1 = json.dumps(knee)
d2 = json.loads(s1)
d2['data']['available_balance']
embed=discord.Embed(description="Bitcoin: btc here \n\nLitecoin: here\n\nDogecoin: {}".format(d2['data']['available_balance']), color=0x00ff00)
await client.say(embed=embed)
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
elif user_id in amounts:
knee = block_io.get_address_balance(label=user_id)
s1 = json.dumps(knee)
d2 = json.loads(s1)
d2['data']['available_balance']
embed=discord.Embed(description="Bitcoin: btc here \n\nLitecoin: here\n\nDogecoin: {}".format(d2['data']['available_balance']), color=0x00ff00)
await client.say(embed=embed)
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
Json代码:
amounts = {}
@client.event
async def on_ready():
global amounts
try:
with open('amounts.json') as f:
amounts = json.load(f)
except FileNotFoundError:
print("Could not load amounts.json")
amounts = {}
答案 0 :(得分:0)
您需要对json文件进行数据修复,以使其具有代码期望的结构。如果有重复的条目,则下面将对其值进行求和。这不是您的机器人的一部分,您只需要运行一次即可。您还需要遍历任何其他涉及或依赖于amounts
import json
with open('amounts.json') as f:
old_amounts = json.load(f)
new_amounts = {}
for d in old_amounts:
for k, v in d:
new_amounts[k] = new_amounts.get(k, 0) + v
with open('amounts.json') as f:
json.dump(new_amounts, f)
这些d2 = json.loads(s1)
行可能应该是d2 = json.load(s1)