Answer the question
In order to leave comments, you need to log in
Need help with sqlite3, discord.py?
The goal is simple, I want to write a few commands, the purpose of which is to first display the "store page in the chat" i.e. in the first column the name in the second is the price and in the third is the description. After that, with the help of a command (for example, !buy *product id*),
the item was bought and placed in the inventory.
I just don’t know anymore, I tried 15 options, nothing just happens to me. The bot is running, online, but still nothing.
Here is the code (it used to be bigger, but then I decided to rewrite everything from scratch.):
from cgitb import text
from discord.ext import commands
import sqlite3
import discord
import random
TOKEN = "OTQ1NzU2NTQ5NzkcNq1ZOTY4.HhUybA.1flzil982KjpA2c_E1C0qR-XxLM"
bot = commands.Bot(command_prefix=('рпг'))
bot.remove_command( 'help' )
db = sqlite3.connect('eco.db')
c = db.cursor()
@bot.command()
async def купить(ctx, text, id):
if c.execute("SELECT cost FROM shop WHERE name = ?", (text)).fetchone() > c.execute("SELECT money FROM userinfos WHERE id_user = ?", (id)).fetchone():
await ctx.send("Недостаточно средств для покупки")
else:
await ctx.send("Товар куплен")
bot.run(TOKEN)
Answer the question
In order to leave comments, you need to log in
There is more wrong than right in the code...
1. You have written that the command should receive two parameters - text and id. If she must find the id herself, then it must be removed from the buy() parameters. id must be obtained from ctx.author.id.
2. Have you ever tried accessing your database? Have you looked at what fetchone() returns to you?
At your request, it will return either a tuple with a single value, or None if no matching row was found.
Neither can be directly compared!
Pull the result into variables, check for None, and then compare null elements.
3. Remove the bot token from the question body.
4. I advise you to create a cursor inside the handler, in order to avoid problems with simultaneous use.
5. I advise you to specify the types of parameters in the command description! discord.py pays attention to them and tries to convert the input string.
6. As it was already written, execute() takes a tuple as the second parameter, even if it has only one element. A tuple of one element is written as .
async def купить(ctx, text: str):
(item,)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question