diff options
| author | 2019-06-14 10:46:04 -0700 | |
|---|---|---|
| committer | 2019-06-14 21:08:11 -0700 | |
| commit | e003dfc9e9fa2bb876a71f9b73a764d0f4bc6186 (patch) | |
| tree | 3970e4b6896e6f2cb954cd7769d188df88c8d0d2 | |
| parent | Snekbox: move input code preparation to a separate function (diff) | |
Snekbox: move paste service upload to a separate function
| -rw-r--r-- | bot/cogs/snekbox.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index 1d18d2d84..9b0c39ac1 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -3,6 +3,7 @@ import logging import random import re import textwrap +from typing import Optional from discord import Colour, Embed from discord.ext.commands import ( @@ -64,6 +65,18 @@ class Snekbox: async with self.bot.http_session.post(url, json=data, raise_for_status=True) as resp: return await resp.json() + async def upload_output(self, output: str) -> Optional[str]: + """Upload the eval output to a paste service and return a URL to it if successful.""" + url = URLs.paste_service.format(key="documents") + try: + async with self.bot.http_session.post(url, data=output, raise_for_status=True) as resp: + data = await resp.json() + + if "key" in data: + return URLs.paste_service.format(key=data["key"]) + except Exception: + log.exception("Failed to upload full output to paste service!") + @staticmethod def prepare_input(code: str) -> str: """Extract code from the Markdown, format it, and insert it into the code template.""" @@ -149,16 +162,7 @@ class Snekbox: truncated = True if truncated: - try: - response = await self.bot.http_session.post( - URLs.paste_service.format(key="documents"), - data=full_output - ) - data = await response.json() - if "key" in data: - paste_link = URLs.paste_service.format(key=data["key"]) - except Exception: - log.exception("Failed to upload full output to paste service!") + paste_link = await self.upload_output(full_output) if output.strip(): if paste_link: |