'NoneType' object is not subscriptable | discord.py
Сам код:
connection=sqlite3.connect('server.db')
cursor = connection.cursor()
@client.event
async def on_ready():
print('Бот успешно подключен к серверу!')
await client.change_presence( status = discord.Status.online, activity = discord.Game('Sublime (пишет код)') )
cursor.execute("""CREATE TABLE IF NOT EXISTS users (
name TEXT,
id INT,
cash BIGINT,
lvl INT
)""")
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, 1)")
else:
pass
connection.commit()
@client.event
async def on_member_join(member):
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, 1)")
connection.commit()
else:
pass
@client.command( pass_context = True)
async def balance(ctx, member: discord.Member = None):
if member is None:
# 94 строчка,в которой вроде ошибка, строкой ниже
await ctx.send(embed = discord.Embed(
description = f"""баланс пользователя **{ctx.author}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]} :leaves:**"""
))
else:
await ctx.send(embed = discord.Embed(
description = f"""баланс пользователя **{member}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(member.id)).fetchone()[0]} :leaves:**"""
))
Ошибка: Traceback (most recent call last):
File "D:\python\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke await ctx.command.invoke(ctx)
File "D:\python\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke await injected(*ctx.args, **ctx.kwargs)
File "D:\python\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
Уже очень долго не могу понять в чем проблема. Списывал в точь точь с гайдов, пробовал копипастить у других.
Ответы (1 шт):
'NoneType' object is not subscriptable означает, что вы пробуете выполнить операцию [] для None.
В вашем случае запрос SELECT cash FROM users WHERE id = {} очевидно не нашел пользователя и .fetchone() возвращает None.