discord.py commands.when_提到不适用于自定义前缀

时间:2020-12-21 02:49:49

标签: python discord.py discord.py-rewrite

我想做什么:如果有人不知道前缀,他们可以提及机器人并使用提及来代替。经过一番研究,我发现 How to send commands to a bot by name? 这让我想尝试将 commands.when_mentionedcommands.when_mentioned_or 函数与我的自定义前缀一起使用。

我的问题:机器人要么只响应提及(同时向我抛出错误),要么根本不响应。

这是我使用的自定义前缀代码:How to get a customizable prefix discord.py

这是带有 command_prefix 的客户端定义:

intents = discord.Intents.all()
client = commands.Bot(
    command_prefix= (get_prefix),
    description='A bot who wants your toes',
    owner_id=(394506589350002688),
    case_insensitive=True,
    intents=intents
    )

下面我列出了我尝试过的方法。我不确定接下来要尝试什么,因此我将非常感谢您提供的任何帮助。

试验 1:

command_prefix= commands.when_mentioned_or((get_prefix))

结果:

TypeError: Iterable command_prefix or list returned from get_prefix must contain only strings, not function

试验 2:

command_prefix= commands.when_mentioned or (get_prefix)

结果:没有错误,但机器人不再响应自定义前缀,如下所示。

Bot does not respond to bl!

试验 3:

command_prefix= commands.when_mentioned and (get_prefix)

结果:没有错误,但机器人不再响应提及,如下所示。

Bot no longer responds to mention

1 个答案:

答案 0 :(得分:2)

when_mentioned_or 应该传递一个前缀列表,而不是获取该列表的函数。不过修改起来很容易:

def when_mentioned_or_function(func):
    def inner(bot, message):
        r = func(bot, message)
        r = commands.when_mentioned(bot, msg) + r
        return r
    return inner