Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cogs/ai_summaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,21 @@ async def ensure_outlook_summary(day: str, raw_text: str = None) -> Any | None:
return None


async def _resolve_message_thread(
message: discord.Message | None,
) -> discord.Thread | None:
"""Resolve the thread attached to a message, with API fallback for cache misses."""
if not message:
return None
thread = message.thread
if thread is not None:
return thread
try:
return await message.fetch_thread()
except (discord.NotFound, discord.HTTPException):
return None
Comment on lines +430 to +433

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.



async def autopost_outlook_summary(channel: discord.abc.Messageable, day: str, delay: float = 0.5):
"""Wait for outlook summary to be ready and post it as a follow-up message."""
try:
Expand Down Expand Up @@ -680,7 +695,7 @@ async def _handle_sounding_summary(self, interaction: discord.Interaction, cache
color=discord.Color.teal(),
)

thread = interaction.message.thread if interaction.message else None
thread = await _resolve_message_thread(interaction.message)
if thread:
await thread.send(embed=embed)
await interaction.followup.send(
Expand Down Expand Up @@ -718,7 +733,7 @@ async def _handle_md_summary(self, interaction: discord.Interaction, md_num: str
color=discord.Color.purple(),
)

thread = interaction.message.thread if interaction.message else None
thread = await _resolve_message_thread(interaction.message)
if thread:
await thread.send(embed=embed)
await interaction.followup.send(
Expand Down Expand Up @@ -761,7 +776,7 @@ async def _handle_outlook_summary(self, interaction: discord.Interaction, day: s
)
view = None

thread = interaction.message.thread if interaction.message else None
thread = await _resolve_message_thread(interaction.message)
if thread:
await thread.send(embed=embed, view=view)
await interaction.followup.send(
Expand Down
Loading