Skip to content

Commit 08a7a74

Browse files
Merge pull request #171 from python-discord/sync-tree-on-startup
Sync global and guild app command tree on startup
2 parents ae1520e + c4d5630 commit 08a7a74

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Changelog
55
=========
66

7+
- :release:`9.4.0 <24th December 2022>`
8+
- :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.
9+
710

811
- :release:`9.3.1 <23rd December 2022>`
912
- :bug:`170` Save references of newly created tasks in :obj:`pydis_core.utils.scheduling`

pydis_core/_bot.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __init__(
8282

8383
self._statsd_timerhandle: Optional[asyncio.TimerHandle] = None
8484
self._guild_available: Optional[asyncio.Event] = None
85+
self._extension_loading_task: asyncio.Task | None = None
8586

8687
self.stats: Optional[AsyncStatsClient] = None
8788

@@ -116,18 +117,31 @@ def _connect_statsd(
116117
attempt + 1
117118
)
118119

119-
async def load_extensions(self, module: types.ModuleType) -> None:
120-
"""
121-
Load all the extensions within the given module and save them to ``self.all_extensions``.
122-
123-
This should be ran in a task on the event loop to avoid deadlocks caused by ``wait_for`` calls.
124-
"""
120+
async def _load_extensions(self, module: types.ModuleType) -> None:
121+
"""Load all the extensions within the given module and save them to ``self.all_extensions``."""
125122
await self.wait_until_guild_available()
126123
self.all_extensions = walk_extensions(module)
127124

128125
for extension in self.all_extensions:
129126
scheduling.create_task(self.load_extension(extension))
130127

128+
async def _sync_app_commands(self) -> None:
129+
"""Sync global & guild specific application commands after extensions are loaded."""
130+
await self._extension_loading_task
131+
await self.tree.sync()
132+
await self.tree.sync(guild=discord.Object(self.guild_id))
133+
134+
async def load_extensions(self, module: types.ModuleType, sync_app_commands: bool = True) -> None:
135+
"""
136+
Load all the extensions within the given ``module`` and save them to ``self.all_extensions``.
137+
138+
Args:
139+
sync_app_commands: Whether to sync app commands after all extensions are loaded.
140+
"""
141+
self._extension_loading_task = scheduling.create_task(self._load_extensions(module))
142+
if sync_app_commands:
143+
scheduling.create_task(self._sync_app_commands())
144+
131145
def _add_root_aliases(self, command: commands.Command) -> None:
132146
"""Recursively add root aliases for ``command`` and any of its subcommands."""
133147
if isinstance(command, commands.Group):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydis_core"
3-
version = "9.3.1"
3+
version = "9.4.0"
44
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
55
authors = ["Python Discord <info@pythondiscord.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)