5
5
from dateutil .relativedelta import relativedelta
6
6
from discord import TextChannel , Thread
7
7
from discord .ext .commands import Cog , Context , group , has_any_role
8
+ from pydis_core .utils .channel import get_or_fetch_channel
8
9
from pydis_core .utils .scheduling import Scheduler
9
10
10
11
from bot .bot import Bot
11
12
from bot .constants import Channels , Emojis , MODERATION_ROLES
12
13
from bot .converters import Duration , DurationDelta
13
14
from bot .log import get_logger
14
15
from bot .utils import time
15
- from bot .utils .time import TimestampFormats , discord_timestamp
16
+ from bot .utils .time import format_relative , humanize_delta
16
17
17
18
log = get_logger (__name__ )
18
19
@@ -52,11 +53,14 @@ async def get_slowmode(self, ctx: Context, channel: MessageHolder) -> None:
52
53
channel = ctx .channel
53
54
54
55
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 )
58
61
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 } ."
60
64
)
61
65
else :
62
66
await ctx .send (f"The slowmode delay for { channel .mention } is { humanized_delay } ." )
@@ -88,7 +92,7 @@ async def set_slowmode(
88
92
humanized_delay = time .humanize_delta (delay )
89
93
90
94
# Ensure the delay is within discord's limits
91
- if not slowmode_delay <= SLOWMODE_MAX_DELAY :
95
+ if slowmode_delay > SLOWMODE_MAX_DELAY :
92
96
log .info (
93
97
f"{ ctx .author } tried to set the slowmode delay of #{ channel } to { humanized_delay } , "
94
98
"which is not between 0 and 6 hours."
@@ -100,27 +104,28 @@ async def set_slowmode(
100
104
return
101
105
102
106
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 )
105
108
106
109
# Only cache the original slowmode delay if there is not already an ongoing temporary slowmode.
107
110
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
109
112
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 ]
112
115
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 ))
113
118
114
119
self .scheduler .schedule_at (expiry , channel .id , self ._revert_slowmode (channel .id ))
115
120
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 ) } ."
118
123
)
119
124
await channel .edit (slowmode_delay = slowmode_delay )
120
125
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
+ )
124
129
else :
125
130
if await self .slowmode_cache .contains (channel .id ):
126
131
await self .slowmode_cache .delete (channel .id )
@@ -148,23 +153,20 @@ async def _revert_slowmode(self, channel_id: int) -> None:
148
153
cached_data = await self .slowmode_cache .get (channel_id )
149
154
original_slowmode = int (cached_data .split (", " )[0 ])
150
155
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 } ." )
153
159
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 } ."
156
163
)
157
164
await self .slowmode_cache .delete (channel .id )
158
165
159
166
@slowmode_group .command (name = "reset" , aliases = ["r" ])
160
167
async def reset_slowmode (self , ctx : Context , channel : MessageHolder ) -> None :
161
168
"""Reset the slowmode delay for a text channel to 0 seconds."""
162
169
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 )
168
170
169
171
async def cog_check (self , ctx : Context ) -> bool :
170
172
"""Only allow moderators to invoke the commands in this cog."""
0 commit comments