Ошибка при использовании disnake.py+sqlite3

Рейтинг: 0Ответов: 1Опубликовано: 06.02.2023

Пишу бота и вот такая ошибка перепробывал много вариантов как решить

Прикрепляю ошибку:

Ignoring exception in slash command 'welcome':
Traceback (most recent call last):
  File "C:\Users\maksi\AppData\Local\Programs\Python\Python38\lib\site-packages\disnake\ext\commands\slash_core.py", line 730, in invoke
    await call_param_func(self.callback, inter, self.cog, **kwargs)
  File "C:\Users\maksi\AppData\Local\Programs\Python\Python38\lib\site-packages\disnake\ext\commands\params.py", line 1022, in call_param_func
    return await maybe_coroutine(safe_call, function, **kwargs)
  File "C:\Users\maksi\AppData\Local\Programs\Python\Python38\lib\site-packages\disnake\utils.py", line 599, in maybe_coroutine
    return await value
  File "main.py", line 165, in welcome
    _admin:int = cursor.fetchone()['ADMIN']
TypeError: tuple indices must be integers or slices, not str

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

Traceback (most recent call last):
  File "C:\Users\maksi\AppData\Local\Programs\Python\Python38\lib\site-packages\disnake\ext\commands\interaction_bot_base.py", line 1353, in process_application_commands
    await app_command.invoke(interaction)
  File "C:\Users\maksi\AppData\Local\Programs\Python\Python38\lib\site-packages\disnake\ext\commands\slash_core.py", line 739, in invoke
    raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: tuple indices must be integers or slices, not str

Прикрепляю код:

@bot.slash_command(name = "welcome", description="Встановити привітання нового користувача")
async def welcome(ctx, * , text):
    cursor.execute(f"SELECT * FROM users WHERE guild = '{ctx.guild.id}' AND user_id = '{ctx.user.id}'")
    _admin:int = cursor.fetchone()['ADMIN']
    if _admin <6:
        emb4 = disnake.Embed(title=" Что-то пошло не так ",  description= f'\n▹ Упс, у вас не недостаточно прав ', color=0xff9900)
        await ctx.send(embed = emb4)
    else:
        cursor.execute(f"SELECT * FROM guilds WHERE guild_id = '{ctx.guild.id}'")
        _welcome = cursor.fetchone()['welcome']
        if text == "get":
            emb4 = disnake.Embed(title=" Приветствие учасника ",  description= f'\n▹ {_welcome}  ', color=0x008080)
            await ctx.send(embed = emb4)
        elif text == "remove":
            cursor.execute(f"UPDATE guilds SET welcome = NULL WHERE guild_id = '{ctx.guild.id}'")
            connect.commit()
            emb4 = disnake.Embed(title=" Успешно ",  description= f'\n▹ Приветствие нового участника удалено. ', color=0x008080)
            await ctx.send(embed = emb4)
        else:
            cursor.execute(f"UPDATE guilds SET welcome = '{text}' WHERE guild_id = '{ctx.guild.id}'")
            connect.commit()
            emb4 = disnake.Embed(title=" Успешно ",  description= f'\n▹ Приветствие нового участника было добавлено. ', color=0x008080)
            await ctx.send(embed = emb4)

Cуть того что оно должно делать: если посмотреть на ошибку то она в 165 строке а там мы в столбец ADMIN смотрим все данные чтобы потом их проверить(если что в названии переменой можно и без :int потому что та же ошибка)

Ответы

▲ 0

Обращение по ключу - ['ADMIN'], работает только со словарями, а у вас там tuple.

Также: почитайте про SQL-инъекции, в теории через переменную text вам любой желающий сможет вставить доп. SQL-запрос.