Skip to content

Clean improvements#3535

Open
mbaruh wants to merge 9 commits into
mainfrom
clean-improvements
Open

Clean improvements#3535
mbaruh wants to merge 9 commits into
mainfrom
clean-improvements

Conversation

@mbaruh

@mbaruh mbaruh commented Jul 4, 2026

Copy link
Copy Markdown
Member

No description provided.

@mbaruh mbaruh requested a review from MarkKoz as a code owner July 4, 2026 21:56
@mbaruh mbaruh added a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 2 - normal Normal Priority t: enhancement Changes or improvements to existing features labels Jul 4, 2026
Comment thread bot/utils/async_utils.py Outdated
self._running_tasks = set()

def submit[T](self, coro: Awaitable[T]) -> asyncio.Task[T]:
"""Wraps the coroutine with _semaphore logic, schedules it on the event loop, and ensures cleanup."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use imperative mood for docs "Wrap the coroutine" rather than "Wraps the coroutine".

Also, _semaphore is an internal detail so it shouldn't be directly referenced in the docstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah the underscore is because of a refactor, but yeah don't have to mention how it's implemented.

Comment thread bot/utils/async_utils.py Outdated

async def gather(self, return_exceptions: bool = False) -> list[Any]:
"""Waits for all submitted coroutines to finish execution."""
if self._running_tasks:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this check is redundant

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I thought an empty asyncio.gather raised an error, I guess not

Comment thread bot/utils/async_utils.py
await self._event.wait()


class AsyncExecutor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a public interface for cancelling the tasks? Might be needed if the bot is shutting down.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

"""Return the datetime of the earliest message cached, or now if the cache is empty."""
return self.bot.cached_messages[0].created_at if self.bot.cached_messages else arrow.utcnow().datetime

def _use_cache(self, most_recent_limit: datetime | None) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Inconsistent terminology in this file. There's earliest/latest, lower/upper, first/second, and least/most recent.

"""Helper function for getting messages from the cache."""
message_mappings = defaultdict(list)
message_ids = []
message_mappings = {channel: [] for channel in channels}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not leave it as a defaultdict?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To preserve channel order

self,
channels: Iterable[TextChannel],
executor: AsyncExecutor,
*,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: why make them keyword-only?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having this many distinct positional arguments, and the function was being called with kw already anyway

Comment thread bot/exts/moderation/clean.py Outdated
continue

messages_to_delete.append(message)
message_ids.append(message.id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: message IDs can be derived from messages_to_delete. I think it'd be cleaner to not maintain parallel lists, and that it wouldn't be a significant change in performance.

if self.is_older_than_14d(message):
# Further messages are too old to be deleted in bulk
delete_old = True
old_messages[channel] = messages[current_index:]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add to the docstring that this function expects messages to be sorted by timestamp.

# Means that the cleaning was canceled
return None
if old_messages:
old_deleted = await self._delete_messages_individually(old_messages)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do old messages need to be deleted individually?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API limitation

@mbaruh mbaruh requested a review from MarkKoz July 5, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 2 - normal Normal Priority t: enhancement Changes or improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants