-
-
Notifications
You must be signed in to change notification settings - Fork 715
Implemented optional duration parameter in slowmode command #3331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@mock.patch("bot.exts.moderation.slowmode.datetime", wraps=datetime.datetime) | ||
async def test_reschedules_slowmodes(self, mock_datetime) -> None: | ||
"""Slowmodes are loaded from cache at cog reload and scheduled to be reverted.""" | ||
mock_datetime.now.return_value = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC) | ||
mock_now = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC) | ||
self.cog._reschedule = mock.AsyncMock(wraps=self.cog._reschedule) | ||
|
||
channels = [] | ||
slowmodes = ( | ||
(123, (mock_now - datetime.timedelta(10)).timestamp(), 2), # expiration in the past | ||
(456, (mock_now + datetime.timedelta(10)).timestamp(), 4), # expiration in the future | ||
) | ||
|
||
for channel_id, expiration_datetime, delay in slowmodes: | ||
channel = MockTextChannel(slowmode_delay=delay, id=channel_id) | ||
channels.append(channel) | ||
|
||
await self.cog.slowmode_expiration_cache.set(channel_id, expiration_datetime) | ||
await self.cog.original_slowmode_cache.set(channel_id, delay) | ||
|
||
await self.cog.cog_unload() | ||
await self.cog.cog_load() | ||
|
||
# check that _reschedule function was called upon cog reload. | ||
self.cog._reschedule.assert_called() | ||
|
||
# check that a task was created for every cached slowmode. | ||
for channel in channels: | ||
self.assertIn(channel.id, self.cog.scheduler) | ||
|
||
# check that one channel with slowmode expiration in the past was edited immediately. | ||
channels[0].edit.assert_awaited_once_with(slowmode_delay=channels[0].slowmode_delay) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test checks if temporary slowmodes in cache are rescheduled after the cog is reloaded but I'm having trouble with getting the assertion on line 203 to pass. I mock two channels, one where the slowmode is already expired by the time the cog is loaded, and one where it is not expired. The task for the channel where it is expired should immediately callback the func that removes the slowmode. On line 200 I check that two tasks are created, and they are. On line 203 I check if the slowmode was changed and the assertion fails, indicating that the channel was not edited.
I'm not sure if it's something small that I'm missing, or if it's the way that i've mocked everything, or if it's just a consequence of me not really understanding how these events and tasks are handled. I'd appreciate if someone could have a look 🥺
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are creating MockTextChannel
s, but _revert_slowmode
will create it's own mocks from calling self.bot.get_channel(id)
. Those are the mocks that will have edit
called on them, not your ones.
Not sure what the easiest fix would be.
Closes #3327.
Implemented an optional
duration
argument for the slowmode command. If the duration argument is present, the intended expiration datetime and the original slowmode interval are saved in redis cache. After the duration is elapsed, the original slowmode interval is grabbed from redis and restored to the channel.The expiration datetime is cached so that it can persist bot restart.