process stikers as attachments

This commit is contained in:
rambros 2026-03-10 16:16:49 +05:30
parent e9b87042e1
commit c60ad506df
3 changed files with 59 additions and 11 deletions

View file

@ -411,19 +411,20 @@ class BackupEmoji:
class BackupSticker: class BackupSticker:
"""Minimal stand-in for discord.GuildSticker.""" """Minimal stand-in for discord.GuildSticker."""
__slots__ = ("id", "name", "url", "format", "_file_path") __slots__ = ("id", "name", "url", "format", "_backup_root")
def __init__(self, data: dict, media_dir: Path | None = None): def __init__(self, data: dict, backup_root: Path | None = None):
self.id = int(data["id"]) self.id = int(data["messageID"]) if "messageID" in data else int(data.get("id", 0))
self.name = data["name"] self.name = data.get("name", "Sticker")
filename = data.get("filename", "") self.url = data.get("localPath", "")
self._file_path = media_dir / filename if media_dir and filename else None self._backup_root = backup_root
self.url = str(self._file_path) if self._file_path else None
self.format = data.get("format", "png") self.format = data.get("format", "png")
async def read(self) -> bytes: async def read(self) -> bytes:
if self._file_path and self._file_path.exists(): if self._backup_root and self.url:
return self._file_path.read_bytes() full = self._backup_root / self.url
if full.exists():
return full.read_bytes()
return b"" return b""
def __repr__(self) -> str: def __repr__(self) -> str:
@ -561,8 +562,11 @@ class BackupMessage:
# Embeds — store raw dicts (discord.py Embed.from_dict compatible) # Embeds — store raw dicts (discord.py Embed.from_dict compatible)
self.embeds = data.get("embeds", []) self.embeds = data.get("embeds", [])
# Stickers — store raw dicts # Stickers
self.stickers = data.get("stickers", []) self.stickers = [
BackupSticker(s, backup_root=backup_root)
for s in data.get("stickers", [])
]
# Reactions # Reactions
self.reactions = [BackupReaction(r) for r in data.get("reactions", [])] self.reactions = [BackupReaction(r) for r in data.get("reactions", [])]

View file

@ -235,6 +235,28 @@ async def migrate_messages(
except Exception as e: except Exception as e:
logger.error(f"Failed to download attachment {att.filename}: {e}") logger.error(f"Failed to download attachment {att.filename}: {e}")
# Process stickers as attachments
if hasattr(msg, 'stickers') and msg.stickers:
for s in msg.stickers:
try:
sticker_data = await context.discord_reader.download_sticker(s)
if sticker_data:
# Use format to determine extension
ext = getattr(s, 'format', 'png')
if hasattr(ext, 'name'): # discord.py StickerFormat enum
ext = ext.name
# Handle Lottie (json)
if ext == 'lottie':
ext = 'json'
filename = f"sticker_{s.name}_{s.id}.{ext}"
files.append({"filename": filename, "data": sticker_data})
stats["attachments"] += 1
logger.debug(f"Added sticker {s.name} as attachment")
except Exception as e:
logger.error(f"Failed to download sticker {getattr(s, 'name', 'unknown')}: {e}")
try: try:
# Check if this message is a reply # Check if this message is a reply
reply_to_fluxer_id = None reply_to_fluxer_id = None

View file

@ -238,6 +238,28 @@ async def migrate_messages(
except Exception as e: except Exception as e:
logger.error(f"Failed to download attachment {att.filename}: {e}") logger.error(f"Failed to download attachment {att.filename}: {e}")
# Process stickers as attachments
if hasattr(msg, 'stickers') and msg.stickers:
for s in msg.stickers:
try:
sticker_data = await context.discord_reader.download_sticker(s)
if sticker_data:
# Use format to determine extension
ext = getattr(s, 'format', 'png')
if hasattr(ext, 'name'): # discord.py StickerFormat enum
ext = ext.name
# Handle Lottie (json)
if ext == 'lottie':
ext = 'json'
filename = f"sticker_{s.name}_{s.id}.{ext}"
files.append({"filename": filename, "data": sticker_data})
stats["attachments"] += 1
logger.debug(f"Added sticker {s.name} as attachment")
except Exception as e:
logger.error(f"Failed to download sticker {getattr(s, 'name', 'unknown')}: {e}")
try: try:
# Check if this message is a reply # Check if this message is a reply
reply_to_stoat_id = None reply_to_stoat_id = None