I
I
Ivan Stal2020-12-08 14:36:51
Python
Ivan Stal, 2020-12-08 14:36:51

How to fix a bug in creating an economical discord bot in Python?

I created a discord bot in python and I want it to be able to show the user's balance. Here is the code:

import discord
import sqlite3
from discord.ext import commands

connection = sqlite3.connect("Discord.db")
cursor = connection.cursor()
token = ''
bot = commands.Bot(command_prefix='+')

@bot.event
async def on_ready():
  cursor.execute("""CREATE TABLE IF NOT EXISTS "users" (
    "name"	TEXT,
    "id"	INT,
    "cash"	BIGINT,
    "rep"	INT
  )""")
  connection.commit()

  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, 100, 0")
        connection.commit()
      else:
        pass

@bot.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, 100, 0")
    connection.commit()
  else:
    pass

@bot.command(aliases = ['balance','bal','money'])
async def bank(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]}**"""
    ))
  else:
    await ctx.send(embed=discord.Embed(
      description = f"""Баланс пользователя **{member}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}").format(member.id).fetchone()[0]}**"""
    ))

bot.run(token)

I start the bot and everything is fine, there are no errors, when I write + bank to see the balance, nothing happens and in the console it gives this error:
$ python bot.py
Ignoring exception in command bank:
Traceback (most recent call last):
  File "C:\Users\xeon\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot.py", line 50, in bank
    description = f"""Баланс пользователя **{ctx.author}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}").format(ctx.author.id).fetchone()[0]}**"""
sqlite3.OperationalError: unrecognized token: "{"

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

Traceback (most recent call last):
  File "C:\Users\xeon\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\xeon\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\xeon\AppData\Local\Programs\Python\Python38-32\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: OperationalError: unrecognized token: "{"

Help me please!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-12-08
@bacon

Stop stuffing expressions like this into f-strings

S
Sergey Tikhonov, 2020-12-16
@tumbler

cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 0, 100, 0")

The closing brace is missing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question