Repository: python-telegram-bot/python-telegram-bot

Issue:
CommandHandler should not check filters if not listening to the command
I have a filter to restrict some users from call some commands. I call `AdminFilter` and the relevant code can be seen below (actually, I check the admin users from a database).

```python
class AdminFilter(BaseFilter):
    def __init__(self, *admin_users):
        self.admin_users = admin_users

    def filter(self, message):
        if message.from_user.username in self.admin_users:
            return True
        else:
            message.reply_text("You're not an admin!", quote=True)
            return False
```

With this filter applied, when an user that is not an admin calls the forbidden command, then receives a reply warning. And here is the issue.

### Steps to reproduce
The following code illustrate the problem. In this scenario I have an administrative command `/jungle` and a normal command `/rain`.

If I am the admin, I call any command and see no issue, but If am not admin, I receive the warning for both commands.

```python
def jungle(bot, update):
    bot.send_message(update.message.chat_id, 'Welcome to the Jungle!')

def rain(bot, update):
    bot.send_message(update.message.chat_id, 'November Rain...')

dispatcher.add_handler(CommandHandler('jungle', jungle, AdminFilter('wagnermacedo')))
dispatcher.add_handler(CommandHandler('rain', rain))
```

### Expected behaviour
Suppose I'm not the bot administrator, then in a conversation with the bot, I expect the following:

```
me: /jungle
bot: > reply /jungle
       You're not an admin!
```
```
me: /rain
bot: November Rain...
```

### Actual behaviour
What happens instead is that bot reply that I'm not an admin for both commands.

For `/jungle`, it's okay, it was expected
```
me: /jungle
bot: > reply /jungle
       You're not an admin!
```

But `/rain` hasn't the admin filter, so it should not reply the warning.
```
me: /rain
bot: > reply /rain
       You're not an admin!
bot: November Rain...
```

:point_up_2: Surprisingly the command still works, even replying I'm not admin...

### Configuration
**Version of Python, python-telegram-bot & dependencies:**

I tested with the last version from master (59659ea).

```
$ python -m telegram
python-telegram-bot 10.0.1
certifi 2018.01.18
future 0.16.0
Python 3.4.8 (default, Apr 13 2018, 16:18:01)  [GCC 5.4.0 20160609]
```

### Fix
I have already fixed the issue in my machine, I'm going soon to create a pull request.



Discussion:

Resolving patch:
diff --git a/telegram/ext/commandhandler.py b/telegram/ext/commandhandler.py
--- a/telegram/ext/commandhandler.py
+++ b/telegram/ext/commandhandler.py
@@ -140,6 +140,10 @@ def check_update(self, update):
                     command.append(
                         message.bot.username)  # in case the command was sent without a username
 
+                    if not (command[0].lower() in self.command
+                            and command[1].lower() == message.bot.username.lower()):
+                        return False
+
                     if self.filters is None:
                         res = True
                     elif isinstance(self.filters, list):
@@ -147,8 +151,7 @@ def check_update(self, update):
                     else:
                         res = self.filters(message)
 
-                    return res and (command[0].lower() in self.command
-                                    and command[1].lower() == message.bot.username.lower())
+                    return res
 
         return False
 


Test patch:
diff --git a/tests/test_commandhandler.py b/tests/test_commandhandler.py
--- a/tests/test_commandhandler.py
+++ b/tests/test_commandhandler.py
@@ -21,7 +21,7 @@
 
 from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery,
                       ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
-from telegram.ext import CommandHandler, Filters
+from telegram.ext import CommandHandler, Filters, BaseFilter
 
 message = Message(1, User(1, '', False), None, Chat(1, ''), text='test')
 
@@ -246,3 +246,22 @@ def test_pass_job_or_update_queue(self, dp, message):
     def test_other_update_types(self, false_update):
         handler = CommandHandler('test', self.callback_basic)
         assert not handler.check_update(false_update)
+
+    def test_filters_for_wrong_command(self, message):
+        """Filters should not be executed if the command does not match the handler"""
+
+        class TestFilter(BaseFilter):
+            def __init__(self):
+                self.tested = False
+
+            def filter(self, message):
+                self.tested = True
+
+        test_filter = TestFilter()
+
+        handler = CommandHandler('foo', self.callback_basic, filters=test_filter)
+        message.text = '/bar'
+
+        handler.check_update(Update(0, message=message))
+
+        assert not test_filter.tested