【发布时间】:2020-12-23 04:48:09
【问题描述】:
我正在尝试为我的不和谐机器人创建一个警告命令,该命令将以下 4 个值输入数据库、成员 ID、服务器 ID、原因和警告 ID。警告 ID 是最后一次提取到表中的结果加 1。但是,当我尝试添加到警告 ID 时,出现以下错误。
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "main.py", line 47, in on_command_error
raise error
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1325, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/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: can only concatenate tuple (not "int") to tuple
这是我的代码:
@commands.group()
@commands.has_permissions(manage_roles = True)
@commands.bot_has_permissions(manage_roles = True)
async def warn(self, ctx, member: discord.Member, *, reason = 'No reason provided'):
if ctx.invoked_subcommand is not None:
return
db = sqlite3.connect('warns.sqlite')
cursor = db.cursor()
cursor.execute(f'SELECT warn_id FROM warns WHERE guild_id = {ctx.guild.id} AND member_id = {member.id}')
result = cursor.fetchone()
if result is None:
sql = ('INSERT INTO warns(guild_id, member_id, reason, warn_id) VALUES(?,?,?,?)')
val = (ctx.guild.id, member.id, reason, 1)
elif result is not None:
sql = ('INSERT INTO warns(guild_id, member_id, reason, warn_id) VALUES(?,?,?,?)')
val = (ctx.guild.id, member.id, reason, result + 1) # FAILS HERE
await ctx.send(f'{check_mark} **{member.name}** has been warned.')
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
我尝试将结果转换为整数,但没有成功。我不知道为什么现在会这样。
【问题讨论】:
-
您是否尝试在行中打印出
result,以免它无法查看是否符合您的预期? -
它打印了
('1',)。我怎样才能把它变成一个数字,以便我可以添加一个整数? -
cursor.fetchone()总是给出元组,即使数据库返回单个值,所以你必须在检查if result is not None后得到result = result[0]或更好@使用它val = (..., result[0] + 1) -
看起来你需要通过索引来访问元组中的值
标签: python database sqlite discord.py discord.py-rewrite