allthingslinux / tux

Tux is an all in one bot for the All Things Linux discord server.
https://discord.gg/linux
GNU General Public License v3.0
85 stars 25 forks source link

Fixed a critical bug in the GIF ratelimiter cog caused by the double … #581

Closed rm-rf-tux closed 1 month ago

rm-rf-tux commented 1 month ago

Description

This PR is a bug fix in the GIF limiter cog, causing old GIF timestamps to never be removed and preventing the sending of GIFs completely. (I have also removed a useless check in the message handler function.)

Guidelines

How Has This Been Tested? (if applicable)

Tested in the atl.dev Discord server, with extra debug statements to be absolutely sure everything works

Summary by Sourcery

Fix a critical bug in the GIF limiter cog that caused old GIF timestamps to persist, preventing GIFs from being sent, and remove an unnecessary check in the message handler function.

Bug Fixes:

Enhancements:

sourcery-ai[bot] commented 1 month ago

Reviewer's Guide by Sourcery

This pull request fixes a critical bug in the GIF ratelimiter cog that was preventing the removal of old GIF timestamps and blocking the sending of GIFs. The changes include optimizing the message handling logic, improving the old GIF removal process, and enhancing code readability.

Sequence Diagrams

GIF Limiter Message Handling

sequenceDiagram
    participant U as User
    participant B as Bot
    participant GL as GifLimiter
    U->>B: Send message
    B->>GL: on_message
    GL->>GL: _should_process_message
    alt Contains GIF and not in excluded channel
        GL->>GL: _handle_gif_message
        alt Exceeds channel limit
            GL->>B: Delete message
        else Exceeds user limit
            GL->>B: Delete message
        else Within limits
            GL->>GL: Update recent GIFs
        end
    end

Old GIF Timestamp Removal

sequenceDiagram
    participant GL as GifLimiter
    participant T as Time
    GL->>T: Get current time
    loop For each channel
        GL->>GL: Remove old timestamps
    end
    loop For each user
        GL->>GL: Remove old timestamps
        alt No recent GIFs
            GL->>GL: Delete user entry
        end
    end

File-Level Changes

Change Details Files
Fixed the old GIF timestamp removal logic
  • Modified the old_gif_remover task to properly remove expired timestamps
  • Added logic to delete user entries when they have no recent GIFs
  • Used list() to create a copy of the dictionary items before iteration to avoid runtime errors
tux/cogs/services/gif_limiter.py
Optimized message handling logic
  • Removed redundant checks in the _handle_gif_message method
  • Simplified conditions for checking channel and user GIF limits
tux/cogs/services/gif_limiter.py
Improved code readability and formatting
  • Adjusted indentation and line breaks for better readability
  • Updated docstrings and comments for clarity
  • Removed unnecessary parentheses and simplified boolean expressions
tux/cogs/services/gif_limiter.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. #### Customizing Your Experience Access your [dashboard](https://app.sourcery.ai) 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 - [Contact our support team](mailto:support@sourcery.ai) for questions or feedback. - Visit our [documentation](https://docs.sourcery.ai) for detailed guides and information. - Keep in touch with the Sourcery team by following us on [X/Twitter](https://x.com/SourceryAI), [LinkedIn](https://www.linkedin.com/company/sourcery-ai/) or [GitHub](https://github.com/sourcery-ai).