Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
l-lumin authored Jul 5, 2024
2 parents 19a09fe + cf721d4 commit 460bf7b
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 224 deletions.
18 changes: 9 additions & 9 deletions bot/exts/core/internal_eval/_internal_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import discord
from discord.ext import commands
from pydis_core.utils.logging import get_logger
from pydis_core.utils.paste_service import PasteFile, PasteTooLongError, PasteUploadError, send_to_paste_service

from bot.bot import Bot
from bot.constants import Client, Roles
Expand Down Expand Up @@ -86,17 +87,16 @@ def shorten_output(
async def _upload_output(self, output: str) -> str | None:
"""Upload `internal eval` output to our pastebin and return the url."""
data = self.shorten_output(output, max_length=MAX_LENGTH)
file = PasteFile(content=data, lexer="text")
try:
async with self.bot.http_session.post(
"https://paste.pythondiscord.com/documents", data=data, raise_for_status=True
) as resp:
data = await resp.json()

if "key" in data:
return f"https://paste.pythondiscord.com/{data['key']}"
except Exception:
# 400 (Bad Request) means there are too many characters
resp = await send_to_paste_service(
files=[file],
http_session=self.bot.http_session,
)
return resp.link
except (PasteTooLongError, PasteUploadError):
log.exception("Failed to upload `internal eval` output to paste service!")
return None

async def _send_output(self, ctx: commands.Context, output: str) -> None:
"""Send the `internal eval` output to the command invocation context."""
Expand Down
20 changes: 10 additions & 10 deletions bot/exts/fun/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import discord
from PIL import Image
from aiohttp import client_exceptions, web
from aiohttp import client_exceptions
from discord.ext import commands
from pydis_core.utils.logging import get_logger
from pydis_core.utils.paste_service import PasteFile, PasteTooLongError, PasteUploadError, send_to_paste_service

from bot.bot import Bot
from bot.constants import Channels, WHITELISTED_CHANNELS
Expand Down Expand Up @@ -100,17 +101,16 @@ async def _generate_image(self, query: str, out_file: BinaryIO) -> None:

async def _upload_to_pastebin(self, text: str) -> str | None:
"""Uploads `text` to the paste service, returning the url if successful."""
file = PasteFile(content=text, lexer="text")
try:
async with self.bot.http_session.post(
PASTEBIN_URL + "/documents",
data=text,
raise_for_status=True
) as response:
response_json = await response.json()
if "key" in response_json:
return f"{PASTEBIN_URL}/{response_json['key']}.txt?noredirect"
except web.HTTPClientError as e:
resp = await send_to_paste_service(
files=[file],
http_session=self.bot.http_session,
)
return resp.link
except (PasteTooLongError, PasteUploadError) as e:
log.info("Error when uploading latex output to pastebin. %s", e)
return None

async def _prepare_error_embed(self, err: InvalidLatexError | LatexServerError | None) -> discord.Embed:
title = "Server encountered an issue, please retry later."
Expand Down
Loading

0 comments on commit 460bf7b

Please sign in to comment.