首页 > 解决方案 > TypeError: Iterable command_prefix 或从 get_prefix 返回的列表必须只包含字符串,而不是 Cursor | 不和谐.py

问题描述

我正在使用我的 sqlite3 数据库实现自定义前缀使用。每当我尝试使用前缀时,我都会收到此错误:

Traceback (most recent call last):
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 943, in on_message
    await self.process_commands(message)
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 939, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 876, in get_context
    raise TypeError("Iterable command_prefix or list returned from get_prefix must "
TypeError: Iterable command_prefix or list returned from get_prefix must contain only strings, not Cursor

它出错的代码行是这样的:

def get_prefix(bot, message):
    prefix = cursor.execute(f"SELECT prefix FROM guilds WHERE serverid = {message.guild.id}")
    return when_mentioned_or(current_prefix)(bot, message)

我试过做这样的事情,但机器人不响应前缀:

def get_prefix(bot, message):
    prefix = cursor.execute(f"SELECT prefix FROM guilds WHERE serverid = {message.guild.id}")
    prefix = str(prefix)
    return when_mentioned_or(current_prefix)(bot, message)

和这个:

def get_prefix(bot, message):
    prefix = cursor.execute(f"SELECT prefix FROM guilds WHERE serverid = {message.guild.id}")
    return when_mentioned_or(str(current_prefix))(bot, message)

和这个:

def get_prefix(bot, message):
    prefix = cursor.execute(f"SELECT prefix FROM guilds WHERE serverid = {message.guild.id}")
    return when_mentioned_or(f"{current_prefix}")(bot, message)

谢谢!!!

标签: pythonsqlitediscorddiscord.pydiscord.py-rewrite

解决方案


Cursor.execute()返回一个游标对象。要实际从游标中获取结果,您需要添加cursor.fetchone()orcursor.fetchall()方法。

fetchone()将以元组的形式返回结果,因此您需要从所述元组中提取前缀。

fetchall()将返回一个元组列表。

...
prefix = cursor.fetchone()[0]

参考资料


推荐阅读