@@ -52,11 +52,8 @@ async def get_slowmode(self, ctx: Context, channel: MessageHolder) -> None:
5252 channel = ctx .channel
5353
5454 humanized_delay = time .humanize_delta (seconds = channel .slowmode_delay )
55- cached_data = await self .slowmode_cache .get (channel .id , None )
56- if cached_data is not None :
57- original_delay , expiration_time = cached_data .partition (", " )
58- humanized_original_delay = time .humanize_delta (seconds = int (original_delay ))
59- expiration_timestamp = time .format_relative (expiration_time )
55+ original_delay , humanized_original_delay , expiration_timestamp = await self ._fetch_sm_cache (channel .id )
56+ if original_delay is not None :
6057 await ctx .send (
6158 f"The slowmode delay for { channel .mention } is { humanized_delay } "
6259 f" and will revert to { humanized_original_delay } { expiration_timestamp } ."
@@ -87,7 +84,7 @@ async def set_slowmode(
8784 if isinstance (delay , str ):
8885 delay = relativedelta (seconds = 0 )
8986
90- slowmode_delay = time .relativedelta_to_timedelta (delay ).total_seconds ()
87+ slowmode_delay = int ( time .relativedelta_to_timedelta (delay ).total_seconds () )
9188 humanized_delay = time .humanize_delta (delay )
9289
9390 # Ensure the delay is within discord's limits
@@ -105,15 +102,14 @@ async def set_slowmode(
105102 if expiry is not None :
106103 expiration_timestamp = time .format_relative (expiry )
107104
108- # Only cache the original slowmode delay if there is not already an ongoing temporary slowmode.
109- if not await self .slowmode_cache .contains (channel .id ):
110- delay_to_cache = channel .slowmode_delay
105+ original_delay , humanized_original_delay , _ = await self ._fetch_sm_cache (channel .id )
106+ # Cache the channel's current delay if it has no expiry, otherwise use the cached original delay.
107+ if original_delay is None :
108+ original_delay = channel .slowmode_delay
109+ humanized_original_delay = time .humanize_delta (seconds = original_delay )
111110 else :
112- cached_data = await self .slowmode_cache .get (channel .id )
113- delay_to_cache = cached_data .split (", " )[0 ]
114111 self .scheduler .cancel (channel .id )
115- await self .slowmode_cache .set (channel .id , f"{ delay_to_cache } , { expiry } " )
116- humanized_original_delay = time .humanize_delta (seconds = int (delay_to_cache ))
112+ await self .slowmode_cache .set (channel .id , f"{ original_delay } , { expiry } " )
117113
118114 self .scheduler .schedule_at (expiry , channel .id , self ._revert_slowmode (channel .id ))
119115 log .info (
@@ -148,17 +144,34 @@ async def _reschedule(self) -> None:
148144 log .info (f"Rescheduling slowmode expiration for #{ channel } ({ channel_id } )." )
149145 self .scheduler .schedule_at (expiration_datetime , channel_id , self ._revert_slowmode (channel_id ))
150146
147+ async def _fetch_sm_cache (self , channel_id : int ) -> tuple [int | None , str , str ]:
148+ """
149+ Fetch the channel's info from the cache and decode it.
150+
151+ If no cache for the channel, the returned slowmode is None.
152+ """
153+ cached_data = await self .slowmode_cache .get (channel_id , None )
154+ if not cached_data :
155+ return None , "" , ""
156+
157+ original_delay , expiration_time = cached_data .split (", " )
158+ original_delay = int (original_delay )
159+ humanized_original_delay = time .humanize_delta (seconds = original_delay )
160+ expiration_timestamp = time .format_relative (expiration_time )
161+
162+ return original_delay , humanized_original_delay , expiration_timestamp
163+
151164 async def _revert_slowmode (self , channel_id : int ) -> None :
152- cached_data = await self .slowmode_cache .get (channel_id )
153- original_slowmode = int (cached_data .split (", " )[0 ])
154- slowmode_delay = time .humanize_delta (seconds = original_slowmode )
165+ original_delay , humanized_original_delay , _ = await self ._fetch_sm_cache (channel_id )
155166 channel = await get_or_fetch_channel (self .bot , channel_id )
156167 mod_channel = await get_or_fetch_channel (self .bot , Channels .mods )
157- log .info (f"Slowmode in #{ channel .name } ({ channel .id } ) has expired and has reverted to { slowmode_delay } ." )
158- await channel .edit (slowmode_delay = original_slowmode )
168+ log .info (
169+ f"Slowmode in #{ channel .name } ({ channel .id } ) has expired and has reverted to { humanized_original_delay } ."
170+ )
171+ await channel .edit (slowmode_delay = original_delay )
159172 await mod_channel .send (
160173 f"{ Emojis .check_mark } A previously applied slowmode in { channel .jump_url } ({ channel .id } )"
161- f" has expired and has been reverted to { slowmode_delay } ."
174+ f" has expired and has been reverted to { humanized_original_delay } ."
162175 )
163176 await self .slowmode_cache .delete (channel .id )
164177
0 commit comments