P
P
Pam312021-07-28 20:56:46
Python
Pam31, 2021-07-28 20:56:46

What's wrong with the discord bot?

Error:
Ignoring exception in command __balance:
Traceback (most recent call last):
File "C:\Users\Kiber1_KM7\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py ", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Kiber1_KM7\Downloads\myBot\bot.py", line 48, in __balance
description = f"""User balance **{ctx.author}** is **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]} :leaves:* *"""
TypeError: 'NoneType' object is not subscriptable

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

Traceback (most recent call last):
File "C:\Users\Kiber1_KM7\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Kiber1_KM7\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, * *ctx.kwargs)
File "C:\Users\Kiber1_KM7\AppData\Local\Programs\Python\Python39\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

import discord
from discord.ext import commands

import sqlite3
from config import settings

client = commands.Bot(command_prefix = settings['PREFIX'])
client.remove_command('help')

connection = sqlite3.connect('server.db')
cursor = connection.cursor()


@client.event
async def on_ready():
  cursor.execute("""CREATE TABLE IF NOT EXISTS users (
    name TEXT,
    id INT,
    cash BIGINT,
    rep INT,
    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, 0, 1)")
      else:
        pass

  connection.commit()
  print('Bot connected')


@client.event
async def on_member_join(member):
  if cursor.execute(f"SELECT id FROM user WHERE id = {member.id}").fetchone() is None:
    cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 0, 0, 1)")
    connection.commit()
  else:
    pass


@client.command(aliases = ['balance', 'cash']) 
async def __balance(ctx, member: discord.Member = None):
  if member is None:
    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:**"""
    ))


client.run(settings['TOKEN'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-28
@Pam31

> 'NoneType' object is not subscriptable
> .fetchone()[0] The
query didn't return any rows, and you don't check for that.
And yes:
1. Creating a query using .format() is shit, and the path to SQL injection (well, or just bugs). Learn how to use placeholders.
2. Inserting a method call inside a format template is shit because it's unreadable. And you can't check the returned value, as you just saw.
Call the method first, throw the result into a variable, and then substitute it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question