S
S
sola-boat2021-05-09 11:36:26
Python
sola-boat, 2021-05-09 11:36:26

An error occurred while writing a discord bot in python. How to decide?

ОШИБКА:
File "bot.py", line 324, in __buy
    elif cursor.execute("SELECT cost FROM shop WHERE role_id = {}".format(role.id)).fetchone()[0] > cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[1]:
IndexError: tuple index out of range

ТАБЛИЦА:
@client.event
async def on_ready():
  print('SolaBot готов к полёту!')
  await client.change_presence(status = discord.Status.online, activity = discord.Game('!help |alpha|'))

  cursor.execute("""CREATE TABLE IF NOT EXISTS users(
    name TEXT,	
    id INT,
    cash BIGINT,
    rep INT,
    lvl INT,
    server_id INT
  )""")


  cursor.execute("""CREATE TABLE IF NOT EXISTS shop(
    role_id INT,	
    id INT,
    cost BIGINT
  )""")
  
  for guild in client.guilds:
    for member in guild.members:
      if cursor.execute(f"SELECT id FROM users WHERE id = {member.id}").fetchone() is None:
        cursor.execute(f"INSERT  INTO 	users VALUES('{member}', {member.id}, 0, 0, 1, {guild.id})")
      else:
        pass

  conn.commit()
  print('DataBase Loading... 100%')


КОД:
@client.command(aliases = ['buy'])
async def __buy(ctx, role: discord.Role = None):
  if role is None:
    await ctx.send(f"**{ctx.author}**, укажите роль которую желаете приобрести")
  else:
    if role in ctx.author.roles:
      await ctx.send(f"**{ctx.author}**, у вас уже имеется данная роль")
    elif cursor.execute("SELECT cost FROM shop WHERE role_id = {}".format(role.id)).fetchone()[0] > cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[1]:
      await ctx.send(f"**{ctx.author}**, у вас недостаточно средств для покупки данной роли")
    else:
      await ctx.author.add_roles(role)
      cursor.execute("UPDATE users SET cash = cash - {0} WHERE id = {1}".format(cursor.execute("SELECT cost FROM shop WHERE role_id = {}".format(role_id)).fetchone()[0], ctx.author.id)) 

      await ctx.message.add_reaction('✅')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alegzz, 2021-05-09
@alegzz

like the interpreter clearly wrote where to look for the error. if you do select one column and one row then tuple will contain 1 value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question