python - 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)
谢谢!!!
解决方案
Cursor.execute()
返回一个游标对象。要实际从游标中获取结果,您需要添加cursor.fetchone()
orcursor.fetchall()
方法。
fetchone()
将以元组的形式返回结果,因此您需要从所述元组中提取前缀。
fetchall()
将返回一个元组列表。
...
prefix = cursor.fetchone()[0]
参考资料:
推荐阅读
- python - 如何在pytorch中钳制神经元的输出
- python - 尝试连接到本地 mysql 数据库但收到以下错误 pwwee
- r - 在闪亮中使用 selectInput 绘制不同的回归线
- javascript - 存储卡游戏点击事件无法在移动设备上正常运行,但在计算机上运行良好
- c# - 如何使用 websocket ReceiveAsync(内存
) 和内存池 - typescript - 如何使用 const 箭头函数在 TypeScript 中实例化泛型函数类型?
- php - 当我从 php.ini 的下拉列表中选择时,我得到空值。有时,它会显示诸如“未定义索引”之类的错误
- javascript - 如何使用 javascript 添加元素类?
- python - TypeError: forward() 接受 2 个位置参数,但 3 个在 pytorch 中给出
- javascript - 使用 axios 和 async await 循环请求的正确方法是什么?