bot.exts.utilities.snekbox#

Snekbox.

Submodules#

Package Contents#

Classes#

CodeblockConverter

Attempts to extract code from a codeblock, if provided.

Snekbox

Safe evaluation of Python code using Snekbox.

EvalJob

Job to be evaluated by snekbox.

EvalResult

The result of an eval job.

class bot.exts.utilities.snekbox.CodeblockConverter[source]#

Bases: discord.ext.commands.Converter

Attempts to extract code from a codeblock, if provided.

async classmethod convert(ctx: discord.ext.commands.Context, code: str) list[str][source]#

Extract code from the Markdown, format it, and insert it into the code template.

If there is any code block, ignore text outside the code block. Use the first code block, but prefer a fenced code block. If there are several fenced code blocks, concatenate only the fenced code blocks.

Return a list of code blocks if any, otherwise return a list with a single string of code.

class bot.exts.utilities.snekbox.Snekbox(bot: bot.bot.Bot)[source]#

Bases: discord.ext.commands.Cog

Safe evaluation of Python code using Snekbox.

build_python_version_switcher_view(current_python_version: SupportedPythonVersions, ctx: discord.ext.commands.Context, job: bot.exts.utilities.snekbox._eval.EvalJob) pydis_core.utils.interactions.ViewWithUserAndRoleCheck[source]#

Return a view that allows the user to change what version of Python their code is run on.

async post_job(job: bot.exts.utilities.snekbox._eval.EvalJob) bot.exts.utilities.snekbox._eval.EvalResult[source]#

Send a POST request to the Snekbox API to evaluate code and return the results.

async static upload_output(http_session: aiohttp.ClientSession, output: str) str | None[source]#

Upload the job’s output to a paste service and return a URL to it if successful.

static prepare_timeit_input(codeblocks: list[str]) list[str][source]#

Join the codeblocks into a single string, then return the arguments in a list.

If there are multiple codeblocks, insert the first one into the wrapped setup code.

async format_output(output: str, max_lines: int = MAX_OUTPUT_BLOCK_LINES, max_chars: int = MAX_OUTPUT_BLOCK_CHARS, line_nums: bool = True, output_default: str = '[No output]') tuple[str, str | None][source]#

Format the output and return a tuple of the formatted output and a URL to the full output.

Prepend each line with a line number. Truncate if there are over 10 lines or 1000 characters and upload the full output to a paste service.

_filter_files(ctx: discord.ext.commands.Context, files: list[bot.exts.utilities.snekbox._io.FileAttachment], blocked_exts: set[str]) FilteredFiles[source]#

Filter to restrict files to allowed extensions. Return a named tuple of allowed and blocked files lists.

async send_job(ctx: discord.ext.commands.Context, job: bot.exts.utilities.snekbox._eval.EvalJob) discord.Message[source]#

Evaluate code, format it, and send the output to the corresponding channel.

Return the bot response.

async continue_job(ctx: discord.ext.commands.Context, response: discord.Message, job_name: str) bot.exts.utilities.snekbox._eval.EvalJob | None[source]#

Check if the job’s session should continue.

If the code is to be re-evaluated, return the new EvalJob. Otherwise, return None if the job’s session should be terminated.

async get_code(message: discord.Message, command: discord.ext.commands.Command) str | None[source]#

Return the code from message to be evaluated.

If the message is an invocation of the command, return the first argument or None if it doesn’t exist. Otherwise, return the full content of the message.

async run_job(ctx: discord.ext.commands.Context, job: bot.exts.utilities.snekbox._eval.EvalJob) None[source]#

Handle checks, stats and re-evaluation of a snekbox job.

async eval_command(ctx: discord.ext.commands.Context, python_version: SupportedPythonVersions | None, *, code: CodeblockConverter) None[source]#

Run Python code and get the results.

This command supports multiple lines of code, including formatted code blocks. Code can be re-evaluated by editing the original message within 10 seconds and clicking the reaction that subsequently appears.

The starting working directory /home, is a writeable temporary file system. Files created, excluding names with leading underscores, will be uploaded in the response.

If multiple codeblocks are in a message, all of them will be joined and evaluated, ignoring the text outside them.

By default, your code is run on Python 3.11. A python_version arg of 3.10 can also be specified.

We’ve done our best to make this sandboxed, but do let us know if you manage to find an issue with it!

async timeit_command(ctx: discord.ext.commands.Context, python_version: SupportedPythonVersions | None, *, code: CodeblockConverter) None[source]#

Profile Python Code to find execution time.

This command supports multiple lines of code, including code wrapped inside a formatted code block. Code can be re-evaluated by editing the original message within 10 seconds and clicking the reaction that subsequently appears.

If multiple formatted codeblocks are provided, the first one will be the setup code, which will not be timed. The remaining codeblocks will be joined together and timed.

By default, your code is run on Python 3.11. A python_version arg of 3.10 can also be specified.

We’ve done our best to make this sandboxed, but do let us know if you manage to find an issue with it!

class bot.exts.utilities.snekbox.EvalJob[source]#

Job to be evaluated by snekbox.

args: list[str][source]#
files: list[bot.exts.utilities.snekbox._io.FileAttachment][source]#
name: str = 'eval'[source]#
version: bot.exts.utilities.snekbox._cog.SupportedPythonVersions = '3.11'[source]#
classmethod from_code(code: str, path: str = 'main.py') Self[source]#

Create an EvalJob from a code string.

as_version(version: bot.exts.utilities.snekbox._cog.SupportedPythonVersions) Self[source]#

Return a copy of the job with a different Python version.

to_dict() dict[str, list[str | dict[str, str]]][source]#

Convert the job to a dict.

class bot.exts.utilities.snekbox.EvalResult[source]#

The result of an eval job.

property has_output: bool[source]#

True if the result has any output (stdout, files, or failed files).

property has_files: bool[source]#

True if the result has any files or failed files.

property status_emoji: str[source]#

Return an emoji corresponding to the status code or lack of output in result.

property error_message: str[source]#

Return an error message corresponding to the process’s return code.

property files_error_message: str[source]#

Return an error message corresponding to the failed files.

stdout: str[source]#
returncode: int | None[source]#
files: list[bot.exts.utilities.snekbox._io.FileAttachment][source]#
failed_files: list[str][source]#
get_failed_files_str(char_max: int = 85) str[source]#

Return a string containing the names of failed files, truncated char_max.

Will truncate on whole file names if less than 3 characters remaining.

get_message(job: EvalJob) str[source]#

Return a user-friendly message corresponding to the process’s return code.

classmethod from_dict(data: dict[str, str | int | list[dict[str, str]]]) Self[source]#

Create an EvalResult from a dict.