@@ -111,7 +111,7 @@ def pending_application_commands(self):
111111 def commands (self ) -> list [ApplicationCommand | Any ]:
112112 commands = self .application_commands
113113 if self ._bot ._supports_prefixed_commands and hasattr (self ._bot , "prefixed_commands" ):
114- commands += getattr ( self ._bot , " prefixed_commands" )
114+ commands += self ._bot . prefixed_commands
115115 return commands
116116
117117 @property
@@ -265,7 +265,7 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
265265 if isinstance (cmd , SlashCommandGroup ):
266266 if len (cmd .subcommands ) != len (match .get ("options" , [])):
267267 return True
268- for i , subcommand in enumerate ( cmd .subcommands ) :
268+ for subcommand in cmd .subcommands :
269269 match_ = next (
270270 (data for data in match ["options" ] if data ["name" ] == subcommand .name ),
271271 MISSING ,
@@ -357,8 +357,9 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
357357 return_value .append ({"command" : cmd , "action" : None , "id" : int (match ["id" ])})
358358
359359 # Now let's see if there are any commands on discord that we need to delete
360- for cmd , value_ in registered_commands_dict .items ():
361- match = find (lambda c : c .name == value_ ["name" ], pending )
360+ for _ , value_ in registered_commands_dict .items ():
361+ # name default arg is used because loop variables leak in surrounding scope
362+ match = find (lambda c , name = value_ ["name" ]: c .name == name , pending )
362363 if match is None :
363364 # We have this command registered but not in our list
364365 return_value .append (
@@ -517,7 +518,11 @@ def register(
517518 )
518519 continue
519520 # We can assume the command item is a command, since it's only a string if action is delete
520- match = find (lambda c : c .name == cmd ["command" ].name and c .type == cmd ["command" ].type , pending )
521+ wanted = cmd ["command" ]
522+ name = wanted .name
523+ type_ = wanted .type
524+
525+ match = next ((c for c in pending if c .name == name and c .type == type_ ), None )
521526 if match is None :
522527 continue
523528 if cmd ["action" ] == "edit" :
@@ -606,8 +611,10 @@ def register(
606611 registered = await register ("bulk" , data , guild_id = guild_id )
607612
608613 for i in registered :
614+ type_ = i .get ("type" )
615+ # name, type_ default args are used because loop variables leak in surrounding scope
609616 cmd = find (
610- lambda c : c .name == i [ " name" ] and c .type == i . get ( "type" ) ,
617+ lambda c , name = i [ "name" ], type_ = type_ : c .name == name and c .type == type_ ,
611618 self .pending_application_commands ,
612619 )
613620 if not cmd :
@@ -624,7 +631,7 @@ async def sync_commands(
624631 force : bool = False ,
625632 guild_ids : list [int ] | None = None ,
626633 register_guild_commands : bool = True ,
627- check_guilds : list [int ] | None = [] ,
634+ check_guilds : list [int ] | None = None ,
628635 delete_existing : bool = True ,
629636 ) -> None :
630637 """|coro|
@@ -711,25 +718,37 @@ async def on_connect():
711718 )
712719 registered_guild_commands [guild_id ] = app_cmds
713720
714- for i in registered_commands :
721+ for item in registered_commands :
722+ type_ = item .get ("type" )
723+ # name, type_ default args are used because loop variables leak in surrounding scope
715724 cmd = find (
716- lambda c : c .name == i [ " name" ] and c .guild_ids is None and c .type == i . get ( "type" ),
725+ lambda c , name = item [ "name" ], type_ = type_ : ( c .name == name and c .guild_ids is None and c .type == type_ ),
717726 self .pending_application_commands ,
718727 )
719728 if cmd :
720- cmd .id = i ["id" ]
729+ cmd .id = item ["id" ]
721730 self ._application_commands [cmd .id ] = cmd
722731
723732 if register_guild_commands and registered_guild_commands :
724733 for guild_id , guild_cmds in registered_guild_commands .items ():
725734 for i in guild_cmds :
726- cmd = find (
727- lambda cmd : cmd .name == i ["name" ]
728- and cmd .type == i .get ("type" )
729- and cmd .guild_ids is not None
730- and (guild_id := i .get ("guild_id" ))
731- and guild_id in cmd .guild_ids ,
732- self .pending_application_commands ,
735+ name = i ["name" ]
736+ type_ = i .get ("type" )
737+ target_gid = i .get ("guild_id" )
738+ if target_gid is None :
739+ continue
740+
741+ cmd = next (
742+ (
743+ c
744+ for c in self .pending_application_commands
745+ if c .name == name
746+ and c .type == type_
747+ and c .guild_ids is not None
748+ and target_gid == guild_id
749+ and target_gid in c .guild_ids
750+ ),
751+ None ,
733752 )
734753 if not cmd :
735754 # command has not been added yet
0 commit comments