为什么我的前缀在 discord.py 中出现此错误

Why I've this error with my prefix in discord.py

我最近开始在python开发一个discord bot,有一个我不明白的错误

我想要的:我希望能够将机器人的提及和从 JSON 文件中提取的前缀作为前缀

我的问题:我有这个错误:

Traceback (most recent call last):
  File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 942, in on_message
    await self.process_commands(message)
  File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 938, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 875, 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 function

这是我的代码:

def get_prefix(client, message):
    with open(prefix_file, 'r') as f:
        prefixes = json.load(f)
    pref_fin =  prefixes[str(message.guild.id)]
    return pref_fin
 

client = commands.Bot(command_prefix = when_mentioned_or(get_prefix), description = "Bot by Cucus#0001",intents=intents)

我不明白,因为我的函数 get_prefix() return 一个字符串

你能帮帮我吗?

您错误地使用了when_mentioned_or函数。

async def get_prefix(client, message):
    with open(prefix_file, 'r') as f:
        prefixes = json.load(f)
    pref_fin = prefixes[str(message.guild.id)]
    return when_mentioned_or(pref_fin)(client, message)

然后使用就可以使用了:

client = commands.Bot(command_prefix=get_prefix, description="Bot by Cucus#0001",intents=intents)