Pyrogram 用户机器人命令过滤器对有效命令无效

0 投票
1 回答
36 浏览
提问于 2025-04-14 18:29

我正在尝试制作一个用户机器人,里面有一些只有我可以通过命令调用的功能。不过,当我输入命令时,命令过滤器并没有起作用。以下是命令处理器的代码:

@Client.on_message(cmd_filter("workhere", "work", "starthere", "start"))
async def workhere(client, message):
    print("The start command has been fired")
    current_config.current_targets = add_unique(current_config.current_targets,message.chat.id)

这是 cmd_filter 函数的代码:

from pyrogram import filters
from settings import current_config
from typing import Union, List


def cmd_filter(*text: List[str]):
    return filters.outgoing & filters.command(list(text), list(current_config.command_symbol))

不知道为什么我在命令过滤器里的打印语句没有工作。(如果有帮助的话,current_config.command_symbol["/","."])。

这个问题的原因是什么,我该如何解决呢?

我尝试自己写正则表达式来解决这个问题,但我不太确定如何正确处理特殊符号:

if len(text)>1:
        joined_commands =  "".join(["(","|".join(text),")"])
    else:
        joined_commands = text[0]
        
    escaped_commands = ["\\"+sym for sym in current_config.command_symbol]
    if len(escaped_commands)>1:
        joined_symbols = "".join(["(","|".join(escaped_commands),")"])
    else:
        joined_symbols = escaped_commands[0]
    return filters.me & filters.regex("^"+joined_symbols+joined_commands)

另外,我尝试用 /start@userbot 来调用我的 用户机器人 账户,但也没有帮助。

还有,如果有帮助的话:任何非命令处理器都能正常工作,比如我有一个处理器会在任何以特定符号开头的消息上被调用,它是可以正常工作的。这个处理器在一个单独的组里,即使注释掉这个处理器也没有解决问题。

1 个回答

1

这个问题出现是因为我有多个命令使用了相同的函数名称。下面是错误的版本:

@Client.on_message(cmd_filter("workhere", "work", "starthere", "start"))
async def workhere(client, message):
    print("The start command has been fired")
    current_config.current_targets = add_unique(current_config.current_targets,message.chat.id)


@Client.on_message(cmd_filter("stophere", "stop", "finish"))
async def workhere(client, message):
    print("Finished work in this chat")
    current_config.current_targets = current_config.current_targets.remove(message.chat.id)

而这里是一个修正过的版本,函数名称不同,这样在我使用命令时就能正确调用了:

@Client.on_message(cmd_filter("workhere", "work", "starthere", "start"))
async def workhere(client, message):
    print("The start command has been fired")
    current_config.current_targets = add_unique(current_config.current_targets,message.chat.id)


@Client.on_message(cmd_filter("stophere", "stop", "finish"))
async def stophere(client, message):
    print("Finished work in this chat")
    current_config.current_targets = current_config.current_targets.remove(message.chat.id)

撰写回答