diff options
author | 2023-01-11 22:24:36 +0000 | |
---|---|---|
committer | 2023-01-11 22:24:36 +0000 | |
commit | 08a7a742f61d655c2bd4fd02b95bb70e3d4b815e (patch) | |
tree | 851d6aebf95d613e0e04f57b04546f6a2db6a663 | |
parent | Merge pull request #170 from shtlrs/140-save-references-of-created-tasks (diff) | |
parent | Sync global and guild app command tree on startup (diff) |
Merge pull request #171 from python-discord/sync-tree-on-startupv9.4.0
Sync global and guild app command tree on startup
-rw-r--r-- | docs/changelog.rst | 3 | ||||
-rw-r--r-- | pydis_core/_bot.py | 26 | ||||
-rw-r--r-- | pyproject.toml | 2 |
3 files changed, 24 insertions, 7 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index 0734158a..97661180 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,9 @@ Changelog ========= +- :release:`9.4.0 <24th December 2022>` +- :feature:`171` Sync all app commands after extensions have been loaded. This release also removes the need to run :obj:`pydis_core.BotBase.load_extensions` in a task. + - :release:`9.3.1 <23rd December 2022>` - :bug:`170` Save references of newly created tasks in :obj:`pydis_core.utils.scheduling` 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): diff --git a/pyproject.toml b/pyproject.toml index cac29809..0d173a9a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pydis_core" -version = "9.3.1" +version = "9.4.0" description = "PyDis core provides core functionality and utility to the bots of the Python Discord community." authors = ["Python Discord <[email protected]>"] license = "MIT" |