コード例 #1
0
async def download_media_from_msg(message: types.Message) -> dict:
    '''
       Returns downloaded content in format :
       {
           "content_type" : mime_type,
           "content_data" : bytes_to_str(data.read())
       } 
       OR None on error.
    '''
    global SUPPORTED_MEDIA_TYPES

    content_type = message.content_type
    if content_type not in SUPPORTED_MEDIA_TYPES:
        logging.warning(
            f"download_media_from_msg function was called with unsupported content_type '{content_type}'"
        )
        return
    try:
        content = message.__getattribute__(content_type)
    except AttributeError as err:
        logging.error(
            f"Can't get {content_type} from message. Canceling download_media_from_msg function"
        )
        return

    try:
        content = content[
            0]  # best way to handle content that sometiems is some kind of array
    except Exception:
        pass
    if content is None:
        logging.warning(
            f"{content_type} field in message is None. Canceling download_media_from_msg function"
        )
        return

    data = io.BytesIO()
    await content.download(data)
    mime_type = filetype.guess(data).mime
    data.seek(0)
    item = {
        "content_type": mime_type,
        "content_data": bytes_to_str(data.read())
    }
    return item
コード例 #2
0
 async def check(self, message: Message) -> bool:
     flag = False
     if members := message.__getattribute__("new_chat_members"):
         for member in members:
             flag = True if member.id == (await bot.get_me()).id else flag