diff options
author | 2022-05-02 16:15:02 +0100 | |
---|---|---|
committer | 2022-05-02 16:15:02 +0100 | |
commit | ff1c10f79097cb54755a164cf1a3b9a1a201fb12 (patch) | |
tree | db5415038b0c55f1e005f08820bc560d5dfbd0bb | |
parent | Add contents length validation to send_to_paste_service util (diff) |
Update functions using send_to_paste_service util
-rw-r--r-- | bot/exts/events/code_jams/_cog.py | 13 | ||||
-rw-r--r-- | bot/exts/moderation/dm_relay.py | 18 | ||||
-rw-r--r-- | bot/exts/moderation/metabase.py | 13 | ||||
-rw-r--r-- | bot/exts/utils/internal.py | 12 | ||||
-rw-r--r-- | bot/exts/utils/snekbox.py | 11 | ||||
-rw-r--r-- | bot/utils/__init__.py | 12 |
6 files changed, 51 insertions, 28 deletions
diff --git a/bot/exts/events/code_jams/_cog.py b/bot/exts/events/code_jams/_cog.py index 452199f5f..86c357863 100644 --- a/bot/exts/events/code_jams/_cog.py +++ b/bot/exts/events/code_jams/_cog.py @@ -12,7 +12,7 @@ from bot.constants import Emojis, Roles from bot.exts.events.code_jams import _channels from bot.log import get_logger from bot.utils.members import get_or_fetch_member -from bot.utils.services import send_to_paste_service +from bot.utils.services import PasteTooLongError, PasteUploadError, send_to_paste_service log = get_logger(__name__) @@ -139,11 +139,14 @@ class CodeJams(commands.Cog): format_category_info(category, channels) for category, channels in categories.items() ) - url = await send_to_paste_service(deletion_details) - if url is None: - url = "**Unable to send deletion details to the pasting service.**" + try: + message = await send_to_paste_service(deletion_details) + except PasteTooLongError: + message = "**Too long to upload to paste service.**" + except PasteUploadError: + message = "**Failed to upload to paste service.**" - return f"Are you sure you want to delete all code jam channels?\n\nThe channels to be deleted: {url}" + return f"Are you sure you want to delete all code jam channels?\n\nThe channels to be deleted: {message}" @codejam.command() @commands.has_any_role(Roles.admins, Roles.code_jam_event_team) diff --git a/bot/exts/moderation/dm_relay.py b/bot/exts/moderation/dm_relay.py index a86c9e409..bf0b96a58 100644 --- a/bot/exts/moderation/dm_relay.py +++ b/bot/exts/moderation/dm_relay.py @@ -5,7 +5,7 @@ from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES from bot.log import get_logger from bot.utils.channel import is_mod_channel -from bot.utils.services import send_to_paste_service +from bot.utils.services import PasteTooLongError, PasteUploadError, send_to_paste_service log = get_logger(__name__) @@ -53,14 +53,14 @@ class DMRelay(Cog): f"User: {user} ({user.id})\n" f"Channel ID: {user.dm_channel.id}\n\n" ) - - paste_link = await send_to_paste_service(metadata + output, extension="txt") - - if paste_link is None: - await ctx.send(f"{Emojis.cross_mark} Failed to upload output to hastebin.") - return - - await ctx.send(paste_link) + try: + message = await send_to_paste_service(metadata + output, extension="txt") + except PasteTooLongError: + message = f"{Emojis.cross_mark} Too long to upload to paste service." + except PasteUploadError: + message = f"{Emojis.cross_mark} Failed to upload to paste service." + + await ctx.send(message) async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog in mod channels.""" diff --git a/bot/exts/moderation/metabase.py b/bot/exts/moderation/metabase.py index 0ca9fd4a5..1b4b0cc71 100644 --- a/bot/exts/moderation/metabase.py +++ b/bot/exts/moderation/metabase.py @@ -17,6 +17,7 @@ from bot.converters import allowed_strings from bot.log import get_logger from bot.utils import send_to_paste_service from bot.utils.channel import is_mod_channel +from bot.utils.services import PasteTooLongError, PasteUploadError log = get_logger(__name__) @@ -141,11 +142,15 @@ class Metabase(Cog): # Format it nicely for human eyes out = json.dumps(out, indent=4, sort_keys=True) - paste_link = await send_to_paste_service(out, extension=extension) - if paste_link: - message = f":+1: {ctx.author.mention} Here's your link: {paste_link}" + try: + paste_link = await send_to_paste_service(out, extension=extension) + except PasteTooLongError: + message = f":x: {ctx.author.mention} Too long to upload to paste service." + except PasteUploadError: + message = f":x: {ctx.author.mention} Failed to upload to paste service." else: - message = f":x: {ctx.author.mention} Link service is unavailible." + message = f":+1: {ctx.author.mention} Here's your link: {paste_link}" + await ctx.send( f"{message}\nYou can also access this data within internal eval by doing: " f"`bot.get_cog('Metabase').exports[{question_id}]`" diff --git a/bot/exts/utils/internal.py b/bot/exts/utils/internal.py index 2148a3676..3125cee75 100644 --- a/bot/exts/utils/internal.py +++ b/bot/exts/utils/internal.py @@ -16,6 +16,7 @@ from bot.bot import Bot from bot.constants import DEBUG_MODE, Roles from bot.log import get_logger from bot.utils import find_nth_occurrence, send_to_paste_service +from bot.utils.services import PasteTooLongError, PasteUploadError log = get_logger(__name__) @@ -194,11 +195,14 @@ async def func(): # (None,) -> Any truncate_index = newline_truncate_index if len(out) > truncate_index: - paste_link = await send_to_paste_service(out, extension="py") - if paste_link is not None: - paste_text = f"full contents at {paste_link}" - else: + try: + paste_link = await send_to_paste_service(out, extension="py") + except PasteTooLongError: + paste_text = "too long to upload to paste service." + except PasteUploadError: paste_text = "failed to upload contents to paste service." + else: + paste_text = f"full contents at {paste_link}" await ctx.send( f"```py\n{out[:truncate_index]}\n```" diff --git a/bot/exts/utils/snekbox.py b/bot/exts/utils/snekbox.py index 32b7c8761..98dfd2efa 100644 --- a/bot/exts/utils/snekbox.py +++ b/bot/exts/utils/snekbox.py @@ -18,6 +18,7 @@ from bot.decorators import redirect_output from bot.log import get_logger from bot.utils import send_to_paste_service from bot.utils.messages import wait_for_deletion +from bot.utils.services import PasteTooLongError, PasteUploadError log = get_logger(__name__) @@ -65,7 +66,7 @@ if not hasattr(sys, "_setup_finished"): {setup} """ -MAX_PASTE_LEN = 10000 +MAX_PASTE_LENGTH = 10_000 # The Snekbox commands' whitelists and blacklists. NO_SNEKBOX_CHANNELS = (Channels.python_general,) @@ -137,10 +138,12 @@ class Snekbox(Cog): """Upload the job's output to a paste service and return a URL to it if successful.""" log.trace("Uploading full output to paste service...") - if len(output) > MAX_PASTE_LEN: - log.info("Full output is too long to upload") + try: + return await send_to_paste_service(output, extension="txt", max_length=MAX_PASTE_LENGTH) + except PasteTooLongError: return "too long to upload" - return await send_to_paste_service(output, extension="txt") + except PasteUploadError: + return "unable to upload" @staticmethod def prepare_timeit_input(codeblocks: list[str]) -> tuple[str, list[str]]: diff --git a/bot/utils/__init__.py b/bot/utils/__init__.py index 13533a467..567821126 100644 --- a/bot/utils/__init__.py +++ b/bot/utils/__init__.py @@ -1,4 +1,12 @@ from bot.utils.helpers import CogABCMeta, find_nth_occurrence, has_lines, pad_base64 -from bot.utils.services import send_to_paste_service +from bot.utils.services import PasteTooLongError, PasteUploadError, send_to_paste_service -__all__ = ['CogABCMeta', 'find_nth_occurrence', 'has_lines', 'pad_base64', 'send_to_paste_service'] +__all__ = [ + 'CogABCMeta', + 'find_nth_occurrence', + 'has_lines', + 'pad_base64', + 'send_to_paste_service', + 'PasteUploadError', + 'PasteTooLongError', +] |