Переделана основа для фильтров Command, CommandStart
This commit is contained in:
parent
cb2226eee5
commit
036c92d072
116
maxapi/filters/command.py
Normal file
116
maxapi/filters/command.py
Normal file
@ -0,0 +1,116 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
from ..types.updates import UpdateUnion
|
||||
from ..filters.filter import BaseFilter
|
||||
|
||||
from ..types.updates.message_created import MessageCreated
|
||||
|
||||
|
||||
class Command(BaseFilter):
|
||||
|
||||
"""
|
||||
Фильтр сообщений на соответствие команде.
|
||||
|
||||
Args:
|
||||
commands (str | list[str]): Ожидаемая команда или список команд без префикса.
|
||||
prefix (str, optional): Префикс команды (по умолчанию '/').
|
||||
check_case (bool, optional): Учитывать регистр при сравнении (по умолчанию False).
|
||||
|
||||
Attributes:
|
||||
commands (list[str]): Список команд без префикса.
|
||||
prefix (str): Префикс команды.
|
||||
check_case (bool): Флаг чувствительности к регистру.
|
||||
"""
|
||||
|
||||
def __init__(self, commands: str | List[str], prefix: str = '/', check_case: bool = False):
|
||||
|
||||
"""
|
||||
Инициализация фильтра команд.
|
||||
"""
|
||||
|
||||
if isinstance(commands, str):
|
||||
self.commands = [commands]
|
||||
else:
|
||||
self.commands = commands
|
||||
|
||||
self.prefix = prefix
|
||||
self.check_case = check_case
|
||||
|
||||
if not check_case:
|
||||
self.commands = [cmd.lower() for cmd in self.commands]
|
||||
|
||||
def parse_command(self, text: str) -> Tuple[str, List[str]]:
|
||||
|
||||
"""
|
||||
Извлекает команду из текста.
|
||||
|
||||
Args:
|
||||
text (str): Текст сообщения.
|
||||
|
||||
Returns:
|
||||
Optional[str]: Найденная команда с префиксом, либо None.
|
||||
"""
|
||||
|
||||
args = text.split()
|
||||
first = args[0]
|
||||
|
||||
if not first.startswith(self.prefix):
|
||||
return '', []
|
||||
|
||||
return first[len(self.prefix):], args
|
||||
|
||||
async def __call__(self, event: UpdateUnion):
|
||||
|
||||
"""
|
||||
Проверяет, соответствует ли сообщение заданной(ым) команде(ам).
|
||||
|
||||
Args:
|
||||
event (MessageCreated): Событие сообщения.
|
||||
|
||||
Returns:
|
||||
bool: True, если команда совпадает, иначе False.
|
||||
"""
|
||||
|
||||
if not isinstance(event, MessageCreated):
|
||||
return False
|
||||
|
||||
text = event.message.body.text
|
||||
|
||||
if not text:
|
||||
return False
|
||||
|
||||
parsed_command, args = self.parse_command(text)
|
||||
if not parsed_command:
|
||||
return False
|
||||
|
||||
if not self.check_case:
|
||||
if parsed_command.lower() in [commands.lower() for commands in self.commands]:
|
||||
return {'args': args}
|
||||
else:
|
||||
return False
|
||||
|
||||
if parsed_command in self.commands:
|
||||
return {'args': args}
|
||||
|
||||
return False
|
||||
|
||||
|
||||
class CommandStart(Command):
|
||||
|
||||
"""
|
||||
Фильтр для команды /start.
|
||||
|
||||
Args:
|
||||
prefix (str, optional): Префикс команды (по умолчанию '/').
|
||||
check_case (bool, optional): Учитывать регистр (по умолчанию False).
|
||||
"""
|
||||
|
||||
def __init__(self, prefix = '/', check_case = False):
|
||||
super().__init__(
|
||||
'start',
|
||||
prefix,
|
||||
check_case
|
||||
)
|
||||
|
||||
async def __call__(self, event):
|
||||
return await super().__call__(event)
|
@ -31,7 +31,8 @@ from ..types.attachments.buttons.message_button import MessageButton
|
||||
from ..types.attachments.image import PhotoAttachmentRequestPayload
|
||||
from ..types.message import Message, NewMessageLink
|
||||
|
||||
from ..types.command import Command, BotCommand, CommandStart
|
||||
from ..filters.command import Command, CommandStart
|
||||
from ..types.command import BotCommand
|
||||
|
||||
from .input_media import InputMedia
|
||||
from .input_media import InputMediaBuffer
|
||||
|
@ -1,32 +1,5 @@
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Command:
|
||||
|
||||
"""
|
||||
Класс для представления команды бота.
|
||||
|
||||
Attributes:
|
||||
text (str): Текст команды без префикса.
|
||||
prefix (str): Префикс команды. По умолчанию '/'.
|
||||
"""
|
||||
|
||||
def __init__(self, text: str, prefix: str = '/'):
|
||||
self.text = text
|
||||
self.prefix = prefix
|
||||
|
||||
@property
|
||||
def command(self):
|
||||
|
||||
"""
|
||||
Возвращает полную команду с префиксом.
|
||||
|
||||
Returns:
|
||||
str: Команда, состоящая из префикса и текста.
|
||||
"""
|
||||
|
||||
return self.prefix + self.text
|
||||
|
||||
|
||||
class BotCommand(BaseModel):
|
||||
@ -40,19 +13,4 @@ class BotCommand(BaseModel):
|
||||
"""
|
||||
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
class CommandStart(Command):
|
||||
|
||||
"""
|
||||
Класс для представления команды /start бота.
|
||||
|
||||
Attributes:
|
||||
prefix (str): Префикс команды. По умолчанию '/'.
|
||||
"""
|
||||
|
||||
text = 'start'
|
||||
|
||||
def __init__(self, prefix: str = '/'):
|
||||
self.prefix = prefix
|
||||
description: Optional[str] = None
|
Loading…
x
Reference in New Issue
Block a user