@@ -111,7 +111,7 @@ def pending_application_commands(self):
111
111
def commands (self ) -> list [ApplicationCommand | Any ]:
112
112
commands = self .application_commands
113
113
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
115
115
return commands
116
116
117
117
@property
@@ -265,7 +265,7 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
265
265
if isinstance (cmd , SlashCommandGroup ):
266
266
if len (cmd .subcommands ) != len (match .get ("options" , [])):
267
267
return True
268
- for i , subcommand in enumerate ( cmd .subcommands ) :
268
+ for subcommand in cmd .subcommands :
269
269
match_ = next (
270
270
(data for data in match ["options" ] if data ["name" ] == subcommand .name ),
271
271
MISSING ,
@@ -357,8 +357,9 @@ def _check_command(cmd: ApplicationCommand, match: Mapping[str, Any]) -> bool:
357
357
return_value .append ({"command" : cmd , "action" : None , "id" : int (match ["id" ])})
358
358
359
359
# 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 )
362
363
if match is None :
363
364
# We have this command registered but not in our list
364
365
return_value .append (
@@ -517,7 +518,11 @@ def register(
517
518
)
518
519
continue
519
520
# 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 )
521
526
if match is None :
522
527
continue
523
528
if cmd ["action" ] == "edit" :
@@ -606,8 +611,10 @@ def register(
606
611
registered = await register ("bulk" , data , guild_id = guild_id )
607
612
608
613
for i in registered :
614
+ type_ = i .get ("type" )
615
+ # name, type_ default args are used because loop variables leak in surrounding scope
609
616
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_ ,
611
618
self .pending_application_commands ,
612
619
)
613
620
if not cmd :
@@ -624,7 +631,7 @@ async def sync_commands(
624
631
force : bool = False ,
625
632
guild_ids : list [int ] | None = None ,
626
633
register_guild_commands : bool = True ,
627
- check_guilds : list [int ] | None = [] ,
634
+ check_guilds : list [int ] | None = None ,
628
635
delete_existing : bool = True ,
629
636
) -> None :
630
637
"""|coro|
@@ -711,25 +718,37 @@ async def on_connect():
711
718
)
712
719
registered_guild_commands [guild_id ] = app_cmds
713
720
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
715
724
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_ ),
717
726
self .pending_application_commands ,
718
727
)
719
728
if cmd :
720
- cmd .id = i ["id" ]
729
+ cmd .id = item ["id" ]
721
730
self ._application_commands [cmd .id ] = cmd
722
731
723
732
if register_guild_commands and registered_guild_commands :
724
733
for guild_id , guild_cmds in registered_guild_commands .items ():
725
734
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 ,
733
752
)
734
753
if not cmd :
735
754
# command has not been added yet
0 commit comments