所以我有这段代码可以给用户on_message
提供1个exp,但是当我调用$stream
时,应该给出50 exp的命令却得到了错误.OperationalError: database is locked
。当用户调用漫游器前缀bot = commands.Bot(command_prefix = '$', case_insensitive=True)
时,如何为用户设置例外?
向用户提供+1 exp的代码,如果该表不存在,则创建表
@commands.Cog.listener()
async def on_message(self,message):
db = sqlite3.connect('banco.sqlite')
cursor = db.cursor()
cursor.execute(f'SELECT user_id FROM levels WHERE guild_id = {message.author.guild.id} AND user_id = {message.author.id}')
result = cursor.fetchone()
if result is None:
sql = (f'INSERT INTO levels(guild_id, user_id, exp, lvl) VALUES(?,?,?,?)')
val = (message.guild.id, message.author.id, 0, 0)
cursor.execute(sql, val)
db.commit()
else:
cursor.execute(f'SELECT user_id, exp, lvl FROM levels WHERE guild_id = {message.author.guild.id} AND user_id = {message.author.id}')
result1 = cursor.fetchone()
exp = int(result1[1])
sql = ('UPDATE levels SET exp = ? WHERE guild_id = ? and user_id = ?')
val = (exp + 1, str(message.guild.id), str(message.author.id))
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
工作命令
@commands.command()
async def stream(self,ctx):
db = sqlite3.connect('banco.sqlite')
cursor = db.cursor()
cursor.execute(f'SELECT user_id FROM levels WHERE guild_id = {ctx.author.guild.id} AND user_id = {ctx.author.id}')
em = discord.Embed(title = '', color = (0x70c886))
em.add_field(name = 'Donates da stream :',value = (f'lucrou +<:bbc:639345897922101248> 50 ') )
await ctx.send(embed = em)
cursor.execute(f'SELECT user_id, exp, lvl FROM levels WHERE guild_id = {ctx.author.guild.id} AND user_id = {ctx.author.id}')
result1 = cursor.fetchone()
exp = int(result1[1])
sql = ('UPDATE levels SET exp = ? WHERE guild_id = ? and user_id = ?')
val = (exp + 50, str(ctx.guild.id), str(ctx.author.id))
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
错误
文件 “ /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py”, _run_event中的第312行 等待coro(* args,** kwargs)文件“ /Users/CIP/Documents/GitHub/economia/cogs/dinheiro.py”,第56行, on_message db.commit()sqlite3.OperationalError:数据库已锁定