bug fix: duplicates during update existing backup

This commit is contained in:
rambros 2026-03-12 13:51:54 +05:30
parent 8f80d36503
commit 16e94013c7
2 changed files with 14 additions and 8 deletions

View file

@ -884,8 +884,8 @@ class BackupReader:
self._member_map[uid] = stub
return stub
def _load_channel_messages(self, channel_id: int) -> list[dict]:
"""Loads the messages array from a channel JSON file."""
def _load_channel_messages_data(self, channel_id: int) -> list[dict]:
"""Loads the raw messages array from a channel JSON file."""
bp = self.backup_path / "message_backup"
# Primary: message_backup/{channel_id}/messages.json
@ -957,6 +957,7 @@ class BackupReader:
channel_id: int,
limit: int = None,
after_id: int = None,
inclusive: bool = False
) -> AsyncGenerator["BackupMessage", None]:
"""Yields BackupMessages from the backup, respecting after_id and limit."""
messages = self._load_channel_messages(channel_id)
@ -964,7 +965,10 @@ class BackupReader:
for m in messages:
msg_id = int(m["messageID"])
if after_id and msg_id < after_id:
if after_id:
if inclusive and msg_id < after_id:
continue
if not inclusive and msg_id <= after_id:
continue
yield self._hydrate_message(m, channel_id)

View file

@ -213,13 +213,15 @@ class DiscordReader:
return message
return None
async def fetch_message_history(self, channel_id: int, limit: int = None, after_id: int = None) -> AsyncGenerator[discord.Message, None]:
async def fetch_message_history(self, channel_id: int, limit: int = None, after_id: int = None, inclusive: bool = False) -> AsyncGenerator[discord.Message, None]:
"""Yields messages from a given channel, optionally handling pagination."""
channel = await self.get_channel(channel_id)
if isinstance(channel, discord.TextChannel) or isinstance(channel, discord.Thread):
# Discord's 'after' is exclusive. To make it inclusive, we use after_id - 1.
after = discord.Object(id=after_id - 1) if after_id else None
logger.info(f"Fetching message history for {channel.name} ({channel.id}) oldest_first=True after={after_id}")
# Discord's 'after' is exclusive. To make it inclusive, we use after_id - 1 if requested.
after = None
if after_id:
after = discord.Object(id=after_id - 1) if inclusive else discord.Object(id=after_id)
logger.info(f"Fetching message history for {channel.name} ({channel.id}) oldest_first=True after={after_id} inclusive={inclusive}")
# To avoid exploding RAM, we yield items one by one
async for message in channel.history(limit=limit, oldest_first=True, after=after):
yield message