Z
Z
znsxwq2022-01-20 14:44:55
Python
znsxwq, 2022-01-20 14:44:55

How to check for a match between an element from a list and a value in sqlite3?

I'm working on a guild system and making a command to create a guild and want to do a check by name. I mean, if there is a guild with that name, then it gives an error, and if not, then the guild is successfully created, but the code does not work and gives an error:

Command raised an exception: TypeError: 'NoneType' object is not subscriptable


Is it possible to solve this problem somehow? Thanks in advance.

Here is my code:
@commands.command(aliases=['gcreate'])
  async def __create_guild(self, ctx, name: str = None):
    color = discord.Color.random()
    guild = ctx.guild
    create = self.cursor.execute('SELECT guild_create_numb FROM users WHERE id = ?', (ctx.author.id,)).fetchone()[0]
    names = self.cursor.execute('SELECT g_name FROM guilds').fetchone()[0]
    if create == 1:
      await ctx.send(embed = discord.Embed(title=f"", description=f'**<@{ctx.author.id}>, вы не можете создать больше гильдий!**', color=0xf61e1e))
    else:
      if name in names:
        await ctx.send(embed = discord.Embed(title=f"", description=f'**<@{ctx.author.id}>, гильдия с таким названием уже существует!**', color=0xf61e1e))
      else:
        self.cursor.execute("INSERT INTO guilds VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (name, 'Пусто...', ctx.author.id, 'Не назначен...', 0, 0, 0, 0, 0,))
        self.cursor.execute(f"UPDATE users SET member_guild = '{name}' WHERE id = ?",(ctx.author.id,))
        self.cursor.execute("UPDATE users SET guild_create_numb = ? WHERE id = ?",(1, ctx.author.id,))
        self.connection.commit()
        #role = await guild.create_role(name=f"Участник гильдии '{name}'", colour = color)
        #await ctx.author.add_roles(role)

        embed = discord.Embed(title=f"Гильдия `{name}` успешно создана", description=f"Настроить гильдию: `{ctx.prefix}gedit`", colour=color)
        embed.set_footer(text=f"{ctx.author}", icon_url=f"{ctx.author.avatar_url}")
        await ctx.message.channel.send(embed = embed)
        print(names)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-20
@Vindicar

.fetchone()[0]
Shot himself in the foot, it's called. .fetchone() might return None if the SELECT didn't find any matching records, in which case an attempt to access by index would generate

exception
TypeError: 'NoneType' object is not subscriptable

So check what fetchone() returned to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question