Skip to content

Conversation

@meatsnails
Copy link
Collaborator

@meatsnails meatsnails commented Aug 23, 2024

@kzndotsh requested i create a pull request so she can look at the code

Summary by Sourcery

Add a temporary ban feature to the moderation cog, allowing users to be banned for a specified duration with additional options. Improve snippet editing feedback by showing a content preview.

New Features:

  • Introduce a temporary ban command (tempban) that allows moderators to ban a user for a specified duration with options for silent bans and message purging.

Enhancements:

  • Enhance snippet editing feedback by displaying a preview of the previous and new content when a snippet is edited.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 23, 2024

Reviewer's Guide by Sourcery

This pull request implements a temporary ban feature for a Discord bot. The changes include adding a new TempBan cog, modifying the TempBanFlags class, and updating the snippet editing functionality. The implementation focuses on creating a command to temporarily ban users with customizable duration and reason, along with proper logging and case management.

File-Level Changes

Change Details Files
Implement temporary ban functionality
  • Add TempBan cog with tempban command
  • Implement logic for parsing ban duration
  • Add case creation and management for temp bans
  • Implement untempban method for automatic unbanning
  • Add error handling for invalid inputs
tux/cogs/moderation/tempban.py
Update TempBanFlags class
  • Change expires_at type from int to str
  • Update expires_at description
  • Add silent flag for quiet banning
tux/utils/flags.py
Enhance snippet editing functionality
  • Add logging for snippet edits
  • Implement content truncation for long snippets
  • Update response message to show before and after content
tux/cogs/utility/snippets.py

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

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 @GabberBalls - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Fix logic error in untempban method (link)

Overall Comments:

  • The untempban method needs revision. It's not clear how it's triggered when a ban expires, and the unbanning condition seems incorrect.
  • Consider improving error handling in the tempban command. Currently, it proceeds to create a case and schedule an unban even if the initial ban action fails.
  • The code could benefit from better organization. Consider using constants or enums for magic numbers and strings, and potentially refactoring the duration parsing logic for improved robustness.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 1 other issue
  • 🟢 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 to tell me if it was helpful.

) -> None:
if ctx.guild is None:
logger.warning("Temp ban command used outside of a guild context.")
return
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Improve error handling in tempban command to ensure consistency

If the ban fails, the function should return early to prevent creating a case or scheduling an unban for a user who wasn't actually banned.

Comment on lines 147 to 154
async def untempban(self, ctx: commands.Context[commands.Bot], expires_at, target: discord.Member) -> None:
if expires_at is None:
return
now = datetime.datetime.now(tz=datetime.UTC)

if now <= self.db.case.get_all_cases_by_type(discord.guild_id, case_type=CaseType.TEMPBAN):
unban.Unban.unban(self, target, "Temporary Ban Expired")
return
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Fix logic error in untempban method

The condition for unbanning is incorrect. It should compare the current time with expires_at, not with the result of get_all_cases_by_type. This needs to be fixed to ensure temporary bans are properly lifted.

aliases=["r"],
default=MISSING,
)
expires_at: int = commands.flag(
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider keeping expires_at as an int to simplify its usage.

The recent changes to the TempBanFlags class have increased complexity. Changing expires_at from int to str introduces additional parsing and validation needs, which can complicate the codebase. Consider keeping expires_at as an int to represent the duration in days, simplifying its usage. The addition of the silent flag is a useful feature, but remember to document it thoroughly to ensure clarity for future developers. Here's a suggestion to maintain simplicity:

class TempBanFlags(commands.FlagConverter, delimiter=" ", prefix="-"):
    reason: str = commands.flag(
        name="reason",
        description="The reason for the member temp ban.",
        aliases=["r"],
        default=MISSING,
    )
    expires_at: int = commands.flag(
        name="expires_at",
        description="The time in days the ban will last for.",
        aliases=["t", "d", "e"],
    )
    purge_days: int = commands.flag(
        name="purge_days",
        description="The number of days (< 7) to purge in messages.",
        aliases=["p"],
        default=0,
    )
    silent: bool = commands.flag(
        name="silent",
        description="Do not send a DM to the target.",
        aliases=["s", "quiet"],
        default=False,
    )

This approach keeps the complexity manageable and ensures consistency across the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants