Search code examples
pythonpyrogram

Pyrogram Userbot command filter doesn't work for valid command


I'm trying to make a Userbot with some functions that only I can call via commands. However, the command filter doesn't fire when I input command. Here's the code for the command handler:

@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)

And the code for the cmd_filter function:

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))

For some reason print statement in my CMD filter doesn't work.(if it helps current_config.command_symbol is ["/","."]).

What is the reason of this issue and how can I fix this?

I tried to make my own regex to solve this problem, but i wasn't sure how to correctly escape any specials symbols:

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)

Also, i tried to call my userbot account with /start@userbot which didn't help either.

Also if it helps: any non-command handlers work as expected, for example I have a handler that is being called on any message that starts with specific symbols and it fires. It's in separate group, and event commenting this handler doesn't solve a problem.


Solution

  • The problem was happening because I had multiple commands with the same function name. Here's the incorrect variant:

    @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)
    

    And here is a variant with corrected function names which is calling correctly when I use the command:

    @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)