Skip to content

Commit 17a83cb

Browse files
committed
General refactoring, change message formatting, update tests
1 parent 505e851 commit 17a83cb

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

bot/exts/moderation/slowmode.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from dateutil.relativedelta import relativedelta
66
from discord import TextChannel, Thread
77
from discord.ext.commands import Cog, Context, group, has_any_role
8+
from pydis_core.utils.channel import get_or_fetch_channel
89
from pydis_core.utils.scheduling import Scheduler
910

1011
from bot.bot import Bot
1112
from bot.constants import Channels, Emojis, MODERATION_ROLES
1213
from bot.converters import Duration, DurationDelta
1314
from bot.log import get_logger
1415
from bot.utils import time
15-
from bot.utils.time import TimestampFormats, discord_timestamp
16+
from bot.utils.time import format_relative, humanize_delta
1617

1718
log = get_logger(__name__)
1819

@@ -52,11 +53,14 @@ async def get_slowmode(self, ctx: Context, channel: MessageHolder) -> None:
5253
channel = ctx.channel
5354

5455
humanized_delay = time.humanize_delta(seconds=channel.slowmode_delay)
55-
if await self.slowmode_cache.contains(channel.id):
56-
expiration_time = await self.slowmode_cache.get(channel.id).split(", ")[1]
57-
expiration_timestamp = discord_timestamp(expiration_time, TimestampFormats.RELATIVE)
56+
cached_data = await self.slowmode_cache.get(channel.id, None)
57+
if cached_data is not None:
58+
original_delay, expiration_time = cached_data.partition(", ")
59+
humanized_original_delay = time.humanize_delta(seconds=int(original_delay))
60+
expiration_timestamp = format_relative(expiration_time)
5861
await ctx.send(
59-
f"The slowmode delay for {channel.mention} is {humanized_delay} and expires in {expiration_timestamp}."
62+
f"The slowmode delay for {channel.mention} is {humanized_delay}"
63+
f" and will revert to {humanized_original_delay} {expiration_timestamp}."
6064
)
6165
else:
6266
await ctx.send(f"The slowmode delay for {channel.mention} is {humanized_delay}.")
@@ -88,7 +92,7 @@ async def set_slowmode(
8892
humanized_delay = time.humanize_delta(delay)
8993

9094
# Ensure the delay is within discord's limits
91-
if not slowmode_delay <= SLOWMODE_MAX_DELAY:
95+
if slowmode_delay > SLOWMODE_MAX_DELAY:
9296
log.info(
9397
f"{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, "
9498
"which is not between 0 and 6 hours."
@@ -100,27 +104,28 @@ async def set_slowmode(
100104
return
101105

102106
if expiry is not None:
103-
humanized_expiry = time.humanize_delta(expiry)
104-
expiration_timestamp = discord_timestamp(expiry, TimestampFormats.RELATIVE)
107+
expiration_timestamp = format_relative(expiry)
105108

106109
# Only cache the original slowmode delay if there is not already an ongoing temporary slowmode.
107110
if not await self.slowmode_cache.contains(channel.id):
108-
await self.slowmode_cache.set(channel.id, f"{channel.slowmode_delay}, {expiry}")
111+
delay_to_cache = channel.slowmode_delay
109112
else:
110-
cached_delay = await self.slowmode_cache.get(channel.id)
111-
await self.slowmode_cache.set(channel.id, f"{cached_delay}, {expiry}")
113+
cached_data = await self.slowmode_cache.get(channel.id)
114+
delay_to_cache = cached_data.split(", ")[0]
112115
self.scheduler.cancel(channel.id)
116+
await self.slowmode_cache.set(channel.id, f"{delay_to_cache}, {expiry}")
117+
humanized_original_delay = humanize_delta(seconds=int(delay_to_cache))
113118

114119
self.scheduler.schedule_at(expiry, channel.id, self._revert_slowmode(channel.id))
115120
log.info(
116-
f"{ctx.author} set the slowmode delay for #{channel} to"
117-
f"{humanized_delay} which expires in {humanized_expiry}."
121+
f"{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}"
122+
f" which will revert to {humanized_original_delay} in {humanize_delta(expiry)}."
118123
)
119124
await channel.edit(slowmode_delay=slowmode_delay)
120125
await ctx.send(
121-
f"{Emojis.check_mark} The slowmode delay for {channel.mention}"
122-
f" is now {humanized_delay} and expires in {expiration_timestamp}."
123-
)
126+
f"{Emojis.check_mark} The slowmode delay for {channel.mention}"
127+
f" is now {humanized_delay} and will revert to {humanized_original_delay} {expiration_timestamp}."
128+
)
124129
else:
125130
if await self.slowmode_cache.contains(channel.id):
126131
await self.slowmode_cache.delete(channel.id)
@@ -148,23 +153,20 @@ async def _revert_slowmode(self, channel_id: int) -> None:
148153
cached_data = await self.slowmode_cache.get(channel_id)
149154
original_slowmode = int(cached_data.split(", ")[0])
150155
slowmode_delay = time.humanize_delta(seconds=original_slowmode)
151-
channel = self.bot.get_channel(channel_id)
152-
log.info(f"Slowmode in #{channel} ({channel.id}) has expired and has reverted to {slowmode_delay}.")
156+
channel = await get_or_fetch_channel(self.bot, channel_id)
157+
mod_channel = await get_or_fetch_channel(self.bot, Channels.mods)
158+
log.info(f"Slowmode in #{channel.name} ({channel.id}) has expired and has reverted to {slowmode_delay}.")
153159
await channel.edit(slowmode_delay=original_slowmode)
154-
await channel.send(
155-
f"{Emojis.check_mark} A previously applied slowmode has expired and has been reverted to {slowmode_delay}."
160+
await mod_channel.send(
161+
f"{Emojis.check_mark} A previously applied slowmode in {channel.jump_url} ({channel.id})"
162+
f" has expired and has been reverted to {slowmode_delay}."
156163
)
157164
await self.slowmode_cache.delete(channel.id)
158165

159166
@slowmode_group.command(name="reset", aliases=["r"])
160167
async def reset_slowmode(self, ctx: Context, channel: MessageHolder) -> None:
161168
"""Reset the slowmode delay for a text channel to 0 seconds."""
162169
await self.set_slowmode(ctx, channel, relativedelta(seconds=0))
163-
if channel is None:
164-
channel = ctx.channel
165-
if await self.slowmode_cache.contains(channel.id):
166-
await self.slowmode_cache.delete(channel.id)
167-
self.scheduler.cancel(channel.id)
168170

169171
async def cog_check(self, ctx: Context) -> bool:
170172
"""Only allow moderators to invoke the commands in this cog."""

tests/bot/exts/moderation/test_slowmode.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ async def test_set_slowmode_with_expiry(self, mock_datetime) -> None:
104104
mock_datetime.now.return_value = fixed_datetime
105105

106106
test_cases = (
107-
("python-general", 6, 6000, f"{Emojis.check_mark} The slowmode delay for #python-general is now 6 seconds"
108-
" and expires in <t:1748871600:R>."),
109-
("mod-spam", 5, 600, f"{Emojis.check_mark} The slowmode delay for #mod-spam is now 5 seconds and expires"
110-
" in <t:1748866200:R>."),
111-
("changelog", 12, 7200, f"{Emojis.check_mark} The slowmode delay for #changelog is now 12 seconds and"
112-
" expires in <t:1748872800:R>.")
107+
("python-general", 6, 6000, f"{Emojis.check_mark} The slowmode delay for #python-general is now 6 seconds "
108+
"and will revert to 0 seconds <t:1748871600:R>."),
109+
("mod-spam", 5, 600, f"{Emojis.check_mark} The slowmode delay for #mod-spam is now 5 seconds and will "
110+
"revert to 0 seconds <t:1748866200:R>."),
111+
("changelog", 12, 7200, f"{Emojis.check_mark} The slowmode delay for #changelog is now 12 seconds and will "
112+
"revert to 0 seconds <t:1748872800:R>.")
113113
)
114114
for channel_name, seconds, expiry, result_msg in test_cases:
115115
with self.subTest(
@@ -147,10 +147,15 @@ async def test_callback_scheduled(self):
147147
args = (expiry, text_channel.id, mock.ANY)
148148
self.cog.scheduler.schedule_at.assert_called_once_with(*args)
149149

150-
async def test_revert_slowmode_callback(self) -> None:
150+
@mock.patch("bot.exts.moderation.slowmode.get_or_fetch_channel")
151+
async def test_revert_slowmode_callback(self, mock_get_or_fetch_channel) -> None:
151152
"""Check that the slowmode is reverted"""
152-
text_channel = MockTextChannel(name="python-general", slowmode_delay=2, id=123)
153-
self.bot.get_channel = mock.MagicMock(return_value=text_channel)
153+
text_channel = MockTextChannel(name="python-general", slowmode_delay=2, id=123, jump_url="#python-general")
154+
mod_channel = MockTextChannel(name="mods", id=999, )
155+
# mock.MagicMock(return_value=text_channel)
156+
157+
mock_get_or_fetch_channel.side_effect = [text_channel, mod_channel]
158+
154159
await self.cog.set_slowmode(
155160
self.cog,
156161
self.ctx,
@@ -160,8 +165,9 @@ async def test_revert_slowmode_callback(self) -> None:
160165
)
161166
await self.cog._revert_slowmode(text_channel.id)
162167
text_channel.edit.assert_awaited_with(slowmode_delay=2)
163-
text_channel.send.assert_called_once_with(
164-
f"{Emojis.check_mark} A previously applied slowmode has expired and has been reverted to 2 seconds."
168+
mod_channel.send.assert_called_once_with(
169+
f"{Emojis.check_mark} A previously applied slowmode in {text_channel.jump_url} ({text_channel.id}) "
170+
"has expired and has been reverted to 2 seconds."
165171
)
166172

167173
async def test_reschedule_slowmodes(self) -> None:

0 commit comments

Comments
 (0)