Skip to content

Conversation

@amadaluzia
Copy link

@amadaluzia amadaluzia commented Jul 15, 2025

Description

This PR adds a command named encode and decode. They are both used to encode and decode messages in multiple coding systems ranging from Base16 to Base85. I have made sure to error handle, document, and checked my code with ruff and pyright.

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

  • I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

I have ran the command using all available options, while also running commands which I know will error.

Screenshots (if applicable)

Please add screenshots to help explain your changes.

{14EA23BB-9646-4995-83EF-7ED012C86716} {31931D59-4AF8-4817-9890-51CDBC933DDC} {6F921E7D-5773-460D-AAD1-600793726E76}

Summary by Sourcery

Add a new EncodeDecode cog with hybrid commands to encode and decode text in Base16, Base32, Base64, and Base85 systems, including input validation and error handling.

New Features:

  • Introduce an encode command to convert UTF-8 text into Base16, Base32, Base64, or Base85.
  • Introduce a decode command to convert Base16, Base32, Base64, or Base85 encoded text back to UTF-8.
  • Define a CODING_SYSTEMS mapping and helper functions for supported encoding and decoding operations.

Enhancements:

  • Wrap command responses to disable mentions and generate usage text automatically via generate_usage.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 15, 2025

Reviewer's Guide

This PR introduces an EncodeDecode cog adding two hybrid commands, encode and decode, that support multiple base coding systems (Base16, Base32, Base64, Base85) with proper error handling and usage documentation.

Class diagram for the new EncodeDecode cog

classDiagram
    class EncodeDecode {
        - bot: Tux
        + __init__(bot: Tux)
        + encode(ctx, cs, text)
        + decode(ctx, cs, text)
    }
    EncodeDecode --|> commands.Cog
    class Tux
    class commands.Cog
Loading

Flow diagram for encode/decode command logic

flowchart TD
    A[User invokes encode/decode command] --> B{Is coding system valid?}
    B -- No --> C[Reply: Invalid coding system]
    B -- Yes --> D[Try to encode/decode text]
    D -- Success --> E[Reply: Encoded/Decoded text]
    D -- Error --> F[Reply: Error message]
Loading

File-Level Changes

Change Details Files
Implement EncodeDecode cog with encode and decode commands supporting Base16/32/64/85
  • Added wrap_strings helper and allowed_mentions configuration
  • Defined CODING_SYSTEMS mapping and invalid_cs_message template
  • Implemented EncodeDecode cog: generate_usage, hybrid commands, encoding/decoding logic, and error handling
  • Added setup function to register the cog
tux/cogs/utility/encode_decode.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 @amadaluzia - I've reviewed your changes - here's some feedback:

  • Extract the shared coding‐system validation and reply logic into a helper to DRY up the encode/decode commands.
  • Catch more specific decoding/encoding exceptions (e.g. binascii.Error) rather than a broad Exception to avoid masking unexpected failures.
  • Make the invalid coding system responses ephemeral to prevent cluttering public channels with error messages.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the shared coding‐system validation and reply logic into a helper to DRY up the encode/decode commands.
- Catch more specific decoding/encoding exceptions (e.g. binascii.Error) rather than a broad Exception to avoid masking unexpected failures.
- Make the invalid coding system responses ephemeral to prevent cluttering public channels with error messages.

## Individual Comments

### Comment 1
<location> `tux/cogs/utility/encode_decode.py:59` </location>
<code_context>
+            The text you want to encode.
+        """
+
+        if cs not in CODING_SYSTEMS:
+            await ctx.reply(
+                content=invalid_cs_message,
</code_context>

<issue_to_address>
Coding system check is case-sensitive, which may confuse users.

Consider normalizing the input (e.g., cs.lower()) to accept case-insensitive values and improve usability.
</issue_to_address>

### Comment 2
<location> `tux/cogs/utility/encode_decode.py:67` </location>
<code_context>
+            return
+
+        try:
+            data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
+            await ctx.reply(
+                content=data,
</code_context>

<issue_to_address>
No output length limit may cause issues with large inputs.

Consider checking the output length and truncating or warning if it exceeds Discord's message limit.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
            data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
            await ctx.reply(
                content=data,
                allowed_mentions=allowed_mentions,
            )
=======
            data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
            DISCORD_MESSAGE_LIMIT = 2000
            if len(data) > DISCORD_MESSAGE_LIMIT:
                truncated_data = data[:DISCORD_MESSAGE_LIMIT - 50] + "\n...[truncated, output exceeded Discord's 2000 character limit]"
                await ctx.reply(
                    content=truncated_data,
                    allowed_mentions=allowed_mentions,
                )
            else:
                await ctx.reply(
                    content=data,
                    allowed_mentions=allowed_mentions,
                )
>>>>>>> REPLACE

</suggested_fix>

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.

@amadaluzia
Copy link
Author

@sourcery-ai I've refactored the code to make it less DRY on purpose. I believe this is more readable, as DRY would only be applied if it is repeated 3 or more times.

@amadaluzia amadaluzia force-pushed the base64 branch 2 times, most recently from f460dbe to 3da6bfd Compare July 15, 2025 19:26
@codecov
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

Attention: Patch coverage is 0% with 63 lines in your changes missing coverage. Please review.

Project coverage is 9.26%. Comparing base (d88e22b) to head (84f4a86).
Report is 6 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/cogs/utility/encode_decode.py 0.00% 63 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #949      +/-   ##
========================================
- Coverage   9.32%   9.26%   -0.06%     
========================================
  Files        121     122       +1     
  Lines      10286   10349      +63     
  Branches    1259    1268       +9     
========================================
  Hits         959     959              
- Misses      9226    9289      +63     
  Partials     101     101              
Flag Coverage Δ *Carryforward flag
database 0.30% <ø> (ø) Carriedforward from d88e22b
integration 5.88% <0.00%> (-0.04%) ⬇️
unit 6.33% <0.00%> (-0.04%) ⬇️

*This pull request uses carry forward flags. Click here to find out more.

Components Coverage Δ
Core Bot Infrastructure 16.45% <ø> (ø)
Database Layer 0.00% <ø> (ø)
Bot Commands & Features 0.00% <0.00%> (ø)
Event & Error Handling ∅ <ø> (∅)
Utilities & Helpers ∅ <ø> (∅)
User Interface Components 0.00% <ø> (ø)
CLI Interface ∅ <ø> (∅)
External Service Wrappers ∅ <ø> (∅)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anemoijereja-eden anemoijereja-eden merged commit 3e4f69e into allthingslinux:main Jul 17, 2025
1 check 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