NetherMoon
NetherMoon

Reputation: 331

TypeError: Iterable command_prefix or list returned from get_prefix must contain only strings, not Cursor | Discord.py

I was implementing custom prefix usage with my sqlite3 db. I recieve this error whenever I try using the prefix:

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

The line of code that it gets an error with is this:

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)

I have tried doing things like this but then the bot does not respond to the prefix:

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)

And this:

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)

And this:

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)

Thanks!!!

Upvotes: 1

Views: 573

Answers (1)

Diggy.
Diggy.

Reputation: 6944

Cursor.execute() returns a cursor object. To actually fetch results from the cursor, you will need to add the cursor.fetchone() or cursor.fetchall() methods.

fetchone() will return the result in a tuple, so you'd need to extract the prefix from said tuple.

fetchall() will return a list of tuples.

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

References:

Upvotes: 1

Related Questions