@@ -592,6 +592,13 @@ async def _cache_application_commands(self) -> None:
592
592
593
593
_ , guilds = self ._ordered_unsynced_commands (self ._test_guilds )
594
594
595
+ # Here we only cache global commands and commands from guilds that are spcified in the code.
596
+ # They're collected from the "test_guilds" kwarg of commands.InteractionBotBase
597
+ # and the "guild_ids" kwarg of the decorators. This is the only way to avoid rate limits.
598
+ # If we cache guild commands from everywhere, the limit of invalid requests gets exhausted.
599
+ # This is because we don't know the scopes of the app in all guilds in advance, so we'll have to
600
+ # catch a lot of "Forbidden" errors, exceeding the limit of 10k invalid requests in 10 minutes (for large bots).
601
+
595
602
try :
596
603
commands = await self .fetch_global_commands ()
597
604
self ._connection ._global_application_commands = { # type: ignore
@@ -678,9 +685,6 @@ async def _cache_application_command_permissions(self) -> None:
678
685
# This method is usually called once per bot start
679
686
if not isinstance (self , disnake .Client ):
680
687
raise NotImplementedError (f"This method is only usable in disnake.Client subclasses" )
681
-
682
- if not self ._sync_permissions :
683
- return
684
688
685
689
guilds_to_cache = set ()
686
690
for cmd in self .application_commands :
@@ -689,18 +693,29 @@ async def _cache_application_command_permissions(self) -> None:
689
693
for guild_id in cmd .permissions :
690
694
guilds_to_cache .add (guild_id )
691
695
696
+ if not self ._sync_permissions :
697
+ if guilds_to_cache :
698
+ print (
699
+ "[WARNING] You're using the @commands.guild_permissions decorator, however,"
700
+ f" the 'sync_permissions' kwarg of '{ self .__class__ .__name__ } ' is set to 'False'."
701
+ )
702
+ return
703
+
692
704
for guild_id in guilds_to_cache :
693
- perms = await self .bulk_fetch_command_permissions (guild_id )
694
- self ._connection ._application_command_permissions [guild_id ] = {
695
- perm .id : perm for perm in perms
696
- }
705
+ try :
706
+ perms = await self .bulk_fetch_command_permissions (guild_id )
707
+ self ._connection ._application_command_permissions [guild_id ] = {
708
+ perm .id : perm for perm in perms
709
+ }
710
+ except Exception :
711
+ pass
697
712
698
713
async def _sync_application_command_permissions (self ) -> None :
699
- # Assuming that all relevant permissions are cached
714
+ # Assuming that permissions and commands are cached
700
715
if not isinstance (self , disnake .Client ):
701
716
raise NotImplementedError (f"This method is only usable in disnake.Client subclasses" )
702
717
703
- if not self ._sync_commands or not self . _sync_permissions or self .loop .is_closed ():
718
+ if not self ._sync_permissions or self .loop .is_closed ():
704
719
return
705
720
706
721
guilds_to_compare : Dict [int , List [Any ]] = {} # {guild_id: [partial_perms, ...], ...}
@@ -1196,22 +1211,21 @@ async def process_application_commands(self, interaction: ApplicationCommandInte
1196
1211
if expected_command is None :
1197
1212
expected_command = self .get_guild_command (interaction .guild_id , interaction .data .id ) # type: ignore
1198
1213
1199
- if expected_command is None :
1214
+ if expected_command is None and self . _sync_commands :
1200
1215
# This usually comes from the blind spots of the sync algorithm.
1201
1216
# Since not all guild commands are cached, it is possible to experience such issues.
1202
1217
# In this case, the blind spot is the interaction guild, let's fix it:
1203
- if self ._sync_commands :
1204
- try :
1205
- await interaction .response .send_message (
1206
- 'This is a deprecated local command, which is now deleted.' , ephemeral = True
1207
- )
1208
- except Exception :
1209
- pass
1210
- try :
1211
- await self .bulk_overwrite_guild_commands (interaction .guild_id , []) # type: ignore
1212
- except Exception :
1213
- pass
1214
- return
1218
+ try :
1219
+ await interaction .response .send_message (
1220
+ 'This is a deprecated local command, which is now deleted.' , ephemeral = True
1221
+ )
1222
+ except Exception :
1223
+ pass
1224
+ try :
1225
+ await self .bulk_overwrite_guild_commands (interaction .guild_id , []) # type: ignore
1226
+ except Exception :
1227
+ pass
1228
+ return
1215
1229
1216
1230
self .dispatch (event_name , interaction )
1217
1231
try :
0 commit comments