Skip to content

Commit 41f16a1

Browse files
committed
Fix filters.command not working with multiple running bots
Closes #864
1 parent 78efb04 commit 41f16a1

3 files changed

Lines changed: 5 additions & 11 deletions

File tree

pyrogram/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ def __init__(
286286

287287
self.disconnect_handler = None
288288

289+
# Username used for mentioned bot commands, e.g.: /start@usernamebot
290+
self.username = None
291+
289292
self.loop = asyncio.get_event_loop()
290293

291294
def __enter__(self):

pyrogram/filters.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,6 @@ async def linked_channel_filter(_, __, m: Message):
745745

746746

747747
# region command_filter
748-
749-
# Used by the command filter below
750-
username = None
751-
752-
753748
def command(commands: Union[str, List[str]], prefixes: Union[str, List[str]] = "/", case_sensitive: bool = False):
754749
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
755750
@@ -772,12 +767,7 @@ def command(commands: Union[str, List[str]], prefixes: Union[str, List[str]] = "
772767
command_re = re.compile(r"([\"'])(.*?)(?<!\\)\1|(\S+)")
773768

774769
async def func(flt, client: pyrogram.Client, message: Message):
775-
# Username shared among all commands; used for mention commands, e.g.: /start@username
776-
global username
777-
778-
if username is None:
779-
username = (await client.get_me()).username or ""
780-
770+
username = client.username or ""
781771
text = message.text or message.caption
782772
message.command = None
783773

pyrogram/methods/auth/initialize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ async def initialize(
4848
await self.dispatcher.start()
4949
await Syncer.add(self)
5050

51+
self.username = (await self.get_me()).username
5152
self.is_initialized = True

0 commit comments

Comments
 (0)