async def get_prefix(bot, message):
if isinstance(message.channel, discord.DMChannel):
return commands.when_mentioned_or(DEFAULT_PREFIX)
prefix = await bot.db.fetch('SELECT prefix FROM guilds WHERE "guild_id" = $1', message.guild.id)
if len(prefix) == 0:
await bot.db.execute('INSERT INTO guilds ("guild_id", prefix) VALUES ($1, $2)', message.guild.id, DEFAULT_PREFIX)
prefix = DEFAULT_PREFIX
else:
prefix = prefix[0].get("prefix")
return commands.when_mentioned_or(prefix)(bot, message)
class DiseaseBot(commands.AutoShardedBot):
def __init__(self):
super().__init__(
command_prefix = get_prefix,
case_insensitive = True,
intents=discord.Intents.all(),
help_command= None
)
async def create_db_pool(self):
self.db = await asyncpg.create_pool(database= "DiseasesProject", user="postgres", password= DATABASE_PASSWORD)
print("Database connection successful!")
#The `get_prefix` went wrong when use in DMChannel
1 messages · Page 1 of 1 (latest)
The error says
Traceback (most recent call last):
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 231, in get_prefix
ret = list(ret)
TypeError: 'function' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\client.py", line 382, in _run_event
await coro(*args, **kwargs)
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 382, in on_message
await self.process_commands(message)
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 378, in process_commands
ctx = await self.get_context(message)
File "C:\Users\ACER\Documents\covidbot\bot.py", line 26, in get_context
return await super().get_context(message, cls=cls)
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 284, in get_context
prefix = await self.get_prefix(message)
File "C:\Users\ACER\Documents\covidbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 238, in get_prefix
raise TypeError(
TypeError: command_prefix must be plain string, iterable of strings, or callable returning either of these, not function
It works fine when I use in guild but in DM not.
Can you explain more? because i think that my function return a callable function...
the note already shows an example
This callable returns another callable, so if this is done inside a custom callable, you must call the returned callable, for example:
async def get_prefix(bot, message):
extras = await prefixes_for(message.guild) # returns a list
return commands.when_mentioned_or(*extras)(bot, message)```
oh okay
So from this one, it supposes to be
return commands.when_mentioned_or(DEFAULT_PREFIX)(bot, message)``` right?
yes, you need to do another call with (bot, message)