command filter now can be sets with BotCommand object instead of or together with plain strings

This commit is contained in:
Yuriy Chebyshev 2022-06-24 13:30:49 +03:00
parent df23d4fd59
commit 23568d647d

View file

@ -10,7 +10,7 @@ from babel.support import LazyProxy
from aiogram import types from aiogram import types
from aiogram.dispatcher.filters.filters import BoundFilter, Filter from aiogram.dispatcher.filters.filters import BoundFilter, Filter
from aiogram.types import CallbackQuery, ChatType, InlineQuery, Message, Poll, ChatMemberUpdated from aiogram.types import CallbackQuery, ChatType, InlineQuery, Message, Poll, ChatMemberUpdated, BotCommand
ChatIDArgumentType = typing.Union[typing.Iterable[typing.Union[int, str]], str, int] ChatIDArgumentType = typing.Union[typing.Iterable[typing.Union[int, str]], str, int]
@ -34,7 +34,7 @@ class Command(Filter):
By default this filter is registered for messages and edited messages handlers. By default this filter is registered for messages and edited messages handlers.
""" """
def __init__(self, commands: Union[Iterable, str], def __init__(self, commands: Union[Iterable[str], Iterable[BotCommand], str, BotCommand],
prefixes: Union[Iterable, str] = '/', prefixes: Union[Iterable, str] = '/',
ignore_case: bool = True, ignore_case: bool = True,
ignore_mention: bool = False, ignore_mention: bool = False,
@ -66,8 +66,10 @@ class Command(Filter):
@dp.message_handler(commands=['myCommand'], commands_ignore_caption=False, content_types=ContentType.ANY) @dp.message_handler(commands=['myCommand'], commands_ignore_caption=False, content_types=ContentType.ANY)
@dp.message_handler(Command(['myCommand'], ignore_caption=False), content_types=[ContentType.TEXT, ContentType.DOCUMENT]) @dp.message_handler(Command(['myCommand'], ignore_caption=False), content_types=[ContentType.TEXT, ContentType.DOCUMENT])
""" """
if isinstance(commands, str): if isinstance(commands, str) or isinstance(commands, BotCommand):
commands = (commands,) commands = (commands,)
if isinstance(commands, Iterable):
commands = [cmd.command if isinstance(cmd, BotCommand) else cmd for cmd in commands]
self.commands = list(map(str.lower, commands)) if ignore_case else commands self.commands = list(map(str.lower, commands)) if ignore_case else commands
self.prefixes = prefixes self.prefixes = prefixes