sqlite Discord.py 错误

daolsyd0  于 2022-12-19  发布在  SQLite
关注(0)|答案(1)|浏览(167)

I linked my database with a discord.py command so that when I send one command, the user can't re-send another one. This works because I record his id in the database, but if the user uses the command repeatedly the database fills up with his requests, so if I refresh the database through the code I can make his id be taken into account and not be bugged. Is there a way to fix this or to refresh a database via code? Thank you.

@bot.command()
async def clear(ctx, amount = 0):
    time.sleep(1)
    await ctx.channel.purge(limit=amount)
    texto_final = discord.Embed(title= "Mensajes borrados correctamente :white_check_mark: ", description = f"{ctx.author.name} *tus mensajes se borraron correctamente.* **Mensajes borrados:** {amount}.", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
    await ctx.send(embed = texto_final)
    time.sleep(10)
    await ctx.channel.purge(limit = 1)

#DNI Interfaz 
async def on_BDLA(ctx):
    return row[4] != ctx.author.id

@bot.command()
@commands.check(on_BDLA)
async def crear_dni(ctx):
    respuesta = await bot.wait_for("message", timeout=60)
    respuesta = respuesta.content.split(", ")
    status = True
    if status == False:
        pass
    elif status == True:
        dni_info = discord.Embed(title = f"📇DNI de {ctx.author.name}", description= f"📄**Nombre** \n➥ *{respuesta[0]}* \n📄**Apellido** \n➥ *{respuesta[1]}* \n📅**Nacimiento** \n➥ *{respuesta[2]}* \n🏳️**Nacionalidad** \n➥ *{respuesta[3]}* \n🆔**ID** \n➥ *{ctx.author.id}*", color = discord.Color.purple() )
        dni_info.set_image(url="https://media.discordapp.net/attachments/1052061231680344134/1053135738180472832/banner.png")
        await ctx.send(embed = dni_info)
        async with aiosqlite.connect("main.db") as db:
            async with db.cursor() as cursor:
                await cursor.execute(f"INSERT INTO users (nombre, apellido, nacimiento, nacionalidad, user_id) VALUES (?, ?, ?, ?, ?)", (respuesta[0], respuesta[1], respuesta[2], respuesta[3], ctx.author.id))
            await db.commit()
        

con.close()```
i86rm4rw

i86rm4rw1#

尽量不要对数据库命令使用异步
我的意思是使用普通的sqlite3
这会非常好用

import sqlite3 

db=sqlite3.connect("main.db")
cursor= db.cursor()
@bot.command()
async def clear(ctx, amount = 0):
    time.sleep(1)
    await ctx.channel.purge(limit=amount)
    texto_final = discord.Embed(title= "Mensajes borrados correctamente :white_check_mark: ", description = f"{ctx.author.name} *tus mensajes se borraron correctamente.* **Mensajes borrados:** {amount}.", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
    await ctx.send(embed = texto_final)
    time.sleep(10)
    await ctx.channel.purge(limit = 1)

#DNI Interfaz 
async def on_BDLA(ctx):
    return row[4] != ctx.author.id

@bot.command()
@commands.check(on_BDLA)
async def crear_dni(ctx):
    respuesta = await bot.wait_for("message", timeout=60)
    respuesta = respuesta.content.split(", ")
    status = True
    if status == False:
        pass
    elif status == True:
        dni_info = discord.Embed(title = f"📇DNI de {ctx.author.name}", description= f"📄**Nombre** \n➥ *{respuesta[0]}* \n📄**Apellido** \n➥ *{respuesta[1]}* \n📅**Nacimiento** \n➥ *{respuesta[2]}* \n🏳️**Nacionalidad** \n➥ *{respuesta[3]}* \n🆔**ID** \n➥ *{ctx.author.id}*", color = discord.Color.purple() )
        dni_info.set_image(url="https://media.discordapp.net/attachments/1052061231680344134/1053135738180472832/banner.png")
        await ctx.send(embed = dni_info)
        
        
        
        cursor.execute(f"INSERT INTO users (nombre, apellido, nacimiento, nacionalidad, user_id) VALUES (?, ?, ?, ?, ?)", (respuesta[0], respuesta[1], respuesta[2], respuesta[3], ctx.author.id))
        db.commit()
        

con.close()

相关问题