@@ -113,7 +113,7 @@ def pending_application_commands(self):
113113 def commands (self ) -> list [ApplicationCommand | Any ]:
114114 commands = self .application_commands
115115 if self ._bot ._supports_prefixed_commands and hasattr (self ._bot , "prefixed_commands" ):
116- commands += getattr ( self ._bot , " prefixed_commands" )
116+ commands += self ._bot . prefixed_commands
117117 return commands
118118
119119 @property
@@ -267,7 +267,7 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
267267 if isinstance (cmd , SlashCommandGroup ):
268268 if len (cmd .subcommands ) != len (match .get ("options" , [])):
269269 return True
270- for i , subcommand in enumerate ( cmd .subcommands ) :
270+ for subcommand in cmd .subcommands :
271271 match_ = next (
272272 (data for data in match ["options" ] if data ["name" ] == subcommand .name ),
273273 MISSING ,
@@ -359,8 +359,9 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
359359 return_value .append ({"command" : cmd , "action" : None , "id" : int (match ["id" ])})
360360
361361 # Now let's see if there are any commands on discord that we need to delete
362- for cmd , value_ in registered_commands_dict .items ():
363- match = find (lambda c : c .name == value_ ["name" ], pending )
362+ for _ , value_ in registered_commands_dict .items ():
363+ # name default arg is used because loop variables leak in surrounding scope
364+ match = find (lambda c , name = value_ ["name" ]: c .name == name , pending )
364365 if match is None :
365366 # We have this command registered but not in our list
366367 return_value .append (
@@ -519,7 +520,11 @@ def register(
519520 )
520521 continue
521522 # We can assume the command item is a command, since it's only a string if action is delete
522- match = find (lambda c : c .name == cmd ["command" ].name and c .type == cmd ["command" ].type , pending )
523+ wanted = cmd ["command" ]
524+ name = wanted .name
525+ type_ = wanted .type
526+
527+ match = next ((c for c in pending if c .name == name and c .type == type_ ), None )
523528 if match is None :
524529 continue
525530 if cmd ["action" ] == "edit" :
@@ -608,8 +613,10 @@ def register(
608613 registered = await register ("bulk" , data , guild_id = guild_id )
609614
610615 for i in registered :
616+ type_ = i .get ("type" )
617+ # name, type_ default args are used because loop variables leak in surrounding scope
611618 cmd = find (
612- lambda c : c .name == i [ " name" ] and c .type == i . get ( "type" ) ,
619+ lambda c , name = i [ "name" ], type_ = type_ : c .name == name and c .type == type_ ,
613620 self .pending_application_commands ,
614621 )
615622 if not cmd :
@@ -626,7 +633,7 @@ async def sync_commands(
626633 force : bool = False ,
627634 guild_ids : list [int ] | None = None ,
628635 register_guild_commands : bool = True ,
629- check_guilds : list [int ] | None = [] ,
636+ check_guilds : list [int ] | None = None ,
630637 delete_existing : bool = True ,
631638 ) -> None :
632639 """|coro|
@@ -713,25 +720,37 @@ async def on_connect():
713720 )
714721 registered_guild_commands [guild_id ] = app_cmds
715722
716- for i in registered_commands :
723+ for item in registered_commands :
724+ type_ = item .get ("type" )
725+ # name, type_ default args are used because loop variables leak in surrounding scope
717726 cmd = find (
718- lambda c : c .name == i [ " name" ] and c .guild_ids is None and c .type == i . get ( "type" ),
727+ lambda c , name = item [ "name" ], type_ = type_ : ( c .name == name and c .guild_ids is None and c .type == type_ ),
719728 self .pending_application_commands ,
720729 )
721730 if cmd :
722- cmd .id = i ["id" ]
731+ cmd .id = item ["id" ]
723732 self ._application_commands [cmd .id ] = cmd
724733
725734 if register_guild_commands and registered_guild_commands :
726735 for guild_id , guild_cmds in registered_guild_commands .items ():
727736 for i in guild_cmds :
728- cmd = find (
729- lambda cmd : cmd .name == i ["name" ]
730- and cmd .type == i .get ("type" )
731- and cmd .guild_ids is not None
732- and (guild_id := i .get ("guild_id" ))
733- and guild_id in cmd .guild_ids ,
734- self .pending_application_commands ,
737+ name = i ["name" ]
738+ type_ = i .get ("type" )
739+ target_gid = i .get ("guild_id" )
740+ if target_gid is None :
741+ continue
742+
743+ cmd = next (
744+ (
745+ c
746+ for c in self .pending_application_commands
747+ if c .name == name
748+ and c .type == type_
749+ and c .guild_ids is not None
750+ and target_gid == guild_id
751+ and target_gid in c .guild_ids
752+ ),
753+ None ,
735754 )
736755 if not cmd :
737756 # command has not been added yet
0 commit comments