Skip to content

Commit

Permalink
Add BotBase.wait_until_bot_started which can be used to hold a proces…
Browse files Browse the repository at this point in the history
…s until all extensions are loaded.
  • Loading branch information
ChrisLovering committed Jul 25, 2023
1 parent b52f1ab commit 487f04f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Changelog
=========


- :release:`10.1.0 <25th July 2023>`
- :feature:`190` Add :obj:`pydis_core.BotBase.wait_until_bot_started` which can be used to hold a process until all extensions are loaded. This also waits until all app commands have synced, if ``sync_app_commands`` was set when calling :obj:`pydis_core.BotBase.load_extensions`.
- :feature:`190` Overwrite :obj:`discord.ext.commands.Bot.process_commands` to first call :obj:`pydis_core.BotBase.wait_until_bot_started` to ensure no commands are processed until all extensions are loaded.


- :release:`10.0.0 <14th July 2023>`
- :breaking:`188` Support sending multiple files at once to paste service. All calls to :obj:`pydis_core.utils.paste_service.send_to_paste_service` must now provide a list of :obj:`pydis_core.utils.paste_service.PasteFile`
- :bug:`187 major` Fix :obj:`pydis_core.utils.channel.get_or_fetch_channel`'s return type to include :obj:`discord.abc.PrivateChannel` and :obj:`discord.Thread`.
Expand Down
18 changes: 18 additions & 0 deletions pydis_core/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(

self._statsd_timerhandle: asyncio.TimerHandle | None = None
self._guild_available: asyncio.Event | None = None
self._bot_started: asyncio.Event | None = None
self._extension_loading_task: asyncio.Task | None = None

self.stats: AsyncStatsClient | None = None
Expand Down Expand Up @@ -132,6 +133,7 @@ async def _sync_app_commands(self) -> None:
await self._extension_loading_task
await self.tree.sync()
await self.tree.sync(guild=discord.Object(self.guild_id))
await self._bot_started.set()

async def load_extensions(self, module: types.ModuleType, sync_app_commands: bool = True) -> None:
"""
Expand All @@ -143,6 +145,8 @@ async def load_extensions(self, module: types.ModuleType, sync_app_commands: boo
self._extension_loading_task = scheduling.create_task(self._load_extensions(module))
if sync_app_commands:
scheduling.create_task(self._sync_app_commands())
else:
await self._bot_started.set()

def _add_root_aliases(self, command: commands.Command) -> None:
"""Recursively add root aliases for ``command`` and any of its subcommands."""
Expand Down Expand Up @@ -231,6 +235,19 @@ async def wait_until_guild_available(self) -> None:
"""
await self._guild_available.wait()

async def wait_until_bot_started(self) -> None:
"""
Wait until :obj:`pydis_core.BotBase.load_extensions` has finished loading all extensions.
This also waits until all app commands have synced, if sync_app_commands was set.
"""
await self._bot_started.wait()

async def process_commands(self, message: discord.Message) -> None:
"""Wait until all extensions are loaded before processing commands."""
await self.wait_until_bot_started()
await super().process_commands(message)

async def setup_hook(self) -> None:
"""
An async init to startup generic services.
Expand All @@ -241,6 +258,7 @@ async def setup_hook(self) -> None:
"""
loop = asyncio.get_running_loop()

self._bot_started = asyncio.Event()
self._guild_available = asyncio.Event()

self._resolver = aiohttp.AsyncResolver()
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 = "10.0.0"
version = "10.1.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

0 comments on commit 487f04f

Please sign in to comment.