Skip to content

Commit 5fafad9

Browse files
committed
Small improvements of auto sync tools
1 parent 5014ed0 commit 5fafad9

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

disnake/ext/commands/interaction_bot_base.py

+36-22
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,13 @@ async def _cache_application_commands(self) -> None:
592592

593593
_, guilds = self._ordered_unsynced_commands(self._test_guilds)
594594

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+
595602
try:
596603
commands = await self.fetch_global_commands()
597604
self._connection._global_application_commands = { # type: ignore
@@ -678,9 +685,6 @@ async def _cache_application_command_permissions(self) -> None:
678685
# This method is usually called once per bot start
679686
if not isinstance(self, disnake.Client):
680687
raise NotImplementedError(f"This method is only usable in disnake.Client subclasses")
681-
682-
if not self._sync_permissions:
683-
return
684688

685689
guilds_to_cache = set()
686690
for cmd in self.application_commands:
@@ -689,18 +693,29 @@ async def _cache_application_command_permissions(self) -> None:
689693
for guild_id in cmd.permissions:
690694
guilds_to_cache.add(guild_id)
691695

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+
692704
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
697712

698713
async def _sync_application_command_permissions(self) -> None:
699-
# Assuming that all relevant permissions are cached
714+
# Assuming that permissions and commands are cached
700715
if not isinstance(self, disnake.Client):
701716
raise NotImplementedError(f"This method is only usable in disnake.Client subclasses")
702717

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():
704719
return
705720

706721
guilds_to_compare: Dict[int, List[Any]] = {} # {guild_id: [partial_perms, ...], ...}
@@ -1196,22 +1211,21 @@ async def process_application_commands(self, interaction: ApplicationCommandInte
11961211
if expected_command is None:
11971212
expected_command = self.get_guild_command(interaction.guild_id, interaction.data.id) # type: ignore
11981213

1199-
if expected_command is None:
1214+
if expected_command is None and self._sync_commands:
12001215
# This usually comes from the blind spots of the sync algorithm.
12011216
# Since not all guild commands are cached, it is possible to experience such issues.
12021217
# 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
12151229

12161230
self.dispatch(event_name, interaction)
12171231
try:

0 commit comments

Comments
 (0)