@David1902

Не могу понять в чем ошибка?

Вот ошибка:
File "E:\Python (files)\Bots\tgbot\Dragon\tg_bot.py", line 462, in balance
if cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0] != None:
TypeError: 'NoneType' object is not subscriptable

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

Traceback (most recent call last):
File "C:\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 462, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable

@client.command(pass_context = True)
async def balance(ctx, member: discord.Member = None):
	if member is None:
		if cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0] != None:
			await ctx.send(embed = discord.Embed(
				description = f"""Баланс пользователя **{ctx.author.mention}** составляет **{cursor.execute('SELECT cash FROM users WHERE id = {}'.format(ctx.author.id)).fetchone()[0]}:dollar:**""",
	 				color = 0x2ecc71))

		else:
			await ctx.send(embed = discord.Embed(
			description = f"""Баланс пользователя **{ctx.author.mention}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]}:dollar:**""",
				color = 0x2ecc71))
		
	else:
		await ctx.send(embed = discord.Embed(
			description = f"""Баланс пользователя **{member.mention}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(member.id)).fetchone()[0]}:dollar:**""",
				color = 0x2ecc71))


Что делать?
  • Вопрос задан
  • 1145 просмотров
Пригласить эксперта
Ответы на вопрос 2
hottabxp
@hottabxp Куратор тега Python
Сначала мы жили бедно, а потом нас обокрали..
Здесь 2 ошибки:
1) В sql запросах не нужно использовать форматирование строк;
2) В строке:
if cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0] != None:
fetchone() вернул вам None, и вы далее обращаетесь к "первому" элементу None.

Я в дискорде не шарю, попробуйте такой пример:
user_id = 1 # id пользователя
result = cursor.execute("SELECT cash FROM users WHERE id = ?",(user_id,)).fetchone()
if result != None:
	print('User found!')
else:
	print('Error! User not found!')
Ответ написан
alfss
@alfss
https://career.habr.com/alfss
https://tproger.ru/translations/debugging-python-w... научится отлаживать программу
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы