P
P
progerstas2020-05-15 14:34:30
Python
progerstas, 2020-05-15 14:34:30

Why does the code only work if next_free is 0?

Here is the code:

@client.command()
async def bonus(ctx):
    cursor.execute(f"SELECT next_free FROM users WHERE id = '{ctx.message.author.id}'")
    if int(cursor.fetchone()[0]) < round(time.time()) or cursor.fetchone()[0] == 0:
        r = random.randint(150, 300)
        cursor.execute(f"SELECT coins next_free FROM users WHERE id = '{ctx.message.author.id}'")
        new_balance = r + cursor.fetchone()[0]
        cursor.execute(f"UPDATE users SET coins = {new_balance} WHERE id = '{ctx.message.author.id}'")
        conn.commit()

        t = round(time.time()) + 10800
        cursor.execute(f"UPDATE users SET next_free = {t} WHERE id = '{ctx.message.author.id}'")
        conn.commit()
        await ctx.send(f'Вы получили {r} монет!')
    else:
        await ctx.send('Вы не можете получить бонус сейчас!')


Here is the error:
Ignoring exception in command bonus:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\All\Python\Bot DS\SecurityBot\main.py", line 718, in bonus
    if int(cursor.fetchone()[0]) < round(time.time()) or cursor.fetchone()[0] == 0:
TypeError: 'NoneType' object is not subscriptable

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

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2020-05-15
@MrCute

Maybe because the check passes cursor.fetchone()[0] == 0
And the error is because the cursor returns nothing - cursor.fetchone().
And that's why cursor.fetchone() returns NoneType, which can't fetch the element at index 0.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question