55from dateutil .relativedelta import relativedelta
66from discord import TextChannel , Thread
77from discord .ext .commands import Cog , Context , group , has_any_role
8+ from pydis_core .utils .channel import get_or_fetch_channel
89from pydis_core .utils .scheduling import Scheduler
910
1011from bot .bot import Bot
1112from bot .constants import Channels , Emojis , MODERATION_ROLES
1213from bot .converters import Duration , DurationDelta
1314from bot .log import get_logger
1415from bot .utils import time
15- from bot .utils .time import TimestampFormats , discord_timestamp
16+ from bot .utils .time import format_relative , humanize_delta
1617
1718log = 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."""
0 commit comments