Skip to content

fix: use fetch_thread() fallback when resolving message thread#614

Merged
full-bars merged 1 commit into
mainfrom
coderabbit-thread-fix
Jul 20, 2026
Merged

fix: use fetch_thread() fallback when resolving message thread#614
full-bars merged 1 commit into
mainfrom
coderabbit-thread-fix

Conversation

@full-bars

@full-bars full-bars commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Addresses CodeRabbit review on PR #613interaction.message.thread is a lazy property that may not be populated in Discord's cache. Adds _resolve_message_thread() helper that checks the cached property first, then falls back to fetch_thread() from the API. Applies to all three button handlers (sounding, MD, outlook).

Summary by CodeRabbit

  • Bug Fixes
    • Improved summary interactions by reliably locating associated message threads.
    • Added graceful handling when a thread is unavailable or cannot be retrieved.

Uses _resolve_message_thread() helper that checks the cached thread
property first, then falls back to fetch_thread() from the API to handle
the case where Discord hasn't populated the cache yet. Applies to all
three button handlers (sounding, MD, outlook).
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Added centralized Discord thread resolution with API-fetch fallback and updated three AI summary handlers to use it before posting embeds.

Changes

Message thread resolution

Layer / File(s) Summary
Thread lookup and summary handlers
cogs/ai_summaries.py
Added _resolve_message_thread with guarded fetch fallback, and updated sounding, Markdown, and Outlook summary handlers to use it.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • full-bars/spc-bot#613: Updates MD summary posting and interaction message thread detection, complementing this thread-resolution change.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a fetch_thread() fallback when resolving message threads.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch coderabbit-thread-fix

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
cogs/ai_summaries.py (1)

421-433: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add focused tests for the new resolution contract.

Cover cached-thread use, API fallback, and lookup failures; the existing outlook test does not configure or assert these paths.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cogs/ai_summaries.py` around lines 421 - 433, Add focused tests for
_resolve_message_thread covering a message with a cached thread, a cache miss
that successfully returns the result of fetch_thread(), and fetch_thread()
raising discord.NotFound or discord.HTTPException. Assert the cached path does
not call the API, and lookup failures return None without propagating the
exception.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cogs/ai_summaries.py`:
- Around line 430-433: Update the exception handling around the fetch_thread
call to include discord.InvalidData alongside discord.NotFound and
discord.HTTPException, preserving the existing behavior of returning None for
all these fetch failures.

---

Nitpick comments:
In `@cogs/ai_summaries.py`:
- Around line 421-433: Add focused tests for _resolve_message_thread covering a
message with a cached thread, a cache miss that successfully returns the result
of fetch_thread(), and fetch_thread() raising discord.NotFound or
discord.HTTPException. Assert the cached path does not call the API, and lookup
failures return None without propagating the exception.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6acd8c22-c87b-4a7b-b909-b3cfe2708984

📥 Commits

Reviewing files that changed from the base of the PR and between 4ea91bf and 8b41956.

📒 Files selected for processing (1)
  • cogs/ai_summaries.py

Comment thread cogs/ai_summaries.py
Comment on lines +430 to +433
try:
return await message.fetch_thread()
except (discord.NotFound, discord.HTTPException):
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

rg -n 'discord\.py|py-cord|nextcord' \
  --glob 'pyproject.toml' \
  --glob 'requirements*.txt' \
  --glob 'setup.py' \
  --glob 'poetry.lock'

Repository: full-bars/spc-bot

Length of output: 155


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== repo files of interest ==\n'
git ls-files | rg '(^|/)(pyproject\.toml|requirements.*\.txt|setup\.py|poetry\.lock|Pipfile|Pipfile\.lock|uv\.lock|cogs/ai_summaries\.py)$'

printf '\n== outline cogs/ai_summaries.py ==\n'
ast-grep outline cogs/ai_summaries.py --view expanded || true

printf '\n== fetch_thread occurrences ==\n'
rg -n 'fetch_thread|InvalidData|NotFound|HTTPException' cogs/ai_summaries.py

printf '\n== dependency mentions of discord ==\n'
rg -n 'discord' --glob 'pyproject.toml' --glob 'requirements*.txt' --glob 'setup.py' --glob 'poetry.lock' --glob 'uv.lock' --glob 'Pipfile*' || true

Repository: full-bars/spc-bot

Length of output: 2067


🌐 Web query:

discord.py Message.fetch_thread InvalidData NotFound HTTPException documentation

💡 Result:

In discord.py, Message.fetch_thread is designed specifically to retrieve the public thread started from a given message [1][2]. It is important to note that this method uses the message's ID to fetch the thread, which means it will only succeed if the message is the initial message that created the thread [3]. When using Message.fetch_thread, you may encounter the following exceptions [2]: - NotFound: This exception is raised when no thread is found attached to the message [2]. If you attempt to call this method on a message that did not create a thread, or on a subsequent message within a thread (rather than the original starting message), you will likely receive a 404 Not Found error [3][2]. - HTTPException: This is a general exception raised when the HTTP request to the Discord API fails [4][5]. - InvalidData: This is raised if the library receives an unknown channel type or if the guild associated with the thread does not match the one expected by the message object [2]. Because Message.fetch_thread is an API call, the official documentation recommends using the.thread property for general usage, as it accesses the internal cache [1][2]. Note that the.thread property may be None if the thread is not in the cache (e.g., archived threads), in which case fetch_thread is the appropriate fallback if you are targeting the original message [1][2]. If you simply need the channel a message belongs to, you can use the Message.channel property, which will correctly return the thread object if the message was sent within one [3].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '380,470p' cogs/ai_summaries.py

Repository: full-bars/spc-bot

Length of output: 3477


Handle discord.InvalidData here as well.
Message.fetch_thread() can raise discord.InvalidData, so this helper should treat it like NotFound and HTTPException and return None instead of bubbling into the error path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cogs/ai_summaries.py` around lines 430 - 433, Update the exception handling
around the fetch_thread call to include discord.InvalidData alongside
discord.NotFound and discord.HTTPException, preserving the existing behavior of
returning None for all these fetch failures.

@full-bars
full-bars merged commit 8a43b01 into main Jul 20, 2026
10 checks passed
@full-bars
full-bars deleted the coderabbit-thread-fix branch July 20, 2026 23:11
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.

1 participant