From e3678570b5aecc771d8cca201cbb4455f66c4ffb Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Tue, 21 Feb 2017 18:15:04 +0000 Subject: [PATCH] Handle v0.16.6 API send_file(). --- bot.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index 0de1bd3..9109094 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,7 @@ import asyncio import datetime import discord import hashlib +import io import math import multiprocessing import os @@ -325,7 +326,7 @@ def zzz(): db.set_state(client, avatar = db.IDLE_AVATAR, idle = True) def parse_attachment(attachment, filename = None): - parsed = { 'bytes': None, 'filename': filename } + parsed = { 'filename': filename, 'fd': None } if attachment is None: log.warning("Called with no data!") @@ -333,7 +334,7 @@ def parse_attachment(attachment, filename = None): if type(attachment) == dict: # Already formatted! - if 'bytes' not in attachment: + if 'fd' not in attachment: log.warning("Invalid attachment dict {}".format(attachment)) return None if 'filename' not in attachment: @@ -341,7 +342,9 @@ def parse_attachment(attachment, filename = None): return attachment elif type(attachment) == bytes: # Raw bytes. - parsed['bytes'] = attachment + parsed['fd'] = io.BytesIO(attachment) + elif type(attachment) == io.BufferedReader: + parsed['fd'] = attachment elif type(attachment) == str: # Assume URL. try: @@ -349,7 +352,7 @@ def parse_attachment(attachment, filename = None): fd = open_url(attachment) else: fd = open(attachment, 'rb') - parsed['bytes'] = fd.read() + parsed['fd'] = fd if filename is None: parsed['filename'] = os.path.basename(attachment) except: @@ -369,7 +372,8 @@ def maybe_say(channel, text, *, attachment = None, embed = None, probability = 0 if attachment is not None: parsed = parse_attachment(attachment) if parsed is not None: - message = yield from client.send_file(channel, parsed['bytes'], filename = parsed['filename'], content = text) + message = yield from client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text) + parsed['fd'].close() else: log.warning("Missing or invalid attachment!") if message is None: -- 2.7.4