process stikers as attachments
This commit is contained in:
parent
e9b87042e1
commit
c60ad506df
3 changed files with 59 additions and 11 deletions
|
|
@ -411,19 +411,20 @@ class BackupEmoji:
|
|||
class BackupSticker:
|
||||
"""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):
|
||||
self.id = int(data["id"])
|
||||
self.name = data["name"]
|
||||
filename = data.get("filename", "")
|
||||
self._file_path = media_dir / filename if media_dir and filename else None
|
||||
self.url = str(self._file_path) if self._file_path else None
|
||||
def __init__(self, data: dict, backup_root: Path | None = None):
|
||||
self.id = int(data["messageID"]) if "messageID" in data else int(data.get("id", 0))
|
||||
self.name = data.get("name", "Sticker")
|
||||
self.url = data.get("localPath", "")
|
||||
self._backup_root = backup_root
|
||||
self.format = data.get("format", "png")
|
||||
|
||||
async def read(self) -> bytes:
|
||||
if self._file_path and self._file_path.exists():
|
||||
return self._file_path.read_bytes()
|
||||
if self._backup_root and self.url:
|
||||
full = self._backup_root / self.url
|
||||
if full.exists():
|
||||
return full.read_bytes()
|
||||
return b""
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
|
@ -561,8 +562,11 @@ class BackupMessage:
|
|||
# Embeds — store raw dicts (discord.py Embed.from_dict compatible)
|
||||
self.embeds = data.get("embeds", [])
|
||||
|
||||
# Stickers — store raw dicts
|
||||
self.stickers = data.get("stickers", [])
|
||||
# Stickers
|
||||
self.stickers = [
|
||||
BackupSticker(s, backup_root=backup_root)
|
||||
for s in data.get("stickers", [])
|
||||
]
|
||||
|
||||
# Reactions
|
||||
self.reactions = [BackupReaction(r) for r in data.get("reactions", [])]
|
||||
|
|
|
|||
|
|
@ -234,6 +234,28 @@ async def migrate_messages(
|
|||
stats["attachments"] += 1
|
||||
except Exception as 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:
|
||||
# Check if this message is a reply
|
||||
|
|
|
|||
|
|
@ -237,6 +237,28 @@ async def migrate_messages(
|
|||
stats["attachments"] += 1
|
||||
except Exception as 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:
|
||||
# Check if this message is a reply
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue