mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Fix IDFilter behavior when single str id passed
This commit is contained in:
parent
61cfb8e99a
commit
60bcd88b68
1 changed files with 21 additions and 13 deletions
|
|
@ -12,6 +12,19 @@ from aiogram.dispatcher.filters.filters import BoundFilter, Filter
|
|||
from aiogram.types import CallbackQuery, Message, InlineQuery, Poll, ChatType
|
||||
|
||||
|
||||
IDFilterArgumentType = typing.Union[typing.Iterable[typing.Union[int, str]], str, int]
|
||||
|
||||
|
||||
def extract_filter_ids(id_filter_argument: IDFilterArgumentType) -> typing.List[int]:
|
||||
# since "str" is also an "Iterable", we have to check for it first
|
||||
if isinstance(id_filter_argument, str):
|
||||
return [int(id_filter_argument)]
|
||||
if isinstance(id_filter_argument, Iterable):
|
||||
return [int(item) for (item) in id_filter_argument]
|
||||
# the last possible type is a single "int"
|
||||
return [id_filter_argument]
|
||||
|
||||
|
||||
class Command(Filter):
|
||||
"""
|
||||
You can handle commands by using this filter.
|
||||
|
|
@ -543,12 +556,11 @@ class ExceptionsFilter(BoundFilter):
|
|||
except:
|
||||
return False
|
||||
|
||||
|
||||
class IDFilter(Filter):
|
||||
|
||||
def __init__(self,
|
||||
user_id: Optional[Union[Iterable[Union[int, str]], str, int]] = None,
|
||||
chat_id: Optional[Union[Iterable[Union[int, str]], str, int]] = None,
|
||||
user_id: Optional[IDFilterArgumentType] = None,
|
||||
chat_id: Optional[IDFilterArgumentType] = None,
|
||||
):
|
||||
"""
|
||||
:param user_id:
|
||||
|
|
@ -557,18 +569,14 @@ class IDFilter(Filter):
|
|||
if user_id is None and chat_id is None:
|
||||
raise ValueError("Both user_id and chat_id can't be None")
|
||||
|
||||
self.user_id = None
|
||||
self.chat_id = None
|
||||
self.user_id: Optional[IDFilterArgumentType] = None
|
||||
self.chat_id: Optional[IDFilterArgumentType] = None
|
||||
|
||||
if user_id:
|
||||
if isinstance(user_id, Iterable):
|
||||
self.user_id = list(map(int, user_id))
|
||||
else:
|
||||
self.user_id = [int(user_id), ]
|
||||
self.user_id = extract_filter_ids(user_id)
|
||||
|
||||
if chat_id:
|
||||
if isinstance(chat_id, Iterable):
|
||||
self.chat_id = list(map(int, chat_id))
|
||||
else:
|
||||
self.chat_id = [int(chat_id), ]
|
||||
self.chat_id = extract_filter_ids(chat_id)
|
||||
|
||||
@classmethod
|
||||
def validate(cls, full_config: typing.Dict[str, typing.Any]) -> typing.Optional[typing.Dict[str, typing.Any]]:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue