Skip to content

Commit ca9068f

Browse files
committed
Simplified if statement, fixed docs
1 parent 3bf4399 commit ca9068f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

telegram/ext/filters.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,23 @@ def filter(self, message):
325325
group = _Group()
326326

327327
class user(BaseFilter):
328-
"""Filters messages to allow only those which are from specified user ID
328+
"""Filters messages to allow only those which are from specified user ID.
329+
330+
Notes:
331+
Only one of chat_id or username must be used here.
329332
330333
Args:
331-
user_id(Optional[int]): which user ID to allow through. Required if username is not
332-
specified
333-
username(Optional[str]): which username to allow through. Required if user_id is not
334-
specified
334+
user_id(Optional[int]): which user ID to allow through.
335+
username(Optional[str]): which username to allow through. If username starts with '@'
336+
symbol, it will be ignored.
335337
336338
Raises:
337339
ValueError
338340
"""
339341

340342
def __init__(self, user_id=None, username=None):
341-
if (user_id is None and username is None) or (bool(user_id) and bool(username)):
342-
raise ValueError('You must specify either user_id or username')
343+
if not (bool(user_id) ^ bool(username)):
344+
raise ValueError('One and only one of user_id or username must be used')
343345
if username is not None and username.startswith('@'):
344346
self.username = username[1:]
345347
else:
@@ -355,21 +357,23 @@ def filter(self, message):
355357
message.from_user.username == self.username)
356358

357359
class chat(BaseFilter):
358-
"""Filters messages to allow only those which are from specified chat ID
360+
"""Filters messages to allow only those which are from specified chat ID.
361+
362+
Notes:
363+
Only one of chat_id or username must be used here.
359364
360365
Args:
361-
chat_id(Optional[int]): which chat ID to allow through. Required if username is not
362-
specified
363-
username(Optional[str]): which username to allow through. Required if chat_id is not
364-
specified
366+
chat_id(Optional[int]): which chat ID to allow through.
367+
username(Optional[str]): which username to allow through. If username starts with '@'
368+
symbol, it will be ignored.
365369
366370
Raises:
367371
ValueError
368372
"""
369373

370374
def __init__(self, chat_id=None, username=None):
371-
if (chat_id is None and username is None) or (bool(chat_id) and bool(username)):
372-
raise ValueError('You must specify either chat_id or username')
375+
if not (bool(chat_id) ^ bool(username)):
376+
raise ValueError('One and only one of user_id or username must be used')
373377
if username is not None and username.startswith('@'):
374378
self.username = username[1:]
375379
else:

0 commit comments

Comments
 (0)