aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_core/_bot.py
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2022-12-24 15:17:51 +0000
committerGravatar Chris Lovering <[email protected]>2022-12-26 16:37:55 +0000
commitc4d563057d4eea07c958d33cff432ccd3c164f72 (patch)
tree851d6aebf95d613e0e04f57b04546f6a2db6a663 /pydis_core/_bot.py
parentMerge pull request #170 from shtlrs/140-save-references-of-created-tasks (diff)
Sync global and guild app command tree on startup
Diffstat (limited to 'pydis_core/_bot.py')
-rw-r--r--pydis_core/_bot.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/pydis_core/_bot.py b/pydis_core/_bot.py
index 56814f27..09ab2a63 100644
--- a/pydis_core/_bot.py
+++ b/pydis_core/_bot.py
@@ -82,6 +82,7 @@ class BotBase(commands.Bot):
self._statsd_timerhandle: Optional[asyncio.TimerHandle] = None
self._guild_available: Optional[asyncio.Event] = None
+ self._extension_loading_task: asyncio.Task | None = None
self.stats: Optional[AsyncStatsClient] = None
@@ -116,18 +117,31 @@ class BotBase(commands.Bot):
attempt + 1
)
- async def load_extensions(self, module: types.ModuleType) -> None:
- """
- Load all the extensions within the given module and save them to ``self.all_extensions``.
-
- This should be ran in a task on the event loop to avoid deadlocks caused by ``wait_for`` calls.
- """
+ async def _load_extensions(self, module: types.ModuleType) -> None:
+ """Load all the extensions within the given module and save them to ``self.all_extensions``."""
await self.wait_until_guild_available()
self.all_extensions = walk_extensions(module)
for extension in self.all_extensions:
scheduling.create_task(self.load_extension(extension))
+ async def _sync_app_commands(self) -> None:
+ """Sync global & guild specific application commands after extensions are loaded."""
+ await self._extension_loading_task
+ await self.tree.sync()
+ await self.tree.sync(guild=discord.Object(self.guild_id))
+
+ async def load_extensions(self, module: types.ModuleType, sync_app_commands: bool = True) -> None:
+ """
+ Load all the extensions within the given ``module`` and save them to ``self.all_extensions``.
+
+ Args:
+ sync_app_commands: Whether to sync app commands after all extensions are loaded.
+ """
+ self._extension_loading_task = scheduling.create_task(self._load_extensions(module))
+ if sync_app_commands:
+ scheduling.create_task(self._sync_app_commands())
+
def _add_root_aliases(self, command: commands.Command) -> None:
"""Recursively add root aliases for ``command`` and any of its subcommands."""
if isinstance(command, commands.Group):