繁体   English   中英

Discord.py,如果语句不起作用

[英]Discord.py if statements not working

我有这段代码(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.

我该如何解决它,使它不会在每次执行balance命令时都尝试制作钱包?

码:

@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)

杰森代码:

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 = {}

您需要对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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM