Skip to content

Conversation

@electron271
Copy link
Member

@electron271 electron271 commented Jul 4, 2025

feat(converters): add get_channel_safe function

Description

make starboard work in threads and add a get_channel_safe function to make some of this more abstracted

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 in a channel and in thread, temporarily disabled self star protection so it would work

setup: %starboard setup #starboard ⭐ 1

Screenshots (if applicable)

Please add screenshots to help explain your changes.

Additional Information

Please add any other information that is important to this PR.

Summary by Sourcery

Add a safe channel retrieval helper and refactor channel-fetch logic to support thread messages for starboard and polling features, including error logging.

Bug Fixes:

  • Fix starboard not handling reactions in thread channels

Enhancements:

  • Introduce get_channel_safe utility for abstracted and safe channel fetching with error logging
  • Refactor poll and starboard listeners to use get_channel_safe instead of direct get_channel/fetch logic

@electron271 electron271 requested a review from Copilot July 4, 2025 02:28
@electron271 electron271 self-assigned this Jul 4, 2025
@electron271 electron271 added category: commands category: meta Relating to architecture, systems and critical functions priority: low labels Jul 4, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 4, 2025

Reviewer's Guide

Introduce a unified get_channel_safe helper in converters to abstract channel retrieval (including threads) with error handling, and refactor poll and starboard cogs to use it instead of inlined logic.

Sequence diagram for unified channel retrieval with get_channel_safe

sequenceDiagram
    participant Bot as Tux (bot)
    participant get_channel_safe
    participant DiscordAPI
    participant Logger

    Bot->>get_channel_safe: get_channel_safe(bot, channel_id)
    get_channel_safe->>Bot: bot.get_channel(channel_id)
    alt Channel found in cache
        get_channel_safe-->>Bot: Return channel
    else Channel not found
        get_channel_safe->>DiscordAPI: fetch_channel(channel_id)
        alt Channel fetched successfully
            get_channel_safe-->>Bot: Return channel
        else Channel not found or error
            get_channel_safe->>Logger: Log error
            get_channel_safe-->>Bot: Return None
        end
    end
Loading

Sequence diagram for starboard reaction handling with thread support

sequenceDiagram
    participant StarboardCog
    participant get_channel_safe
    participant Logger
    participant Channel as TextChannel/Thread

    StarboardCog->>get_channel_safe: get_channel_safe(bot, payload.channel_id)
    alt Channel found
        get_channel_safe-->>StarboardCog: Channel (TextChannel or Thread)
        StarboardCog->>Channel: fetch_message(payload.message_id)
    else Channel not found
        get_channel_safe-->>StarboardCog: None
        StarboardCog->>Logger: Log error
    end
Loading

Class diagram for get_channel_safe and channel retrieval

classDiagram
    class Tux
    class discord.TextChannel
    class discord.Thread
    class get_channel_safe {
        +async get_channel_safe(bot: Tux, channel_id: int) discord.TextChannel | discord.Thread | None
    }
    Tux <|-- get_channel_safe
    get_channel_safe --> discord.TextChannel
    get_channel_safe --> discord.Thread
Loading

File-Level Changes

Change Details Files
Abstract channel fetching into a reusable helper
  • Added discord and logger imports in converters
  • Defined get_channel_safe with get_channel, fetch fallback, and error logging
  • Used typing.cast for the return type
tux/utils/converters.py
Refactor cogs to use get_channel_safe
  • Replaced manual get_channel/fetch patterns in on_raw_reaction_add and starboard handler
  • Simplified error handling and logging for missing or inaccessible channels
tux/cogs/utility/poll.py
tux/cogs/services/starboard.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

@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

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 @electron271 - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `tux/cogs/utility/poll.py:76` </location>
<code_context>

-        channel = self.bot.get_channel(payload.channel_id)
-        if not isinstance(channel, discord.TextChannel):
+        channel = await get_channel_safe(self.bot, payload.channel_id)
+        if channel is None:
+            logger.error(f"Channel with ID {payload.channel_id} not found.")
</code_context>

<issue_to_address>
Error logging is duplicated with get_channel_safe.

Consider removing the extra logger.error here to prevent duplicate log entries, as get_channel_safe already handles error logging.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        if channel is None:
            logger.error(f"Channel with ID {payload.channel_id} not found.")
            return
=======
        if channel is None:
            return
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `tux/cogs/services/starboard.py:300` </location>
<code_context>

-        channel = self.bot.get_channel(payload.channel_id)
-        if not isinstance(channel, discord.TextChannel):
+        channel = await get_channel_safe(self.bot, payload.channel_id)
+        if channel is None:
+            logger.error(f"Channel with ID {payload.channel_id} not found.")
</code_context>

<issue_to_address>
Error logging may be redundant with get_channel_safe.

get_channel_safe already logs missing channels, so this additional log may be unnecessary. Please check if both are needed.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        channel = await get_channel_safe(self.bot, payload.channel_id)
        if channel is None:
            logger.error(f"Channel with ID {payload.channel_id} not found.")
            return
=======
        channel = await get_channel_safe(self.bot, payload.channel_id)
        if channel is None:
            return
>>>>>>> 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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a helper to safely fetch text channels or threads and updates the poll and starboard cogs to use it, enabling starboard to work in threads.

  • Added get_channel_safe to abstract channel retrieval with error handling
  • Updated poll.py to use get_channel_safe instead of inline fetching
  • Updated starboard.py similarly to handle threads and reuse the new helper

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tux/utils/converters.py Added get_channel_safe function and imported necessary tools
tux/cogs/utility/poll.py Replaced manual channel lookup with get_channel_safe
tux/cogs/services/starboard.py Swapped inline channel fetching for get_channel_safe
Comments suppressed due to low confidence (3)

tux/utils/converters.py:82

  • The new get_channel_safe function does not have accompanying tests. Consider adding unit tests for successful fetch, NotFound, and exception paths to ensure coverage.
async def get_channel_safe(bot: Tux, channel_id: int) -> discord.TextChannel | discord.Thread | None:

tux/cogs/utility/poll.py:78

  • The logger is used here but not imported in this file, which will cause a NameError. Add from loguru import logger at the top of the file.
            logger.error(f"Channel with ID {payload.channel_id} not found.")

tux/cogs/services/starboard.py:302

  • The logger is used here but not imported in this file, leading to a NameError. Please add from loguru import logger alongside the other imports.
            logger.error(f"Channel with ID {payload.channel_id} not found.")

@codecov
Copy link

codecov bot commented Jul 4, 2025

Codecov Report

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

Project coverage is 9.31%. Comparing base (880e3ed) to head (0073dfb).
Report is 2 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/utils/converters.py 0.00% 16 Missing ⚠️
tux/cogs/services/starboard.py 0.00% 3 Missing ⚠️
tux/cogs/utility/poll.py 0.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #937      +/-   ##
========================================
+ Coverage   9.07%   9.31%   +0.24%     
========================================
  Files        121     121              
  Lines      10272   10280       +8     
  Branches    1257    1258       +1     
========================================
+ Hits         932     958      +26     
+ Misses      9239    9220      -19     
- Partials     101     102       +1     
Flag Coverage Δ *Carryforward flag
database 0.30% <ø> (+<0.01%) ⬆️ Carriedforward from 880e3ed
integration 5.92% <0.00%> (-0.01%) ⬇️
unit 6.38% <0.00%> (-0.01%) ⬇️

*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.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 4, 2025

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0073dfb
Status: ✅  Deploy successful!
Preview URL: https://f8e3fcc1.tux-afh.pages.dev
Branch Preview URL: https://fix-starboard.tux-afh.pages.dev

View logs

@anemoijereja-eden anemoijereja-eden merged commit b12d079 into main Jul 4, 2025
36 checks passed
@anemoijereja-eden anemoijereja-eden deleted the fix-starboard branch July 4, 2025 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: commands category: meta Relating to architecture, systems and critical functions priority: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants