Skip to content

Commit fdd70ae

Browse files
authored
feat: allow .purge to return messages (#1396)
Closes #1390
1 parent 0b98045 commit fdd70ae

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

interactions/models/discord/channel.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,12 @@ async def purge(
404404
search_limit: int = 100,
405405
predicate: Callable[["models.Message"], bool] = MISSING,
406406
avoid_loading_msg: bool = True,
407+
return_messages: bool = False,
407408
before: Optional[Snowflake_Type] = MISSING,
408409
after: Optional[Snowflake_Type] = MISSING,
409410
around: Optional[Snowflake_Type] = MISSING,
410411
reason: Absent[Optional[str]] = MISSING,
411-
) -> int:
412+
) -> int | List["models.Message"]:
412413
"""
413414
Bulk delete messages within a channel. If a `predicate` is provided, it will be used to determine which messages to delete, otherwise all messages will be deleted within the `deletion_limit`.
414415
@@ -424,6 +425,7 @@ async def purge(
424425
search_limit: How many messages to search through
425426
predicate: A function that returns True or False, and takes a message as an argument
426427
avoid_loading_msg: Should the bot attempt to avoid deleting its own loading messages (recommended enabled)
428+
return_messages: Should the bot return the messages that were deleted
427429
before: Search messages before this ID
428430
after: Search messages after this ID
429431
around: Search messages around this ID
@@ -461,13 +463,13 @@ def predicate(m) -> bool:
461463
# message is too old to be purged
462464
continue
463465

464-
to_delete.append(message.id)
466+
to_delete.append(message)
465467

466-
count = len(to_delete)
468+
out = to_delete.copy()
467469
while len(to_delete):
468-
iteration = [to_delete.pop() for i in range(min(100, len(to_delete)))]
470+
iteration = [to_delete.pop().id for i in range(min(100, len(to_delete)))]
469471
await self.delete_messages(iteration, reason=reason)
470-
return count
472+
return out if return_messages else len(out)
471473

472474
async def trigger_typing(self) -> None:
473475
"""Trigger a typing animation in this channel."""

0 commit comments

Comments
 (0)