@@ -249,8 +249,9 @@ async def dispatch_error(self, ctx: Context, error: CommandError) -> None:
249249
250250 if not hasattr (self .on_error , "__doc_only__" ):
251251 injected = wrap_callback (self .on_error )
252- if self ._needs_cog (self .on_error ):
253- await injected (cog , ctx , error )
252+ _earg = self ._get_extra_arg (self .on_error )
253+ if _earg :
254+ await injected (_earg , ctx , error )
254255 await injected (ctx , error )
255256
256257 if cogcmd is not None :
@@ -266,30 +267,40 @@ async def dispatch_error(self, ctx: Context, error: CommandError) -> None:
266267 ctx .bot .dispatch ('command_error' , ctx , error )
267268
268269 def _needs_ccmd (self , func : Callable ) -> bool :
269- if self .cogcmd is not None :
270- if self .__class__ not in self .cogcmd .__class__ .__dict__ .values ():
271- if str (self .cogcmd .__class__ ) in func .__qualname__ :
272- return True
270+ if self .cogcmd is not None and not isinstance (func , MethodType ):
271+ try :
272+ par = func .__qualname__ .split ("." )[- 2 ]
273+ except IndexError :
274+ return False
275+ if self .cogcmd .__class__ .__name__ == par :
276+ return True
273277 return False
274278
275279 def _needs_cog (self , func : Union [AsyncCallable , MethodType ]) -> bool :
276- if self .cog is not None :
277- if isinstance (func , MethodType ): # Helps typing
278- if func .__self__ == self :
279- return False
280- return True
281- elif func in self .__class__ .__dict__ .values ():
280+ if self .cog is not None and not isinstance (func , MethodType ):
281+ try :
282+ par = func .__qualname__ .split ("." )[- 2 ]
283+ except IndexError :
282284 return False
283- return True
285+ if self .cog .__class__ .__name__ == par :
286+ return True
287+ return False
284288 return False
285289
290+ def _get_extra_arg (self , func : Callable ) -> Union [Cog , CCmd , None ]:
291+ if self ._needs_ccmd (func ):
292+ return self .cogcmd
293+ if self ._needs_cog (func ):
294+ return self .cog
295+ return None
296+
286297 @property
287298 def clean_params (self ) -> Mapping [str , Parameter ]:
288299 """:meta private:""" # Has Been documented in dpy.
289300 result = self .params .copy ()
290301
291302 if not ismethod (self .callback ):
292- if self ._needs_cog (self .callback ) or self ._needs_ccmd (self .callback ):
303+ if self ._needs_cog (self .callback ) or self ._needs_ccmd (self .callback ):
293304 result .popitem (last = False ) # self
294305
295306 try :
@@ -299,16 +310,16 @@ def clean_params(self) -> Mapping[str, Parameter]:
299310 return result
300311
301312 async def _parse_arguments (self , ctx : Context ) -> None :
302- _cog = self ._needs_cog (self .callback )
303- ctx .args = [( self . cogcmd , self . cog )[ _cog ], ctx ] if _cog or self . _needs_ccmd ( self . callback ) else [ctx ]
313+ _earg = self ._get_extra_arg (self .callback )
314+ ctx .args = [_earg , ctx ] if _earg else [ctx ]
304315 ctx .kwargs = {}
305316 args = ctx .args
306317 kwargs = ctx .kwargs
307318
308319 view = ctx .view
309320 iterator = iter (self .params .items ())
310321
311- if _cog :
322+ if _earg :
312323 # we have 'self' as the first parameter so just advance
313324 # the iterator and resume parsing
314325 try :
@@ -367,8 +378,9 @@ async def call_before_hooks(self, ctx: Context) -> None:
367378 await self .call_if_overridden (cogcmd .subcommand_before_invoke , ctx )
368379
369380 if self ._before_invoke is not None :
370- if self ._needs_cog (self ._before_invoke ):
371- _arg : Union [Tuple [Optional [Cog ], Context ], Tuple [Context ]] = (cog , ctx )
381+ _earg = self ._get_extra_arg (self ._before_invoke )
382+ if _earg :
383+ _arg : Union [Tuple [Union [Cog , CCmd , None ], Context ], Tuple [Context ]] = (_earg , ctx )
372384 else :
373385 _arg = (ctx ,)
374386 await self .call_if_overridden (self ._before_invoke , * _arg )
@@ -377,8 +389,9 @@ async def call_after_hooks(self, ctx: Context) -> None:
377389 cog = self .cog
378390 cogcmd = self .cogcmd
379391 if self ._after_invoke is not None :
380- if self ._needs_cog (self ._after_invoke ):
381- _arg : Union [Tuple [Optional [Cog ], Context ], Tuple [Context ]] = (cog , ctx )
392+ _earg = self ._get_extra_arg (self ._after_invoke )
393+ if _earg :
394+ _arg : Union [Tuple [Union [Cog , CCmd , None ], Context ], Tuple [Context ]] = (_earg , ctx )
382395 else :
383396 _arg = (ctx ,)
384397 await self .call_if_overridden (self ._after_invoke , * _arg )
0 commit comments