Add convenience NewMessage attrs to get media of specific types

This commit is contained in:
Lonami Exo 2018-03-08 10:30:49 +01:00
parent 841aed13da
commit 09f0f86f1e

View File

@ -482,6 +482,52 @@ class NewMessage(_EventBuilder):
if isinstance(doc, types.Document): if isinstance(doc, types.Document):
return doc return doc
def _document_by_attribute(self, kind, condition=None):
"""
Helper method to return the document only if it has an attribute
that's an instance of the given kind, and passes the condition.
"""
doc = self.document
if doc:
for attr in doc.attributes:
if isinstance(attr, kind):
if not condition or condition(doc):
return doc
@property
def audio(self):
"""
If the message media is a document with an Audio attribute,
this returns the (:obj:`Document`) object.
"""
return self._document_by_attribute(types.DocumentAttributeAudio,
lambda attr: not attr.voice)
@property
def voice(self):
"""
If the message media is a document with a Voice attribute,
this returns the (:obj:`Document`) object.
"""
return self._document_by_attribute(types.DocumentAttributeAudio,
lambda attr: attr.voice)
@property
def video(self):
"""
If the message media is a document with a Video attribute,
this returns the (:obj:`Document`) object.
"""
return self._document_by_attribute(types.DocumentAttributeVideo)
@property
def sticker(self):
"""
If the message media is a document with a Sticker attribute,
this returns the (:obj:`Document`) object.
"""
return self._document_by_attribute(types.DocumentAttributeSticker)
@property @property
def out(self): def out(self):
""" """