From 85e7ed15eff1a36a8c7fd49952ef9535d025f1fa Mon Sep 17 00:00:00 2001 From: Purpur <43528123+Pemigrade@users.noreply.github.com> Date: Fri, 12 Feb 2021 16:14:54 -0600 Subject: [PATCH] improve binflop file encoding & improve logging --- bot.py | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/bot.py b/bot.py index 95a0f5d..2e116f1 100644 --- a/bot.py +++ b/bot.py @@ -8,7 +8,6 @@ from discord.ext import commands, tasks from discord.ext.commands import has_permissions, MissingPermissions from dotenv import load_dotenv import aiohttp -from unidecode import unidecode # import subprocess @@ -37,29 +36,25 @@ async def on_message(message): if len(message.attachments) > 0: if not message.attachments[0].url.endswith( ('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi', '.gif', '.image', '.svg')): - download = message.attachments[0].url - async with aiohttp.ClientSession() as session: - async with session.get(download, allow_redirects=True) as r: - - # r = requests.get(download, allow_redirects=True) - text = await r.text() - text = unidecode(text) - text = "\n".join(text.splitlines()) - if '�' not in text: # If it's not an image/gif - truncated = False - if len(text) > 100000: - text = text[:99999] - truncated = True - req = requests.post('https://bin.birdflop.com/documents', data=text) - key = json.loads(req.content)['key'] - response = "" - response = response + "https://bin.birdflop.com/" + key - response = response + "\nRequested by " + message.author.mention - if truncated: - response = response + "\n(file was truncated because it was too long.)" - embed_var = discord.Embed(title="Please use a paste service", color=0x1D83D4) - embed_var.description = response - await message.channel.send(embed=embed_var) + text = await discord.Attachment.read(message.attachments[0], use_cached=False) + text = text.decode('Latin-1') + text = "\n".join(text.splitlines()) + if '�' not in text: # If it's not an image/gif + truncated = False + if len(text) > 100000: + text = text[:99999] + truncated = True + req = requests.post('https://bin.birdflop.com/documents', data=text) + key = json.loads(req.content)['key'] + response = "" + response = response + "https://bin.birdflop.com/" + key + response = response + "\nRequested by " + message.author.mention + if truncated: + response = response + "\n(file was truncated because it was too long.)" + embed_var = discord.Embed(title="Please use a paste service", color=0x1D83D4) + embed_var.description = response + await message.channel.send(embed=embed_var) + logging.info(f'File uploaded by {message.author} ({message.author.id}): https://bin.birdflop.com/{key}') timings = bot.get_cog('Timings') await timings.analyze_timings(message) await bot.process_commands(message)