transcribe: read media in forwarded messages
Discord keeps forwarded attachments in message snapshots, so the old top-level attachment check reported no media for a forwarded voice message. Check snapshots wherever media is looked up and label the origin note.
This commit is contained in:
+39
-16
@@ -139,9 +139,9 @@ class Transcribe(commands.Cog):
|
||||
|
||||
# ------------------------------------------------------------------ helpers
|
||||
@staticmethod
|
||||
def _is_media_attachment(message: discord.Message) -> Optional[discord.Attachment]:
|
||||
"""First attachment carrying audio, including video containers."""
|
||||
for attachment in message.attachments:
|
||||
def _media_attachment(attachments) -> Optional[discord.Attachment]:
|
||||
"""First attachment in a list carrying audio, including video containers."""
|
||||
for attachment in attachments:
|
||||
content_type = (attachment.content_type or "").lower()
|
||||
name = (attachment.filename or "").lower()
|
||||
if content_type.startswith(("audio/", "video/")):
|
||||
@@ -150,39 +150,61 @@ class Transcribe(commands.Cog):
|
||||
return attachment
|
||||
return None
|
||||
|
||||
def _message_media(
|
||||
self, message: discord.Message
|
||||
) -> tuple[Optional[discord.Attachment], bool]:
|
||||
"""Audio from a message, including media inside forwarded snapshots.
|
||||
|
||||
A forwarded message has no top-level attachments; the original content
|
||||
and files live in `message_snapshots`. Both are checked so forwarding a
|
||||
voice message works the same as posting the file directly.
|
||||
"""
|
||||
attachment = self._media_attachment(message.attachments)
|
||||
if attachment is not None:
|
||||
return attachment, False
|
||||
for snapshot in getattr(message, "message_snapshots", ()) or ():
|
||||
attachment = self._media_attachment(snapshot.attachments)
|
||||
if attachment is not None:
|
||||
return attachment, True
|
||||
return None, False
|
||||
|
||||
async def _find_media(
|
||||
self, ctx: commands.Context
|
||||
) -> tuple[Optional[discord.Attachment], Optional[discord.Message]]:
|
||||
) -> tuple[Optional[discord.Attachment], Optional[discord.Message], bool]:
|
||||
"""Media from the reply, this message, or the message immediately before.
|
||||
|
||||
Deliberately shallow. The first version scanned 25 messages back, so a
|
||||
new video that did not match the extension list silently fell through to
|
||||
whatever older clip it found - every request then transcribed the same
|
||||
audio. Better to say "nothing here" than to guess.
|
||||
|
||||
The third value says the attachment came from a forwarded snapshot, so
|
||||
the origin note can say so instead of implying the sender recorded it.
|
||||
"""
|
||||
replied = await self._replied_message(ctx)
|
||||
if replied is not None:
|
||||
attachment = self._is_media_attachment(replied)
|
||||
attachment, forwarded = self._message_media(replied)
|
||||
if attachment is not None:
|
||||
return attachment, replied
|
||||
return attachment, replied, forwarded
|
||||
|
||||
attachment = self._is_media_attachment(ctx.message)
|
||||
attachment, forwarded = self._message_media(ctx.message)
|
||||
if attachment is not None:
|
||||
return attachment, ctx.message
|
||||
return attachment, ctx.message, forwarded
|
||||
|
||||
async for previous in ctx.channel.history(limit=1, before=ctx.message):
|
||||
attachment = self._is_media_attachment(previous)
|
||||
attachment, forwarded = self._message_media(previous)
|
||||
if attachment is not None:
|
||||
return attachment, previous
|
||||
return None, None
|
||||
return attachment, previous, forwarded
|
||||
return None, None, False
|
||||
|
||||
@staticmethod
|
||||
def _origin_note(message: Optional[discord.Message]) -> str:
|
||||
def _origin_note(message: Optional[discord.Message], forwarded: bool = False) -> str:
|
||||
"""Say which message was transcribed, so a wrong pick is obvious."""
|
||||
if message is None:
|
||||
return ""
|
||||
kind = "forwarded message" if forwarded else "message"
|
||||
return (
|
||||
f"\n\n-# Transcribed {message.author.display_name}'s message"
|
||||
f"\n\n-# Transcribed {message.author.display_name}'s {kind}"
|
||||
f" ({message.jump_url})"
|
||||
)
|
||||
|
||||
@@ -391,14 +413,15 @@ class Transcribe(commands.Cog):
|
||||
await ctx.send(str(exc))
|
||||
return
|
||||
|
||||
attachment, origin_message = await self._find_media(ctx)
|
||||
attachment, origin_message, forwarded = await self._find_media(ctx)
|
||||
if attachment is None:
|
||||
await ctx.send(
|
||||
"I don't see audio or video here. Reply to the message you want "
|
||||
"transcribed, or attach the file to your command."
|
||||
"transcribed, attach the file to your command, or forward the "
|
||||
"voice message and run this again."
|
||||
)
|
||||
return
|
||||
origin = self._origin_note(origin_message)
|
||||
origin = self._origin_note(origin_message, forwarded)
|
||||
|
||||
async with ctx.typing(), self._slow_notice(ctx):
|
||||
try:
|
||||
|
||||
+4
-2
@@ -43,9 +43,11 @@ A message qualifies when any attachment has a content type starting with `audio/
|
||||
|
||||
Video containers are accepted because Whisper extracts the audio track, and members send videos expecting them to be transcribed.
|
||||
|
||||
Forwarded messages have no top level attachments. Their files live in a message snapshot, and the same extension and content type checks run against each snapshot the message carries. Forwarding a voice message and then running `[p]transcribe` bare works because the forward is the previous message. Replying to a forward works too, as does attaching media to the command itself. Forwarded message support needs a bot running discord.py 2.5 or newer, which introduced message snapshots. On an older build, forwards still produce the usual no-media reply.
|
||||
|
||||
When no media is found, the reply explains the three places it checked.
|
||||
|
||||
Each reply also includes a small-print note naming the author of the transcribed message and linking to it, so a wrong pick is visible.
|
||||
Each reply also includes a small-print note naming the author of the transcribed message and linking to it, so a wrong pick is visible. When the audio came from a forwarded snapshot, the note says "forwarded message" and links to the forward rather than the original post. That author is the member who forwarded it, because snapshots do not carry the original author.
|
||||
|
||||
## Transcription paths
|
||||
|
||||
@@ -68,7 +70,7 @@ Replies are clipped near 1900 characters, with the note that the text was cut of
|
||||
|
||||
The command holds a typing indicator for the whole operation. If a request is still running after three seconds, including the download and any translation, the bot posts a short notice that it is still transcribing. The notice is deleted when the request finishes, whether it succeeds or fails. Short clips complete before the delay and never show it.
|
||||
|
||||
Service errors are prefixed with a warning symbol and include the specific cause: Service errors are prefixed with a warning symbol and include the specific cause:
|
||||
Service errors are prefixed with a warning symbol and include the specific cause:
|
||||
|
||||
| Message | Cause |
|
||||
|---|---|
|
||||
|
||||
Reference in New Issue
Block a user