Discord.py:只能将元组(不是"int")连接到元组
Discord.py: can only concatenate tuple (not "int") to tuple
我正在尝试为我的 discord 机器人发出警告命令,将以下 4 个值输入数据库、成员 ID、服务器 ID、原因和警告 ID。警告 ID 是最后一次提取到 table 的结果加 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()
我试过将结果转换为整数,但没有成功。我不知道为什么现在会这样。
试试这个:
int(result[0]) + 1
您的打印输出表明 result
是 ('1',)
,这是一个包含字符串的长度为 1 的 tuple
。 [0]
从元组中获取第一个元素,int()
将字符串转换为数字,因此您可以向其添加一个数字。
您还可以在 Python 中的字符串或元组上使用 +
,但两个操作数必须是同一类型。 (它将它们连接起来。)这就是错误消息抱怨操作数而不是 +
运算符的原因。
cursor.fetchone()
总是给出元组(类似于列表),即使数据库 returns 是单个值,所以你必须从元组
中获取第一个元素
result = result[0]
最好先检查一下 result
是否不是 `None
if result is not None:
val = (..., int(result[0]) + 1)
正如@gilch 所注意到的,它还需要 int()
将字符串 '1'
转换为整数 1
我正在尝试为我的 discord 机器人发出警告命令,将以下 4 个值输入数据库、成员 ID、服务器 ID、原因和警告 ID。警告 ID 是最后一次提取到 table 的结果加 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()
我试过将结果转换为整数,但没有成功。我不知道为什么现在会这样。
试试这个:
int(result[0]) + 1
您的打印输出表明 result
是 ('1',)
,这是一个包含字符串的长度为 1 的 tuple
。 [0]
从元组中获取第一个元素,int()
将字符串转换为数字,因此您可以向其添加一个数字。
您还可以在 Python 中的字符串或元组上使用 +
,但两个操作数必须是同一类型。 (它将它们连接起来。)这就是错误消息抱怨操作数而不是 +
运算符的原因。
cursor.fetchone()
总是给出元组(类似于列表),即使数据库 returns 是单个值,所以你必须从元组
result = result[0]
最好先检查一下 result
是否不是 `None
if result is not None:
val = (..., int(result[0]) + 1)
正如@gilch 所注意到的,它还需要 int()
将字符串 '1'
转换为整数 1