【问题标题】:Discord.py: can only concatenate tuple (not "int") to tupleDiscord.py:只能将元组(不是“int”)连接到元组
【发布时间】: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


【解决方案1】:

试试这个:

int(result[0]) + 1

您的打印输出表明result('1',),即长度为1 的tuple 包含一个字符串。 [0] 获取元组中的第一个元素,int() 将字符串转换为数字,因此您可以向其添加数字。

您也可以在 Python 中对字符串或元组使用 +,但两个操作数必须是相同的类型。 (它将它们连接起来。)这就是错误消息抱怨操作数而不是 + 运算符的原因。

【讨论】:

  • @Dom:furas 提出了一个很好的观点,即不能保证结果的长度为 1,因为这取决于您的数据。如果长度为零,则索引[0] 将崩溃,如果长度大于 1,则其余结果将被静默忽略。
  • 好的@gilch。我会看看是否可以找到替代/解决方法,感谢您告诉我!
【解决方案2】:

cursor.fetchone() 总是给出元组(类似于列表),即使数据库返回单个值,所以你必须从元组中获取第一个元素

 result = result[0] 

最好在检查 result 是否不是 `None

if result is not None:
     val = (..., int(result[0]) + 1)

@gilch 注意到它还需要 int() 将字符串 '1' 转换为整数 1

【讨论】:

  • 执行此操作后,我得到TypeError: can only concatenate str (not "int") to str
  • 结果有字符串'1',您必须使用int()将其转换为整数1
猜你喜欢
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多