Skip to content

Conversation

@HikariNee
Copy link
Contributor

@HikariNee HikariNee commented Jun 1, 2025

Description

This PR fixes a typo in the godbolt.py file, adds a minimal wrapper on top of Wandbox's API and fixes some longstanding issues with the $run command while upgrading compilers and adding new languages.

Guidelines

  • My code follows the style guidelines of this project (formatted with Ruff)

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • I have made corresponding changes to the documentation if needed

  • My changes generate no new warnings

  • I have tested this change

  • Any dependent changes have been merged and published in downstream modules

  • I have added all appropriate labels to this PR

  • [Y] I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

NA

Screenshots (if applicable)

NA

Additional Information

NA

Summary by Sourcery

Enhance the run command by integrating Wandbox support for additional languages, upgrading compiler mappings, refactoring execution logic, and improving UI feedback and error handling.

New Features:

  • Introduce a Wandbox API wrapper and integrate it into the run command
  • Allow run command to fetch code from a replied message when no code is provided
  • Add an interactive close button to embedded run outputs
  • React to the run command invocation with a custom emoji

Bug Fixes:

  • Fix typo in the godbolt wrapper docstring
  • Correct backtick removal regex to properly handle triple backticks

Enhancements:

  • Refactor run command into private helper methods for code sanitization and language detection
  • Update compiler mappings with upgraded versions and broader language coverage
  • Consolidate Godbolt and Wandbox execution logic into a single execute function
  • Simplify and update the supported languages listing in the languages command embed

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 1, 2025

Reviewer's Guide

This PR overhauls the $run command by refactoring its core execution flow, introducing Wandbox support alongside Godbolt, updating compiler mappings, and streamlining helper routines for code cleaning, language detection, and error handling.

Sequence Diagram of the Refactored $run Command

sequenceDiagram
    actor User
    participant Bot as "Tux Bot (Discord)"
    participant RunCog as "Run Cog"
    participant GodboltAPI as "Godbolt API"
    participant WandboxAPI as "Wandbox API"

    User->>Bot: $run `lang\ncode` (or reply)
    Bot->>RunCog: process_run_command(context, code)
    activate RunCog
    RunCog->>RunCog: Add reaction to user's message
    RunCog->>RunCog: language = __get_language(code)
    RunCog->>RunCog: cleaned_code = __clean_code(code)

    alt Language not supported by Godbolt or Wandbox
        RunCog->>Bot: Send error embed ("No compiler found")
        Bot-->>User: Error Embed
    else Language supported
        %% Call self.execute to determine backend and run code
        RunCog->>RunCog: output = self.execute(map, language, cleaned_code, is_wandbox, options)
        activate RunCog
        alt is_wandbox (language in compiler_map_wandbox)
            RunCog->>WandboxAPI: HTTP POST (code, compiler, options)
            activate WandboxAPI
            WandboxAPI-->>RunCog: JSON Response (program_output, compiler_error)
            deactivate WandboxAPI
            RunCog->>RunCog: Process Wandbox response to get output string
        else Use Godbolt (language in compiler_map_godbolt)
            RunCog->>GodboltAPI: HTTP POST (code, compiler_id, options)
            activate GodboltAPI
            GodboltAPI-->>RunCog: Text Response
            deactivate GodboltAPI
            RunCog->>RunCog: Process Godbolt response to get output string
        end
        RunCog-->>RunCog: Return formatted_output or None
        deactivate RunCog

        alt Execution failed (output is None or error string)
            RunCog->>Bot: Send error embed ("Failed to get output from compiler")
            Bot-->>User: Error Embed
        else Execution successful
            RunCog->>RunCog: Clear reaction from user's message
            RunCog->>Bot: send_embedded_reply(context, output, language, is_wandbox)
            Bot-->>User: Success Embed (with output and Close button)
        end
    end
    deactivate RunCog
Loading

Updated Class Diagram for Run Cog and Execution Wrappers

classDiagram
    direction LR

    class Run {
        <<Cog>>
        -bot: Tux
        -compiler_map_godbolt: dict
        -compiler_map_wandbox: dict
        +__init__(bot: Tux)
        -__remove_ansi(ansi: str) str
        -__remove_backticks(st: str) str
        -__clean_code(st: str) str
        -__get_language(st: str) str
        +execute(compiler_map: dict, lang: str, code: str, is_wandbox: bool, opts: str|None) str|None
        +send_embedded_reply(ctx: Context, output: str, lang: str, is_wandbox: bool) None
        +run(ctx: Context, code: str|None) None
        +languages(ctx: Context) None
    }

    class Wandbox {
        <<Wrapper Module>>
        -client: httpx.Client
        -url: str
        +getoutput(code: str, compiler: str, options: str|None) dict|None
    }

    class Godbolt {
        <<Wrapper Module>>
        +getoutput(code: str, compiler_id: str, options: str|None) str|None
    }

    class EmbedCreator {
        <<Utility>>
        +create_embed(...)
    }

    Run ..> Wandbox : uses
    Run ..> Godbolt : uses
    Run ..> EmbedCreator : uses
Loading

File-Level Changes

Change Details Files
Introduce Wandbox execution path
  • Add minimal tux/wrappers/wandbox.py wrapper for Wandbox API
  • Import wandbox alongside godbolt in run.py
  • Define compiler_map_wandbox with language-to-compiler mappings
  • In execute, branch on is_wandbox to invoke Wandbox or Godbolt and merge outputs
tux/cogs/utility/run.py
tux/wrappers/wandbox.py
Refactor and unify code execution helpers
  • Remove old generalized_code_executor and generalized_code_constructor methods
  • Introduce private helper methods __remove_ansi, __remove_backticks, __clean_code, __get_language
  • Consolidate compilation logic into a single public execute method
tux/cogs/utility/run.py
Update and extend compiler mappings
  • Rename compiler_map to compiler_map_godbolt and adjust IDs to newer compiler versions
  • Replace backtick regex to target triple backticks
  • Add a broad set of new languages by defining compiler_map_wandbox
tux/cogs/utility/run.py
Enhance command invocation and error handling
  • Allow code extraction from replied messages when no inline code is provided
  • Validate presence and formatting of code, raising errors or sending embeds on failure
  • Use reactions (BreakdancePengu) instead of temporary ‘typing’ messages
  • Clear reactions and handle missing compilers gracefully
tux/cogs/utility/run.py
Improve embed responses and UI
  • Dynamically set embed titles to show Godbolt or Wandbox links
  • Add a persistent ‘Close’ button to delete the response embed
  • Update the supported languages list formatting in the languages command
tux/cogs/utility/run.py
tux/wrappers/godbolt.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @HikariNee - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Context has no fetch_message method (link)

General comments:

  • In tux/wrappers/wandbox.py the payload dict mistakenly uses the options variable as a key instead of the literal string "options"; it should be { 'compiler': compiler, 'code': code, 'options': copt }.
  • The hack in execute() that prepends dummy "Padding word" lines and then slices with lines[5:] is very fragile—refactor to parse the Wandbox JSON response properly rather than relying on magic line offsets.
  • The nested f-string in send_embedded_reply for the service attribution is syntactically invalid; simplify the quoting or split it into separate strings to avoid malformed f-string interpolation.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 8 other issues
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@HikariNee HikariNee force-pushed the run-com branch 2 times, most recently from 396894b to c938ea7 Compare June 1, 2025 04:04
… invoked in an interaction and add a space in the handling of opts in GodboltService._execute(), remove an escape in the code for __remove_backticks().
HikariNee added 2 commits June 1, 2025 09:41
…hen an error happens, and update the version of the C# compiler for an alias.
@HikariNee HikariNee requested a review from kzndotsh June 1, 2025 07:52
@anemoijereja-eden
Copy link
Collaborator

looks good to me! run command definitely could use the attention.

@anemoijereja-eden anemoijereja-eden merged commit a171685 into allthingslinux:main Jun 1, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants