-
-
Notifications
You must be signed in to change notification settings - Fork 44
fix: Made the encode and decode commands more descriptive #1030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideThis PR refactors the encode/decode commands by renaming the “cs” parameter to “codesystem”, enriching command decorators with descriptions, aliases, and predefined choices, enforcing ephemeral responses, and updating docstrings accordingly. Class diagram for updated encode/decode command structureclassDiagram
class UtilityCog {
+send_message(ctx, data)
+encode(ctx, codesystem, text)
+decode(ctx, codesystem, text)
}
UtilityCog : encode uses codesystem (str)
UtilityCog : decode uses codesystem (str)
UtilityCog : encode, decode decorated with descriptions, aliases, choices
UtilityCog : send_message(ctx, data) -- ephemeral response
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Extract the repeated codesystem choice list into a shared constant to avoid duplication between the encode and decode commands.
- Replace the repetitive if/elif chains with a mapping from codesystem values to the corresponding base64 encode/decode functions for cleaner, more maintainable code.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the repeated codesystem choice list into a shared constant to avoid duplication between the encode and decode commands.
- Replace the repetitive if/elif chains with a mapping from codesystem values to the corresponding base64 encode/decode functions for cleaner, more maintainable code.
## Individual Comments
### Comment 1
<location> `tux/cogs/utility/encode_decode.py:77` </location>
<code_context>
btext = text.encode(encoding="utf-8")
try:
- if cs == "base16":
+ if codesystem == "base16":
data = base64.b16encode(btext)
</code_context>
<issue_to_address>
Consider refactoring the encoding and decoding logic to use shared mappings and choices for code systems.
```suggestion
# At module top, define mappings and shared choices
ENCODERS = {
"base16": base64.b16encode,
"base32": base64.b32encode,
"base64": base64.b64encode,
"base85": base64.b85encode,
}
DECODERS = {
"base16": base64.b16decode,
"base32": base64.b32decode,
"base64": base64.b64decode,
"base85": base64.b85decode,
}
CODE_CHOICES = [
app_commands.Choice(name=name, value=name)
for name in ENCODERS.keys()
]
# Reuse CODE_CHOICES in both commands:
@commands.hybrid_command(
name="encode", aliases=["ec"], description="Encode a message"
)
@app_commands.describe(text="Text to encode")
@app_commands.choices(codesystem=CODE_CHOICES)
async def encode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str):
btext = text.encode("utf-8")
func = ENCODERS.get(codesystem.lower())
if not func:
return await ctx.reply(
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', ENCODERS))}",
allowed_mentions=allowed_mentions, ephemeral=True
)
try:
data = func(btext)
await self.send_message(ctx, data.decode("utf-8"))
except Exception as e:
await ctx.reply(
content=f"Unknown exception: {e.__class__.__name__}: {e}",
allowed_mentions=allowed_mentions, ephemeral=True
)
@commands.hybrid_command(
name="decode", aliases=["dc"], description="Decode a message"
)
@app_commands.describe(text="Text to decode")
@app_commands.choices(codesystem=CODE_CHOICES)
async def decode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str):
btext = text.encode("utf-8")
func = DECODERS.get(codesystem.lower())
if not func:
return await ctx.reply(
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', DECODERS))}",
allowed_mentions=allowed_mentions, ephemeral=True
)
try:
data = func(btext)
await self.send_message(ctx, data.decode("utf-8"))
except Exception as e:
await ctx.reply(
content=f"Unknown exception: {e.__class__.__name__}: {e}",
allowed_mentions=allowed_mentions, ephemeral=True
)
```
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
btext = text.encode(encoding="utf-8") | ||
|
||
try: | ||
if cs == "base16": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider refactoring the encoding and decoding logic to use shared mappings and choices for code systems.
if cs == "base16": | |
# At module top, define mappings and shared choices | |
ENCODERS = { | |
"base16": base64.b16encode, | |
"base32": base64.b32encode, | |
"base64": base64.b64encode, | |
"base85": base64.b85encode, | |
} | |
DECODERS = { | |
"base16": base64.b16decode, | |
"base32": base64.b32decode, | |
"base64": base64.b64decode, | |
"base85": base64.b85decode, | |
} | |
CODE_CHOICES = [ | |
app_commands.Choice(name=name, value=name) | |
for name in ENCODERS.keys() | |
] | |
# Reuse CODE_CHOICES in both commands: | |
@commands.hybrid_command( | |
name="encode", aliases=["ec"], description="Encode a message" | |
) | |
@app_commands.describe(text="Text to encode") | |
@app_commands.choices(codesystem=CODE_CHOICES) | |
async def encode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str): | |
btext = text.encode("utf-8") | |
func = ENCODERS.get(codesystem.lower()) | |
if not func: | |
return await ctx.reply( | |
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', ENCODERS))}", | |
allowed_mentions=allowed_mentions, ephemeral=True | |
) | |
try: | |
data = func(btext) | |
await self.send_message(ctx, data.decode("utf-8")) | |
except Exception as e: | |
await ctx.reply( | |
content=f"Unknown exception: {e.__class__.__name__}: {e}", | |
allowed_mentions=allowed_mentions, ephemeral=True | |
) | |
@commands.hybrid_command( | |
name="decode", aliases=["dc"], description="Decode a message" | |
) | |
@app_commands.describe(text="Text to decode") | |
@app_commands.choices(codesystem=CODE_CHOICES) | |
async def decode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str): | |
btext = text.encode("utf-8") | |
func = DECODERS.get(codesystem.lower()) | |
if not func: | |
return await ctx.reply( | |
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', DECODERS))}", | |
allowed_mentions=allowed_mentions, ephemeral=True | |
) | |
try: | |
data = func(btext) | |
await self.send_message(ctx, data.decode("utf-8")) | |
except Exception as e: | |
await ctx.reply( | |
content=f"Unknown exception: {e.__class__.__name__}: {e}", | |
allowed_mentions=allowed_mentions, ephemeral=True | |
) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1030 +/- ##
========================================
- Coverage 9.65% 9.65% -0.01%
========================================
Files 123 123
Lines 10427 10432 +5
Branches 1281 1281
========================================
Hits 1007 1007
- Misses 9311 9316 +5
Partials 109 109
*This pull request uses carry forward flags. Click here to find out more.
☔ View full report in Codecov by Sentry. |
9dabfbe
to
727ca84
Compare
Deploying tux with
|
Latest commit: |
f02e749
|
Status: | ✅ Deploy successful! |
Preview URL: | https://abc5c273.tux-afh.pages.dev |
Branch Preview URL: | https://encode-decode-fixes.tux-afh.pages.dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
727ca84
to
6d2d531
Compare
6d2d531
to
702bba9
Compare
Description
Added Descriptions to the command options as well as replacing "cs" with "codesystem" for the entire file
also added predefined options for the codesystem flag and fixed other issues mentioned in issue #1005
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)
Tested on my own Tux instance by using the command in various ways
Screenshots (if applicable)
Summary by Sourcery
Enhance encode and decode commands with clearer parameter naming, help descriptions, command aliases, and predefined encoding choices; make bot replies ephemeral by default.
Bug Fixes:
Enhancements: