diff options
| author | 2022-12-14 18:29:53 +0800 | |
|---|---|---|
| committer | 2022-12-14 18:29:53 +0800 | |
| commit | d00584408a20e231df1e3d6ce3c4837e6bb829e4 (patch) | |
| tree | 1a35c9b8e9f76f9e4ebcade75b61f02680ce8645 | |
| parent | Rename attribute `failed_files` (diff) | |
Add FILE_COUNT_LIMIT and error messages
| -rw-r--r-- | bot/exts/utils/snekbox/_cog.py | 6 | ||||
| -rw-r--r-- | bot/exts/utils/snekbox/_eval.py | 24 | ||||
| -rw-r--r-- | bot/exts/utils/snekbox/_io.py | 3 | 
3 files changed, 24 insertions, 9 deletions
| diff --git a/bot/exts/utils/snekbox/_cog.py b/bot/exts/utils/snekbox/_cog.py index 9abbbcfc4..f8ddbbf0e 100644 --- a/bot/exts/utils/snekbox/_cog.py +++ b/bot/exts/utils/snekbox/_cog.py @@ -286,12 +286,12 @@ class Snekbox(Cog):                  log.trace("Formatting output...")                  output, paste_link = await self.format_output(result.stdout) -            msg = f"{ctx.author.mention} {result.status_emoji} {msg}.\n" +            msg = f"{ctx.author.mention} {result.status_emoji} {msg}.\n\n"              if not result.files or output not in ("[No output]", ""): -                msg += f"\n```\n{output}\n```" +                msg += f"```\n{output}\n```"              if paste_link: -                msg = f"{msg}\nFull output: {paste_link}" +                msg += f"Full output: {paste_link}"              # Collect stats of job fails + successes              if result.returncode != 0: diff --git a/bot/exts/utils/snekbox/_eval.py b/bot/exts/utils/snekbox/_eval.py index 1764b6871..d955d26d0 100644 --- a/bot/exts/utils/snekbox/_eval.py +++ b/bot/exts/utils/snekbox/_eval.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field  from signal import Signals  from typing import TYPE_CHECKING -from bot.exts.utils.snekbox._io import FILE_SIZE_LIMIT, FileAttachment, sizeof_fmt +from bot.exts.utils.snekbox._io import FILE_COUNT_LIMIT, FILE_SIZE_LIMIT, FileAttachment, sizeof_fmt  from bot.log import get_logger  if TYPE_CHECKING: @@ -92,10 +92,17 @@ class EvalResult:          # Add error message for failed attachments          if self.failed_files:              failed_files = f"({', '.join(self.failed_files)})" -            msg += ( -                f".\n\n> Some attached files were not able to be uploaded {failed_files}." -                f" Check that the file size is less than {sizeof_fmt(FILE_SIZE_LIMIT)}" -            ) +            # Case for over 10 +            if len(self.failed_files) + len(self.files) > FILE_COUNT_LIMIT: +                msg += ( +                    f".\n\n> Some files were not able to be uploaded, as they exceeded" +                    f" the {FILE_COUNT_LIMIT} file upload limit {failed_files}" +                ) +            else: +                msg += ( +                    f".\n\n> Some files were not able to be uploaded {failed_files}." +                    f" Check that the file size is less than {sizeof_fmt(FILE_SIZE_LIMIT)}" +                )          return msg, error @@ -107,7 +114,12 @@ class EvalResult:              returncode=data["returncode"],          ) -        for file in data.get("files", []): +        files = iter(data["files"]) +        for i, file in enumerate(files): +            # Limit to FILE_COUNT_LIMIT files +            if i >= FILE_COUNT_LIMIT: +                res.failed_files.extend(file["path"] for file in files) +                break              try:                  res.files.append(FileAttachment.from_dict(file))              except ValueError as e: diff --git a/bot/exts/utils/snekbox/_io.py b/bot/exts/utils/snekbox/_io.py index a7f84a241..fcf5451aa 100644 --- a/bot/exts/utils/snekbox/_io.py +++ b/bot/exts/utils/snekbox/_io.py @@ -12,6 +12,9 @@ from discord import File  # or 50 MiB for lvl 2 boosted servers  FILE_SIZE_LIMIT = 8 * 1024 * 1024 +# Discord currently has a 10-file limit per message +FILE_COUNT_LIMIT = 10 +  def sizeof_fmt(num: int, suffix: str = "B") -> str:      """Return a human-readable file size.""" | 
