【问题标题】:Why I've this error with my prefix in discord.py [closed]为什么我在 discord.py 中的前缀出现此错误 [关闭]
【发布时间】:2022-01-09 04:55:55
【问题描述】:

我最近开始用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() 返回一个字符串????

你能帮帮我吗?

【问题讨论】:

  • 请发布full traceback - 它们包含许多有价值的信息,对调试至关重要。
  • 另外,请参阅本指南以创建 minimal reproducible example。为了调试,我们需要一个包含数据和所有导入/定义的完整示例。在生成这样的示例时,请尝试将问题缩小到确切的问题并去除不必要的细节,同时确保代码仍然完整且可执行。
  • 您将函数 get_prefix 作为参数提供了您可能的意思是 get_prefix(client, message)

标签: python discord discord.py prefix


【解决方案1】:

您错误地使用了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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2013-01-18
    • 2021-02-07
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多