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:
|
||||
|
||||
Reference in New Issue
Block a user