def download_data(self, message: discord.Message) -> str:
"""Checks if an attachment is viable to be used as
MIDI input and downloads it."""
if not message.attachments:
raise self.SayException('You did not attach a file to '
'use as MIDI input!, `j!help midi`')
attachment = message.attachments[0]
if not attachment.filename.endswith('.txt'):
raise self.SayException('File must be a .txt!')
# see if the file is bigger than 20 KiB as we don't want
# to download huge files
if attachment.size >= 20 * 1024:
raise self.SayException('File is too large. '
'Your file may only be 20KiB big.')
log.info('downloading file to use as MIDI input. '
f'{attachment.size} bytes large.')
buffer = io.BytesIO()
await attachment.save(buffer)
return buffer.getvalue().decode('utf-8')
评论列表
文章目录