@@ -175,13 +175,16 @@ def block_duplicate_invocations(
175
175
* ,
176
176
cooldown_duration : float = 5 ,
177
177
send_notice : bool = False ,
178
+ args_preprocessor : Callable [P , Iterable [object ]] | None = None ,
178
179
) -> Callable [[Callable [P , Awaitable [R ]]], Callable [P , Awaitable [R ]]]:
179
180
"""
180
181
Prevent duplicate invocations of a command with the same arguments in a channel for ``cooldown_duration`` seconds.
181
182
182
183
Args:
183
184
cooldown_duration: Length of the cooldown in seconds.
184
185
send_notice: If :obj:`True`, notify the user about the cooldown with a reply.
186
+ args_preprocessor: If specified, this function is called with the args and kwargs the function is called with,
187
+ its return value is then used to check for the cooldown instead of the raw arguments.
185
188
186
189
Returns:
187
190
A decorator that adds a wrapper which applies the cooldowns.
@@ -195,16 +198,19 @@ def decorator(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
195
198
196
199
@command_wraps (func )
197
200
async def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> R :
198
- arg_tuple = _create_argument_tuple (* args [2 :], ** kwargs ) # skip self and ctx from the command
201
+ if args_preprocessor is not None :
202
+ all_args = args_preprocessor (* args , ** kwargs )
203
+ else :
204
+ all_args = _create_argument_tuple (* args [2 :], ** kwargs ) # skip self and ctx from the command
199
205
ctx = typing .cast ("Context[BotBase]" , args [1 ])
200
206
201
207
if not isinstance (ctx .channel , discord .DMChannel ):
202
- if mgr .is_on_cooldown (ctx .channel , arg_tuple ):
208
+ if mgr .is_on_cooldown (ctx .channel , all_args ):
203
209
if send_notice :
204
210
with suppress (discord .NotFound ):
205
211
await ctx .reply ("The command is on cooldown with the given arguments." )
206
212
raise CommandOnCooldown (ctx .message .content , func , * args , ** kwargs )
207
- mgr .set_cooldown (ctx .channel , arg_tuple )
213
+ mgr .set_cooldown (ctx .channel , all_args )
208
214
209
215
return await func (* args , ** kwargs )
210
216
0 commit comments