【问题标题】:discord.py commands.when_mentioned not working with custom prefixdiscord.py commands.when_提到不使用自定义前缀
【发布时间】:2021-03-30 21:00:26
【问题描述】:

我正在尝试做的事情:如果有人不知道前缀,他们可以提及机器人并改用提及。经过一番研究,我发现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
    )

下面我列出了我尝试过的内容。我不确定接下来要尝试什么,因此我将不胜感激提供的任何帮助。

试验一:

command_prefix= commands.when_mentioned_or((get_prefix))

结果:

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

试炼二:

command_prefix= commands.when_mentioned or (get_prefix)

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

试验 3:

command_prefix= commands.when_mentioned and (get_prefix)

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

【问题讨论】:

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


    【解决方案1】:

    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
    

    【讨论】:

    • 所以 command_prefix=when_提到_or_function(get_prefix)?或 when_提到_or_function(commands.when_提到_or(get_prefix))
    • @StatTrakDiamondSword 第一个
    • 我查看了 readthedocs 上的示例,它实际上非常简单。在 get_prefix 函数中,您返回 commands.when_mentioned_or(prefix)(bot, message)
    猜你喜欢
    • 2021-03-31
    • 2021-04-04
    • 2021-11-01
    • 2021-07-19
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多