Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
26 changes: 20 additions & 6 deletions pydis_core/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(

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

Expand Down Expand Up @@ -116,18 +117,31 @@ def _connect_statsd(
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):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <info@pythondiscord.com>"]
license = "MIT"
Expand Down