@@ -52,11 +52,8 @@ async def get_slowmode(self, ctx: Context, channel: MessageHolder) -> None:
52
52
channel = ctx .channel
53
53
54
54
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 :
60
57
await ctx .send (
61
58
f"The slowmode delay for { channel .mention } is { humanized_delay } "
62
59
f" and will revert to { humanized_original_delay } { expiration_timestamp } ."
@@ -87,7 +84,7 @@ async def set_slowmode(
87
84
if isinstance (delay , str ):
88
85
delay = relativedelta (seconds = 0 )
89
86
90
- slowmode_delay = time .relativedelta_to_timedelta (delay ).total_seconds ()
87
+ slowmode_delay = int ( time .relativedelta_to_timedelta (delay ).total_seconds () )
91
88
humanized_delay = time .humanize_delta (delay )
92
89
93
90
# Ensure the delay is within discord's limits
@@ -105,15 +102,14 @@ async def set_slowmode(
105
102
if expiry is not None :
106
103
expiration_timestamp = time .format_relative (expiry )
107
104
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 )
111
110
else :
112
- cached_data = await self .slowmode_cache .get (channel .id )
113
- delay_to_cache = cached_data .split (", " )[0 ]
114
111
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 } " )
117
113
118
114
self .scheduler .schedule_at (expiry , channel .id , self ._revert_slowmode (channel .id ))
119
115
log .info (
@@ -148,17 +144,34 @@ async def _reschedule(self) -> None:
148
144
log .info (f"Rescheduling slowmode expiration for #{ channel } ({ channel_id } )." )
149
145
self .scheduler .schedule_at (expiration_datetime , channel_id , self ._revert_slowmode (channel_id ))
150
146
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
+
151
164
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 )
155
166
channel = await get_or_fetch_channel (self .bot , channel_id )
156
167
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 )
159
172
await mod_channel .send (
160
173
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 } ."
162
175
)
163
176
await self .slowmode_cache .delete (channel .id )
164
177
0 commit comments