如果由于某种原因,Discord.py if语句不起作用..?

时间:2018-08-02 12:52:07

标签: python python-3.x discord.py

所以,我有以下代码。

@client.command(pass_context=True)
async def tip(ctx, amount: int, other: discord.Member):
    id = ctx.message.author.id
    other_id = other.id
    if id in ids:
        maddy = block_io.get_address_balance(label=id)
        oaddy = block_io.get_address_balance(label=other_id)
        s1 = json.dumps(maddy)
        d2 = json.loads(s1)
        s2 = json.dumps(oaddy)
        d3 = json.loads(s2)
        d2['data']['available_balance']
        d3['data']['available_balance']

        if float(d2['data']['available_balance']) > amount:
            if amount > 2:
                embed=discord.Embed(description="Tip Sent! **-1 Fee Deducted**", color=0x00ff00)
                await client.say(embed=embed)
                block_io.withdraw_from_labels(amounts=amount, from_labels=id, to_labels=other_id)
            else:
                embed=discord.Embed(description="Tipping requires you to have at least 2 Doge. **1 is deducted for fees.**", color=0x00ff00)
                await client.say(embed=embed)

            #embed=discord.Embed(description="", color=0x00ff00)
            #await client.say(embed=embed)
        else: #less then 
            embed=discord.Embed(description="**You dont have enough to tip!**", color=0x00ff00)
            await client.say(embed=embed)
            with open("ids.txt", "a") as f:
                print(id, file=f)
    else:
        block_io.get_new_address(label=id)
        block_io.get_address_balance(label=id)
        maddy = block_io.get_address_balance(label=id)
        oaddy = block_io.get_address_balance(label=other_id)
        s1 = json.dumps(maddy)
        d2 = json.loads(s1)
        s2 = json.dumps(oaddy)
        d3 = json.loads(s2)
        d2['data']['available_balance']
        d3['data']['available_balance']

        if float(d2['data']['available_balance']) > amount:
            if amount > 2:
                embed=discord.Embed(description="Tip Sent! **-1 Fee Deducted**", color=0x00ff00)
                await client.say(embed=embed)
                block_io.withdraw_from_labels(amounts=amount, from_labels=id, to_labels=other_id)
            else:
                embed=discord.Embed(description="Tipping requires you to have at least 2 Doge. **1 is deducted for fees.**", color=0x00ff00)
                await client.say(embed=embed)

            #embed=discord.Embed(description="", color=0x00ff00)
            #await client.say(embed=embed)
        else: #less then 
            embed=discord.Embed(description="**You dont have enough to tip!**", color=0x00ff00)
            await client.say(embed=embed)
            with open("ids.txt", "a") as f:
                print(id, file=f)
    with open("ids.txt", "a") as f:
        print(id, file=f)

当我运行命令时,我会尝试提示没有帐户的人。但是,它没有执行else语句,而是转到if语句,并产生错误。我希望它转到else语句,但是即使用户id不在id中也不会。

Ignoring exception in command tip
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "index.py", line 89, in tip
    oaddy = block_io.get_address_balance(label=other_id)
  File "/usr/local/lib/python3.6/site-packages/block_io/__init__.py", line 168, in hook
    return self.api_call(attr, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/block_io/__init__.py", line 290, in api_call
    raise BlockIoAPIError('Failed: '+response['data']['error_message'])
block_io.BlockIoAPIError: Failed: One or more addresses or labels do not exist on your account for Network=DOGE, or are invalid for Network=DOGE.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: BlockIoAPIError: Failed: One or more addresses or labels do not exist on your account for Network=DOGE, or are invalid for Network=DOGE.
Ignoring exception in command tip
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "index.py", line 89, in tip
    oaddy = block_io.get_address_balance(label=other_id)
  File "/usr/local/lib/python3.6/site-packages/block_io/__init__.py", line 168, in hook
    return self.api_call(attr, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/block_io/__init__.py", line 290, in api_call
    raise BlockIoAPIError('Failed: '+response['data']['error_message'])
block_io.BlockIoAPIError: Failed: One or more addresses or labels do not exist on your account for Network=DOGE, or are invalid for Network=DOGE.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: BlockIoAPIError: Failed: One or more addresses or labels do not exist on your account for Network=DOGE, or are invalid for Network=DOGE.

我想念什么?

1 个答案:

答案 0 :(得分:0)

这是失败的行:

oaddy = block_io.get_address_balance(label=other_id)

请注意,这同时存在于您的ifelse块中。如果 other 人没有帐户,那么您没有任何逻辑,只有调用命令的人没有帐户时才可以。您需要在某个时候检查if other_id in ids:

相关问题