Discord.py,如果语句不起作用 [英] Discord.py if statements not working

查看:80
本文介绍了Discord.py,如果语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码(Python 3.6),如果用户有钱包,那应该可以做到,然后显示余额。如果他们没有钱包,则装一个钱包并显示余额。

I have this code (Python 3.6), and its supposed to do, if the user has a wallet with them, then show the balance. If they don't have a wallet, crate a wallet and show the balance.

我有一个名为 assets.json的文件,其中包含用户ID。

I have a file called amounts.json with the users id.

代码总是跳转到声明用户没有帐户的语句,实际上我确实有,但出现错误:

The code always jumps to the statement where it says the user does not have a account, when in fact I do, and gives me the error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: BlockIoAPIError: Failed: Label already exists on your account for Network=DOGE.

我该如何修复它,这样我每次尝试做余额时都不会尝试制作钱包

How can I fix it so it does not try and make a wallet every single time I do the balance command?

代码:

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


推荐答案

您需要对json文件进行数据修复,使其具有代码所期望的结构。如果有重复的条目,则下面将对其值进行求和。这不是您的机器人的一部分,您只需要运行一次即可。您还需要遍历任何其他涉及或依赖于金额

You need to datafix your json file so it has the structure your code expects. If there are duplicate entries the below will sum their values. This isn't part of your bot and you should only have to run this once. You'll also want to go over any other code that touches or relies on 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)

这篇关于Discord.py,如果语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆